| 1 experimental purposes: review of the last experiment-to insert data into the database based on the data displayed in the database, and even to display the updated data in the GridView control. 2 Procedural steps: 0, create a database named "Productdb" with SQL Server, new table product (Id,productid,productdes) 1, drag the GridView from the toolbar, two textbox, a button, look at the picture below 1 experimental purposes: review of the last experiment-to insert data into the database based on the data displayed in the database, and even to display the updated data in the GridView control. 2 Procedural steps: 0, create a database named "Productdb" with SQL Server, new table product (Id,productid,productdes) 1, drag the GridView from the toolbar, two textbox, a button, look at the picture below 2, note this step, double-click the button on the page to enter the Code Editor (to bind the event button1_click) 3, copy the corresponding code The original interface of the experiment: Final effect: Default.aspx file contents <%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <title> Untitled Page </title> <body> <form id= "Form1" runat= "Server" > <div> <asp:gridview id= "GridView1" runat= "Server" > </asp:GridView> </div> Product Id:<asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox><br/> Product Description: <asp:textbox id= "TextBox2" runat= "Server" ></asp:textbox><br/> <asp:button id= "Button1" runat= "Server" onclick= "Button1_Click" text= "button"/> </form> </body>
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; public partial class _default:system.web.ui.page { String constr = "server=.; Database=productdb; uid=sa;pwd=; "; protected void Page_Load (object sender, EventArgs e) { if (! IsPostBack) { Bind (); } } public void Bind () { String sqlstr = "SELECT * from Product"; SqlConnection sqlconn = new SqlConnection (CONSTR); SqlDataAdapter SDA = new SqlDataAdapter (Sqlstr, sqlconn); DataSet ds = new DataSet (); Sda. Fill (DS); This. Gridview1.datasource = ds; This. Gridview1.databind (); Sqlconn.close (); } protected void Button1_Click (object sender, EventArgs e) { String sqlstr = "INSERT into Product (productid,productdes) VALUES (' +textbox1.text.trim () +" ', ' "+textbox2.text.trim () +"')"; SqlConnection sqlconn = new SqlConnection (CONSTR); Sqlconn.open (); SqlCommand SQLCMD = new SqlCommand (sqlstr, sqlconn); Sqlcmd.executenonquery (); Sqlconn.close (); This. Bind (); } } |