locked
Pls explain data binding process (SQLDataSource, eval and bind, sql command paramters) RRS feed

  • Question

  • User-1842273979 posted

    Hello all!

    Pls help me to understand data binding process. I have sql data souce with select, update, insert and delete commands and formview. I got ptolems with updating data. But now it seems to be fixed. However it is not absolutly clear for me how does data binding process work. So my questions are:

    1) I have eval and bind methods. What have i  to put within <%@Eval ("XXX")%>  and <%# bind("XXX")%>  ? Is it table field name or  SQL command  parametr name?

    I tried to put param name then my insert command worked but update command (SQL param names of datasource and filed names were different like field1=@param1).

    2) Have SQL parametr names of datasource to match to fileds names of table     (like  field1=@filed1 or it can be like filed1=@param1)?

    I got all to work when param names matched to filed names...

    3) I can't understand how eval and bind method pass values to SQL data source paramters?(if i put field name into bind method how does it know which SQL datasource paramters to pass value)

    Tuesday, August 30, 2011 7:23 AM

Answers

  • User1194139608 posted

    second question

    if u make procedure 

    like create procedure sp_name

    (

    @name varchar(100)

    )insert into tbl_student(student_name)values(@name)

    on c# page u used

    @name not student_name

    @name="Twinkle";

    Thanks

    if u feel it's correct mark as answer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2011 2:22 AM
  • User3866881 posted

    However if update parameter's name differ from datafield names it is failed (records are not updated).Why??

    Hello,

    @movie_Id is only a token symbol, if you've defiend in the Parameters in the aspx codes, it will substitute for another suitable values. And movie_id is from DataKeyNames. It will first search whether you've defined in the Bind or Eval, if not found, then DataKeyNames.

    paramter???And where does @movie_id paramter take value from??? Does value come from property DAtaKeyName??? if it does what is the mechanism of it?

    You should include your movie_id in the UpdateParameters, otherwise it won't use it. Even it comes from DataKeyNames.

    Something like this:

    <UpdateParameters>
                   
    <asp:Parameter Name="title" Type="String" />
                   
    <asp:Parameter Name="idznar" Type="Int32" />
                    <asp:Parameter Name="movie_id"
     Type="Int32"/>
    </UpdateParameters>

    Why does "insert" work and "update" does not in case of paramters names are not the same as datafield names???

    No matter how you define the names, remember they are only TOKEN quote symbols, which will be substituted for other values later. So the running time only check and run the subtituted sql statement. If this right, update, delete or insert can be done.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 4, 2011 10:19 PM

All replies

  • User1135668169 posted

    Hi,

    Eval v/s Bind

    http://forums.asp.net/p/1097781/1661386.aspx

    Good Explanation of Eval and Bind 

    http://msdn.microsoft.com/en-us/library/ms178366.aspx

    Tuesday, August 30, 2011 7:48 AM
  • User1194139608 posted

    1) I have eval and bind methods. What have i  to put within <%@Eval ("XXX")%>  and <%# bind("XXX")%>  ? Is it table field name or  SQL command  parametr name?

    This is Table Field name

    or 

    it's as name like select tbl_id as account_id from table_name

    now u write in <%# Eval("account_id ")%>

    if u write select tbl_id from table name then u write

    now u write in <%# Eval("tbl_id ")%>


    Wednesday, August 31, 2011 1:27 AM
  • User-1842273979 posted

    Guys, thanks for replies.

    However the second question is still not clear for me...Have i to name SQL paramters as data files name?

    Wednesday, August 31, 2011 2:01 AM
  • User1194139608 posted

    second question

    if u make procedure 

    like create procedure sp_name

    (

    @name varchar(100)

    )insert into tbl_student(student_name)values(@name)

    on c# page u used

    @name not student_name

    @name="Twinkle";

    Thanks

    if u feel it's correct mark as answer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 31, 2011 2:22 AM
  • User-1842273979 posted

    Guys. thx for help!

    But ther is something i still need to sort out. There are snippets of my code.

    1. SQLDatasource

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"  
            ConnectionString="<%$ ConnectionStrings:dvd_collectionConnectionString %>" 
            ProviderName="<%$ ConnectionStrings:dvd_collectionConnectionString.ProviderName %>" 
            SelectCommand="SELECT movie_id, title, idznar FROM film" 
            InsertCommand="INSERT INTO film(title, idznar) VALUES (@filmtitle, @filmzanr)"
            UpdateCommand="UPDATE film SET title = @title, idznar = @idznar WHERE (movie_id = @movie_id)"
            DeleteCommand="DELETE FROM film WHERE movie_id = @movie_id">
          
                <InsertParameters>
                    <asp:Parameter Name="filmtitle" Type="String" />
                    <asp:Parameter Name="filmzanr" Type="Int32" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="title" Type="String" />
                    <asp:Parameter Name="idznar" Type="Int32" />
                </UpdateParameters>
    </asp:SqlDataSource>

    2. FromView

    <asp:FormView ID="formsview1" runat="server" DataSourceID="SqlDataSource1" 
        CellPadding="4" ForeColor="#333333" DataKeyNames="movie_id" AllowPaging="True">
    
    <EditItemTemplate>
                movie_id:
                <asp:Label ID="movie_idLabel1" runat="server" Text='<%# Eval("movie_id") %>' />
                <br />
                title:
                <asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("title") %>' />
                <br />
                idznar:
                <asp:TextBox ID="idznarTextBox" runat="server" Text='<%# Bind("idznar") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update" />
                    &nbsp;
                <asp:LinkButton ID="UpdateCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
    
     <InsertItemTemplate>
                title:
                <asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("filmtitle") %>' />
                <br />
                idznar:
                <asp:TextBox ID="idznarTextBox" runat="server" Text='<%# Bind("filmzanr") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="Insert" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </InsertItemTemplate>
    </asp:FormView>

    So my qestions are almost the same...

    1) @movie_id parameter

    I even did not place this paramter into <deleteparamter/> and <UpdateParametr/>. So why there is no error that i did not declare this paramter???And where does @movie_id paramter take value from??? Does value come from property DAtaKeyName??? if it does what is the mechanism of it?

    2)Insert and Update parameters

    My insert parametr's name are not the same as datafiled names. And it is fine to insert new record. However if update parameter's name differ from datafield names it is failed (records are not updated).Why?? Why does "insert" work and "update" does not in case of paramters names are not the same as datafield names???

    Thursday, September 1, 2011 12:58 AM
  • User3866881 posted

    However if update parameter's name differ from datafield names it is failed (records are not updated).Why??

    Hello,

    @movie_Id is only a token symbol, if you've defiend in the Parameters in the aspx codes, it will substitute for another suitable values. And movie_id is from DataKeyNames. It will first search whether you've defined in the Bind or Eval, if not found, then DataKeyNames.

    paramter???And where does @movie_id paramter take value from??? Does value come from property DAtaKeyName??? if it does what is the mechanism of it?

    You should include your movie_id in the UpdateParameters, otherwise it won't use it. Even it comes from DataKeyNames.

    Something like this:

    <UpdateParameters>
                   
    <asp:Parameter Name="title" Type="String" />
                   
    <asp:Parameter Name="idznar" Type="Int32" />
                    <asp:Parameter Name="movie_id"
     Type="Int32"/>
    </UpdateParameters>

    Why does "insert" work and "update" does not in case of paramters names are not the same as datafield names???

    No matter how you define the names, remember they are only TOKEN quote symbols, which will be substituted for other values later. So the running time only check and run the subtituted sql statement. If this right, update, delete or insert can be done.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 4, 2011 10:19 PM
  • User-1842273979 posted

    Thanks, Decker Dong!

    it is more clear now.  I would like to investigate data binding process more deeply. If  you guys can give me links to articles or maybe books it  would be really appreaciated.

    Monday, September 5, 2011 5:17 AM