Insert a data instance to the database in asp.net

Source: Internet
Author: User

This article introduces how to insert data to a database. Even if you want to display the updated data in the Gridview control, you can refer to this article.

The Code is as follows: Copy code

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 ();
}
}

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.