Click here to Skip to main content
15,307,392 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
SqlDataSource Name = ds_CompanyDetails

ds_CompanyDetails Update Command

UpdateCommand="DECLARE @CompanyName NVARCHAR(50) DECLARE @CompanyDetailId INT UPDATE CompanyDetail SET CompanyName=@CompanyName WHERE CompanyDetailId=@CompanyDetailId"


On Button Click trying to update SqlDataSource
C#
protected void btnSave_Click(object sender, EventArgs e)
        {
            ds_CompanyDetails.UpdateParameters.Add("@CompanyName",txtCompanyName.Text);
            ds_CompanyDetails.UpdateParameters.Clear();
            ds_CompanyDetails.UpdateParameters.Add("@CompanyDetailId", lblId.Text);
            ds_CompanyDetails.UpdateParameters.Clear();
            ds_CompanyDetails.Update();
            ds_CompanyDetails.DataBind();
            Response.Redirect("~/Company.aspx");
        }


after button click nothing is happened.
Is there any mistake?
Posted
Comments
   
But why? What exactly you want to achieve?
PIEBALDconsult 3-Jan-16 0:35am
   
Because you keep clearing the parameters maybe?
And why do you try to declare variables in the SQL statement?

1 solution

I can see two problems...
1. Your SQL is written in a single line and no separators (like ; ) in between, so it should lead to some errors on the SQL side...Try something like this:
SQL
UpdateCommand="DECLARE @CompanyName NVARCHAR(50); DECLARE @CompanyDetailId INT; UPDATE CompanyDetail SET CompanyName=@CompanyName WHERE CompanyDetailId=@CompanyDetailId;"

2. You clear all the parameters immediately after you init them...Change your code like this:
C#
ds_CompanyDetails.UpdateParameters.Clear();
ds_CompanyDetails.UpdateParameters.Add("@CompanyName",txtCompanyName.Text);
ds_CompanyDetails.UpdateParameters.Add("@CompanyDetailId", lblId.Text);
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900