Home > DeveloperSection > Forums > grid view bulk updating all cells
Pravesh Singh

Total Post:411

Points:2881
Posted on    August-25-2014 1:28 AM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 701  View(s)
Rate this:

I am using a asp.net grid view to load data from my sql data table.I am able to successfully load the data.

When user hits Edit button all the cells in the gridview are made editable so that user can edit and save the values.

My problem arises with the save button where i am unable to save the edited data back to the SQL.

Here is the code for the save button click:

protected void btnSave_Click(object sender, EventArgs e)

{

    int RowIndex=0;

    GridViewRow row = (GridViewRow)gvres.Rows[RowIndex];

    TextBox txtLanguage1 = row.FindControl("txtFName") as TextBox;

    TextBox txtLanguage2 = row.FindControl("txtLName") as TextBox;

    SqlConnection myConnection = new SqlConnection(connectionString);

    SqlCommand cmd = new SqlCommand("UPDATE UsersTable SET FirstName = @FirstName, LastName = @LastName WHERE Location = @Location", myConnection);

    cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());

    cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());

    myConnection.Open();

    cmd.ExecuteNonQuery();

    gvusers.EditIndex = -1;

    DataBind();

}

Exception:"Must declare the scalar variable "@Location"."



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    August-25-2014 2:26 AM

Hi Pravesh, 

try this:

protected void btnSave_Click(object sender, EventArgs e)

{

    int RowIndex=0;

    GridViewRow row = (GridViewRow)gvres.Rows[RowIndex];

    TextBox txtLanguage1 = row.FindControl("txtFName") as TextBox;

    TextBox txtLanguage2 = row.FindControl("txtLName") as TextBox;

    TextBox txtLanguage3 = row.FindControl("txtLocation") as TextBox;

    SqlConnection myConnection = new SqlConnection(connectionString);

    SqlCommand cmd = new SqlCommand("UPDATE UsersTable SET FirstName = @FirstName, LastName = @LastName WHERE Location = @Location", myConnection);

cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());

cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());

cmd.Parameters.AddWithValue("@Location", txtLocation.Text.Trim());

myConnection.Open();

cmd.ExecuteNonQuery();

gvusers.EditIndex = -1;

DataBind();

}


Don't want to miss updates? Please click the below button!

Follow MindStick