Welcome to AspAdvice Sign in | Join | Help

AzamSharp

Some day I will know everything I hope that day never comes

Syndication

Tags

Navigation

GridView Update All Rows At Once

Few weeks back I wrote an article in which I explained how to change the state of the GridView in edit mode where all rows are replaced by the textboxes. You can check out the article at the link below:

http://gridviewguy.com/ArticleDetails.aspx?articleID=219

Serveral readers asked me how to send the data to the database at one go. Well, you can contruct an XML string or appending text and send the text to the database to be executed as a query. You need to be very careful when doing this sort of thing since, you might be inviting SQL Injections to the party. You can use Server.HtmlEncode to encode the strings and then send to them to be executed as a query.

Here is the Update method which iterates through all the rows in the Gridview and creates a long update statement.

private void Update()

{

StringBuilder sb = new StringBuilder();

// build the query

foreach (GridViewRow row in GridView1.Rows)

{

sb.Append("UPDATE Users SET FirstName = '");

sb.Append((row.FindControl("txtFirstName") as TextBox).Text);

sb.Append("',");

sb.Append("LastName = '");

sb.Append((row.FindControl("txtLastName") as TextBox).Text);

sb.Append("', ");

sb.Append("ClassCode = '");

sb.Append((row.FindControl("txtClassCode") as TextBox).Text);

sb.Append("'");

sb.Append(" WHERE UserID = ");

sb.Append(Convert.ToInt32((row.FindControl("lblUserID") as Label).Text));

sb.Append(" ");

}

string connectionString = "Server=HCUBE008;Database=School;Trusted_Connection=true";

SqlConnection myConnection = new SqlConnection(connectionString);

SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection);

myConnection.Open();

myCommand.ExecuteNonQuery();

myConnection.Close();

}

Sponsor

Published Monday, December 18, 2006 3:40 PM by azamsharp

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: GridView Update All Rows At Once @ Wednesday, February 07, 2007 2:56 AM

Wouldn't it be better to leave the plumbing to an SqlDataAdapter or something? Seems a bit inefficient to generate SQL code manually for every row... Also there's an overhead on doing FindControl so maybe (if the control hierarchy is guaranteed to be the same for every row) it'd be better to make a note of which control is at which index.

Joe

# re: GridView Update All Rows At Once @ Wednesday, February 07, 2007 4:21 PM

I agree with you about the index. The only reason I did not used the index is what if I need to add one more columns at in the middle of other columns then I would have to re-arrange all the columns again.

The SqlDataAdapter can be used to update a DataSet. But, SqlDataAdapter.Update() method calls the query on each row inside the DataSet. This means if I have 300 rows then the database access will be 300 times.

Generating the query has overhead but it updates the database in one shot using only a single access.

azamsharp

# re: GridView Update All Rows At Once @ Wednesday, February 07, 2007 8:33 PM

That's true - it does seem like more overhead to be submitting multiple statements sequentially when it could be submitted in one hit. Surprisingly, however, my experience has shown that when you look at the performance in SQL Server Profiler (or something similar) it's actually *much* faster performance-wise to do segmented queries in the way that the SqlDataAdapter does them. As an example, try doing 1000 update statements in a block. Example: UPDATE [MyTable] SET [Column]='a' WHERE ID=1 UPDATE [MyTable] SET [Column]='b' WHERE ID=2 [...] Then add a 'go' command after each one (obviously this will need to be done in SQL Query Analyzer) UPDATE [MyTable] SET [Column]='a' WHERE ID=1 go UPDATE [MyTable] SET [Column]='b' WHERE ID=2 go [...] Performance is much, much faster when SQL Server processes the statements one at a time rather than all at once. Strange but true - I discovered it when spending days ironing out performance problems in a rather hefty SQL-based system once.

Joe

# re: GridView Update All Rows At Once @ Wednesday, February 07, 2007 9:44 PM

Yes, you are right! Some time back I also did a small test and the results showed that inserting data in batches in faster in most cases.

http://aspadvice.com/blogs/azamsharp/archive/2006/10/14/Multiple-Insert-in-Database-Test-Results.aspx

azamsharp

# re: GridView Update All Rows At Once @ Tuesday, June 26, 2007 12:45 PM

i have read about sqldata adapter's batch update method but not so much but it can be used to update several rows at a time like we sqlbulkcopy, i just want to know that sqldata adapter's method is better or above ?

what about sqldataAdapter's Batch Update method

# re: GridView Update All Rows At Once @ Tuesday, June 26, 2007 12:50 PM

Hi,

It all depends on the particular scenario. Sometimes, a batch update is much faster then submitting all the statements sequentially. Take a look at the link above where I tested different approaches. Maybe you will get some idea about the performance.

azamsharp

# re: GridView Update All Rows At Once @ Wednesday, June 27, 2007 1:12 AM

Hi, how about if we have checkboxes in gridview that when users checks on several ones those rows will be editable at once, how can we manage that without button click?

jeff

# re: GridView Update All Rows At Once @ Wednesday, June 27, 2007 1:14 PM

Hi Jeff,

Thanks for the comment. Your technique sounds interesting. It will make an interesting article. I will check on it and post the reply!

Thanks,

azamsharp

# Edit GridView Using CheckBoxes(转载) @ Sunday, July 08, 2007 10:45 AM

SometimebackIwroteapostabouthowtoconvertthewholeGridViewcontrolintotheeditmodewith...

earlier

# re: GridView Update All Rows At Once @ Tuesday, October 16, 2007 2:23 AM

do u have a complete working example of the bulk update gridview? thanks in advance

certuser

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below