asp.netsqldatasource

What is wrong with this ASP.net DataBinding program?


I am used to writing the data on the client side, then with JavaScript, validate and submit forms to a new page on server side which updated the database and then go again.

I am thinking with programming on ASP.net, it is not a good practice to do it on client side because security is better on server side.

I am trying to do this using sqlDataSources and DataBinding unsuccessfully.

On my page I have only two input boxes. These boxes are empty.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Prueba.aspx.cs"
    Inherits="MaximaBR.com.Prueba" %>

<!DOCTYPE html>    
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:SqlDataSource id="SqlDataSource1" runat="server"
                DataSourceMode="DataReader"
                ConnectionString="<%$ ConnectionStrings:Data %>"
                SelectCommand="SELECT [idpropiedad],[propietario] 
                               FROM [dbo].[Propiedades] 
                               WHERE [idpropiedad] = 525293">
                <SelectParameters>
                    <asp:ControlParameter name="idpropiedad" 
                        controlid="idpropiedadBox" propertyname="Text" />
                    <asp:ControlParameter name="propietario" 
                        controlid="propietarioBox" propertyname="Text" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:TextBox id="idpropiedadBox" runat="server"
                DataSourceID="SqlDataSource1"></asp:TextBox>
            <asp:TextBox id="propietarioBox" runat="server" 
                DataSourceID="SqlDataSource1"></asp:TextBox>
        </div>
    </form>
</body>
</html>

I have read so many articles about this but my application is on WebForms, not mvc, so I would like to do it this way.

Also, I have so many fields with responsive html, so I need to not use gridview.

It seems to me that I lost the dataview and that's what I need. I got used to working with databind on Visual Basic 6.0, but here it seems a little tricky and can't get it to work.

Maybe one of my problems is that I don't understand the WebForm Cycles times for Page Loading and others cycles times like pre render?


Solution

    1. You need a databound server control to show / edit / update data. For a single row asp:FormView is OK.
    2. You need to bind child controls to underlying fields. Eval for one-way binding, Bind for two-way binding.

    The code may look like this.

    <asp:FormView runat="server" DefaultMode="Edit" DataSourceID="SqlDataSource1">
        <EditItemTemplate>
            <asp:TextBox ID="idpropiedadBox" runat="server" Text='<%#Bind("idpropiedad") %>'></asp:TextBox>
            <asp:TextBox ID="propietarioBox" runat="server" Text='<%#Bind("propietario") %>'></asp:TextBox>
            <asp:Button ID="btnUpdate" runat="server" CommandName="Update" Text="Update" />
        </EditItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource
        ID="SqlDataSource1"
        runat="server"
        DataSourceMode="DataSet"
        ConnectionString="<%$ ConnectionStrings:Data%>"
        SelectCommand="SELECT [idpropiedad],[propietario] FROM  [dbo].[Propiedades] WHERE [idpropiedad] = 525293 "
        UpdateCommand="update [dbo].[Propiedades] set [idpropiedad]=@idpropiedad,[propietario]=@propietario WHERE [idpropiedad] = 525293">
        <SelectParameters>
            <%--no need as select command doesn't have parameters --%>
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="idpropiedad" Type="Int32" />
            <asp:Parameter Name="propietario" Type="String" Size="1024" />
        </UpdateParameters>
    </asp:SqlDataSource>