View plaincopy to clipboardprint?
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. Data. SQL;
/// <Summary>
/// Manually insert, update, sort, and delete data in the gridview
/// I have been troubled by this job for several days. It has finally been completed with complete functions and improved Exception Handling. I have not performed SQL injection only.
/// Development Environment vs2008, Database Name: manualdb, table name: Student, change the environment, please note that you modify the table definition and database connection string
/// </Summary>
Public partial class _ default: system. Web. UI. Page
{
/// <Summary>
/// Pre-load the page, initialize the sorting and Data
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Viewstate ["sortorder"] = "studentid"; // Save the status through the view
Viewstate ["orderdire"] = "ASC ";
BIND ();
}
}
/// <Summary>
/// Data binding method, which sorts data through views
/// </Summary>
Protected void BIND ()
{
Dataset DS = new dataset ();
String commandtext = "select * from student ";
Sqlconnection conn = getconn ();
Sqldataadapter sqglad = new sqldataadapter (commandtext, Conn );
Try
{
Sqglad. Fill (DS, "student ");
Dataview view = Ds. Tables ["student"]. defaultview;
String sort = (string) viewstate ["sortorder"] + "" + (string) viewstate ["orderdire"];
View. Sort = sort;
Gridview1.datasource = view;
Gridview1.datakeynames = new string [] {"studentid"}; // identifies the primary key of the table, which will be used later
Gridview1.databind ();
Sqglad. Dispose (); // release the adapter Resource
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("failed to read data, error message:" + err. Message );
}
}
/// <Summary>
/// Row data deletion method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> E is the source that triggers the deletion event. </param>
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
Sqlconnection conn = getconn ();
String commandtext = "delete student where studentid = '" + gridview1.datakeys [E. rowindex]. value. tostring () + "'";
Sqlcommand sqlcom = new sqlcommand (commandtext, Conn );
/*
* Exception handling, the same below: If the SQL statement is executed smoothly but the data bit is 0, an error is prompted; otherwise, an error message is printed.
*/
Try
{
Conn. open ();
Int I = sqlcom. executenonquery ();
If (I <= 0)
Response. Write ("<MCE: script language = JavaScript> <! --
Alert (creation failed! ')
// --> </MCE: SCRIPT> "); // The number of query results affected is 0.
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("deletion failed, error message:" + err. Message );
}
BIND ();
}
/// <Summary>
/// The row editing method. Only the gridview status is changed.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
Gridview1.editindex = E. neweditindex;
BIND ();
}
/// <Summary>
/// Cancel row editing
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
Gridview1.editindex =-1;
BIND ();
}
/// <Summary>
/// Row data update Method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> use e to obtain the new value of the current row. </param>
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
String name = (textbox) gridview1.rows [E. rowindex]. cells [2]. controls [0]). Text. tostring (). Trim ();
String sex = (textbox) gridview1.rows [E. rowindex]. cells [3]. controls [0]). Text. tostring (). Trim ();
String telephone = (textbox) gridview1.rows [E. rowindex]. cells [4]. controls [0]). Text. tostring (). Trim ();
String comtext = "Update student set name = '" + name + "', sex = '" + sex + "', telephone = '"+ telephone +" 'where studentid =' "+ gridview1.datakeys [E. rowindex]. value. tostring () + "'";
Sqlconnection conn = getconn ();
Sqlcommand sqlcom = new sqlcommand (comtext, Conn );
Try
{
Conn. open ();
Int I = sqlcom. executenonquery ();
If (I <= 0) response. Write ("<MCE: script language = JavaScript> <! --
Alert (creation failed! ')
// --> </MCE: SCRIPT> ");
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("failed to update data, error message:" + err. Message );
}
Gridview1.editindex =-1;
BIND ();
}
/// <Summary>
/// Sorting method. If it is in ascending order, it is in descending order, and vice versa.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void gridviewinclusorting (Object sender, gridviewsorteventargs E)
{
String spage = E. sortexpression;
If (viewstate ["sortorder"]. tostring () = spage)
{
If (viewstate ["orderdire"]. tostring () = "DESC ")
Viewstate ["orderdire"] = "ASC ";
Else
Viewstate ["orderdire"] = "DESC ";
}
Else
{
Viewstate ["sortorder"] = E. sortexpression;
}
BIND ();
}
/// <Summary>
/// General method to obtain a database connection
/// </Summary>
/// <Returns> </returns>
Protected sqlconnection getconn ()
{
String connstring = "Data Source = MIRROR-PC // sqlexpress; initial catalog = manualdb; Integrated Security = true; pooling = false ";
Sqlconnection conn = new sqlconnection (connstring );
Return conn;
}
/// <Summary>
/// Add a data method. After adding a data method, the reset text box is blank.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void Add _ click (Object sender, eventargs E)
{
String commdstr = "insert into student (name, sex, telephone) values ('" + textbox4.text + "', '" + textbox2.text + "', '" + textbox3.text + "') ";
Sqlconnection conn = getconn ();
Sqlcommand sqlcom = new sqlcommand (commdstr, Conn );
Try
{
Conn. open ();
Int I = sqlcom. executenonquery ();
If (I <= 0) response. Write ("<MCE: script language = JavaScript> <! --
Alert (creation failed! ')
// --> </MCE: SCRIPT> ");
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("creation failed, error message:" + err. Message );
}
BIND ();
Textbox3.text = ""; textbox4.text = ""; textbox2.text = "";
}
}
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. Data. SQL;
/// <Summary>
/// Manually insert, update, sort, and delete data in the gridview
/// I have been troubled by this job for several days. It has finally been completed with complete functions and improved Exception Handling. I have not performed SQL injection only.
/// Development Environment vs2008, Database Name: manualdb, table name: Student, change the environment, please note that you modify the table definition and database connection string
/// </Summary>
Public partial class _ default: system. Web. UI. Page
{
/// <Summary>
/// Pre-load the page, initialize the sorting and Data
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Viewstate ["sortorder"] = "studentid"; // Save the status through the view
Viewstate ["orderdire"] = "ASC ";
BIND ();
}
}
/// <Summary>
/// Data binding method, which sorts data through views
/// </Summary>
Protected void BIND ()
{
Dataset DS = new dataset ();
String commandtext = "select * from student ";
Sqlconnection conn = getconn ();
Sqldataadapter sqglad = new sqldataadapter (commandtext, Conn );
Try
{
Sqglad. Fill (DS, "student ");
Dataview view = Ds. Tables ["student"]. defaultview;
String sort = (string) viewstate ["sortorder"] + "" + (string) viewstate ["orderdire"];
View. Sort = sort;
Gridview1.datasource = view;
Gridview1.datakeynames = new string [] {"studentid"}; // identifies the primary key of the table, which will be used later
Gridview1.databind ();
Sqglad. Dispose (); // release the adapter Resource
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("failed to read data, error message:" + err. Message );
}
}
/// <Summary>
/// Row data deletion method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> E is the source that triggers the deletion event. </param>
Protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs E)
{
Sqlconnection conn = getconn ();
String commandtext = "delete student where studentid = '" + gridview1.datakeys [E. rowindex]. value. tostring () + "'";
Sqlcommand sqlcom = new sqlcommand (commandtext, Conn );
/*
* Exception handling, the same below: If the SQL statement is executed smoothly but the data bit is 0, an error is prompted; otherwise, an error message is printed.
*/
Try
{
Conn. open ();
Int I = sqlcom. executenonquery ();
If (I <= 0)
Response. Write ("<MCE: script language = JavaScript> <! --
Alert (creation failed! ')
// --> </MCE: SCRIPT> "); // The number of query results affected is 0.
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("deletion failed, error message:" + err. Message );
}
BIND ();
}
/// <Summary>
/// The row editing method. Only the gridview status is changed.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void gridviewinclurowediting (Object sender, gridviewediteventargs E)
{
Gridview1.editindex = E. neweditindex;
BIND ();
}
/// <Summary>
/// Cancel row editing
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs E)
{
Gridview1.editindex =-1;
BIND ();
}
/// <Summary>
/// Row data update Method
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> use e to obtain the new value of the current row. </param>
Protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs E)
{
String name = (textbox) gridview1.rows [E. rowindex]. cells [2]. controls [0]). Text. tostring (). Trim ();
String sex = (textbox) gridview1.rows [E. rowindex]. cells [3]. controls [0]). Text. tostring (). Trim ();
String telephone = (textbox) gridview1.rows [E. rowindex]. cells [4]. controls [0]). Text. tostring (). Trim ();
String comtext = "Update student set name = '" + name + "', sex = '" + sex + "', telephone = '"+ telephone +" 'where studentid =' "+ gridview1.datakeys [E. rowindex]. value. tostring () + "'";
Sqlconnection conn = getconn ();
Sqlcommand sqlcom = new sqlcommand (comtext, Conn );
Try
{
Conn. open ();
Int I = sqlcom. executenonquery ();
If (I <= 0) response. Write ("<MCE: script language = JavaScript> <! --
Alert (creation failed! ')
// --> </MCE: SCRIPT> ");
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("failed to update data, error message:" + err. Message );
}
Gridview1.editindex =-1;
BIND ();
}
/// <Summary>
/// Sorting method. If it is in ascending order, it is in descending order, and vice versa.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void gridviewinclusorting (Object sender, gridviewsorteventargs E)
{
String spage = E. sortexpression;
If (viewstate ["sortorder"]. tostring () = spage)
{
If (viewstate ["orderdire"]. tostring () = "DESC ")
Viewstate ["orderdire"] = "ASC ";
Else
Viewstate ["orderdire"] = "DESC ";
}
Else
{
Viewstate ["sortorder"] = E. sortexpression;
}
BIND ();
}
/// <Summary>
/// General method to obtain a database connection
/// </Summary>
/// <Returns> </returns>
Protected sqlconnection getconn ()
{
String connstring = "Data Source = MIRROR-PC // sqlexpress; initial catalog = manualdb; Integrated Security = true; pooling = false ";
Sqlconnection conn = new sqlconnection (connstring );
Return conn;
}
/// <Summary>
/// Add a data method. After adding a data method, the reset text box is blank.
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Protected void Add _ click (Object sender, eventargs E)
{
String commdstr = "insert into student (name, sex, telephone) values ('" + textbox4.text + "', '" + textbox2.text + "', '" + textbox3.text + "') ";
Sqlconnection conn = getconn ();
Sqlcommand sqlcom = new sqlcommand (commdstr, Conn );
Try
{
Conn. open ();
Int I = sqlcom. executenonquery ();
If (I <= 0) response. Write ("<MCE: script language = JavaScript> <! --
Alert (creation failed! ')
// --> </MCE: SCRIPT> ");
Conn. Close ();
}
Catch (exception ERR)
{
Response. Write ("creation failed, error message:" + err. Message );
}
BIND ();
Textbox3.text = ""; textbox4.text = ""; textbox2.text = "";
}
}