1. Purpose: To review the previous experiment-insert data into the database based on the data displayed in the database, and even display the updated data in the Gridview control. 2 procedures: 0. Use SqlServer to create a database named "ProductDB" and create a table Product (ID, ProductID, ProductDes) 1. Drag the GridView from the toolbar. There are two TextBox and one Button. For details, see the following figure. 1. Purpose: To review the previous experiment-insert data into the database based on the data displayed in the database, and even display the updated data in the Gridview control.
2 procedures: 0. Use SqlServer to create a database named "ProductDB" and create a table Product (ID, ProductID, ProductDes) 1. Drag the GridView from the toolbar. There are two TextBox and one Button. For details, see the following figure. 2. Pay attention to this step. Double-click the Button on the webpage to edit the code (to bind the event button#click) 3. Copy the corresponding code Original experiment interface: Final effect: Default. aspx File Content <% @ 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"> <Html xmlns = "http://www.w3.org/1999/xhtml"> <Head runat = "server"> <Title> No title page </title> </Head> <Body> <Form id = "form1" runat = "server"> <Div> <Asp: GridView ID = "GridView1" runat = "server"> </Asp: GridView> </Div> Item 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 = "button#click" Text = "Button"/> </Form> </Body> </Html> Default. aspx. cs File Content 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 button#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 (); } } |