How to set parameters for SqlDataSource UpdateCommand

For a Gridview:

I am trying to use a stored procedure for the first time in a SQLDataSource for the UpdateCommand:

<asp:SqlDataSource ID="TECT_DataSource" runat="server"
    ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName %>"
    SelectCommand="SELECT MPID, User_Id, Last_Name, First_Name
                   FROM Scripts.vw_Tect_Exam"
    UpdateCommand="P_TECT_UPD_EXAM_ID" UpdateCommandType="StoredProcedure">  
    <UpdateParameters>                    
        <asp:Parameter Name="MPID" Type="Int32"  />  
        <asp:Parameter Name="User_Id" Type="String" />   
    </UpdateParameters> 
</asp:SqlDataSource>

I am wondering how the UpdateParameters get their values set, since I only specify a name?
The procedure P_TECT_UPD_EXAM_ID expects two parameters as input: "in_MPID" and "in_UserId"
I am also wondering how to map those values to the input parameters for the procedure as the names are different?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can set them something like this :

Example : In the example MPID is the sql parameter name @MPID

<UpdateParameters>                    
    <asp:ControlParameter Name="MPID" ControlID="MPID_TextBox" PropertyName="Text  />  
    <asp:ControlParameter Name="User_Id" ControlID="User_Id_TextBox" PropertyName="Text  />  
</UpdateParameters>

Correction: Just spotted your proc param names so it must be

<asp:ControlParameter Name="in_MPID" ...............
<asp:ControlParameter Name="in_User_Id" ...............

Hope this helps….

Method 2

I really wouldn’t use a SqlDataSource. It will be much easier if you make the call to the database in the code-behind (or a better yet in a Data Access Layer).

If you use a SqlDataSource the stored procedure call will only be available on that page. Every time you want to make that same call you will have to copy and paste the SqlDataSource or make a UserControl out of it.

The following example uses the Entity Framework to connect to the database and retrieve records:

public List<Record> GetAllRecordsByUserName(string credentials)
{
    List<Record> recordList;
    using (CustomEntities context = new CustomEntities())
    {

        IQueryable<Section> recordQuery = from records in context.Records
                                              where records.UserName == credentials
                                              select records; 
        recordList = recordQuery.ToList<Record>();
    }
    return recordList;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x