Inserting data instances into a database in asp.net

Source: Internet
Author: User
The code is as follows Copy Code

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

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.