In Asp.net, all records of the gridview are updated at one time)

Source: Internet
Author: User

From: http://www.cnblogs.com/failyblue/archive/2008/07/03/1234966.html

I have implemented my functions by referring to the second method. However, the "invalid character \ n" error is reported when multiple statements are executed in the second method. I will slightly modify the second method. For details about the modification, refer to the red section in the second method in this article.

 

The original article is as follows:

We often encounter this situation. In all records listed in the gridview, we sometimes need to modify multiple records at the same time and save them to the database. So how should we implement it in the gridview? In the gridview, there are two implementation methods:

First, let's take a look at the first method. This method uses sqldatasource to update all records, but this method is slow, because every time a record is updated, a data connection is established and the updatecommand is executed, will affect the performance. Its mainCodeAs follows:

  Void Button#click ( Object Sender, eventargs E)
{
For ( Int I =   0 ; I < Gridview1.rows. Count; I ++ )
{
Gridviewrow row = Gridview1.rows;
Sqldatasource1.updateparameters [ 0 ]. Defaultvalue = (Textbox) Row. cells [ 0 ]. Findcontrol ( " Textbox2 " ). Text;
Sqldatasource1.updateparameters [ 1 ]. Defaultvalue = (Textbox) Row. cells [ 1 ]. Findcontrol ( " Textbox3 " ). Text;
Sqldatasource1.updateparameters [2] . Defaultvalue = Gridview1.datakeys. value. tostring ();
Sqldatasource1.update ();
}
}

 

In the above Code, we must first specify the updateparameters parameter set, that is, specify the fields to be updated and their types. Then, the updatecommand Statement of sqldatasource is pointed out. In the Click Event of the update button button1, The for loop will be used in the form of traversal to check each row in the gridview, put the content of each updated text box in the updateparameters parameter of sqldatasouce, and call the update method of sqldatasource to complete the update.

Method 2 uses to traverse each row in the gridview and use an SQL statement to connect the updated content before using command. executenonquery () is updated with high efficiency. The main code is as follows:

Protected   Void Page_load ( Object Sender, eventargs E)
{
If ( ! Page. ispostback)
{
Sqlconnection con =   New Sqlconnection (configurationmanager. connectionstrings [ " Appconnectionstring1 " ]. Connectionstring );
Sqlcommand command =   New Sqlcommand ( " Select [customerid], [companyName], [contactname], [contacttitle] from [Customers] " , Con );
Con. open ();
Gridview1.datasource = Command. executereader ();
Gridview1.databind ();
Con. Close ();
}
}
Protected   Void Button#click ( Object Sender, eventargs E)
{

Stringbuilder Query= NewStringbuilder ();

Query. append ("begin"). append ("\ n ");

For ( Int I =   0 ; I <gridview1.rows. Count; I ++ )
{
Gridviewrow row = Gridview1.rows [I];
  String Value1 = (Textbox) Row. cells [ 0 ]. Findcontrol ( " Textbox2 " ). Text. Replace ( " ' " , " '' " );
  String Value2 = (Textbox) Row. cells [ 1 ]. Findcontrol ( " Textbox3 " ). Text. Replace ( " ' " , " '' " );
  String Value3 = Gridview1.datakeys [I]. value. tostring ();
Query. append ( " Update [MERs] Set [companyName] =' " ). Append (value1). append ( " ', [Contacttitle] =' " )
. Append (value2). append ( " 'Where [mermerid] =' " ). Append (value3). append ( " '; \ N " );
}
Query. append ("end ;"); Sqlconnection con =   New Sqlconnection (configurationmanager. connectionstrings [ " Appconnectionstring1 " ]. Connectionstring );
Sqlcommand command =   New Sqlcommand (query. tostring (), con );
Con. open ();
Command. executenonquery ();
Con. Close ();
}
}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.