Asp. NET Learning Note 2--yourself write code binding GridView

Source: Internet
Author: User

As before, write your own style layout first,

The second step is to create a commodity table in the database

The code is as follows:

CREATE TABLE [Shangping_info] ([shangping_id] INT PRIMARY KEY IDENTITY, [Shangping_name] CHAR ( -) not NULL, [shangping_content] CHAR ( -) not NULL, [Add_time] DATETIME) inserts into [Vip_user] VALUES ('Tangxuelong','[email protected]'INSERT into [Shangping_info] VALUES ('airconditioner','Air Conditioning', the-5- -)

The third step, start writing the background code binding data,

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingSystem.Data.SqlClient;usingDAL;namespacemystore{ Public Partial classMain:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) {DBHelper db=NewDBHelper ();//instantiation ofSqlConnection conn = db. Getcoon ();//Create a Connection object            if(Conn. state = = ConnectionState.Open)//determine if the database is open{Conn.            Close (); } conn.                   Open (); //Open Database            stringsql ="select * FROM [Shangping_info]";//Query StatementsSqlDataAdapter da =NewSqlDataAdapter (Sql,conn);//querying data using DataAdapterDataSet ds =NewDataSet ();//Create a DataSet objectDa. Fill (DS);//populating data read by DataAdapter into a datasetGridview_shangping. DataSource = ds;//bind the data source of the GridView to a datasetgridview_shangping.            DataBind (); Conn.            Close (); Da.                    Dispose (); }    }}

F5 ran up to see the data.

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.