I've been learning Webfrom recently. First, make a news system, practice practiced hand.
TextBox controls and Button buttons controls
Receive the TextBox content and click the Add button to add to the database.
Establishing a SQL Server database
Two databases were built in a nutshell.
1/Content Library
2/Type library
After the database is established, open the configuration file in the project root directory.
<appsettings><add key= "ConnStr" value= "Server=desktop-td0v8nd;uid=sa;pwd=123456;database=appworld"/> </appSettings>
Add the configuration information for the linked database-appworld for my database name, desktop-td0v8nd for my Windows logon method name, connstr for my database connection variable, which will be written behind.
Double-click the button to confirm the Add buttons and enter the Write code:
Add news type protected void Btuadd_click (object sender, EventArgs e) {//Insert SQL statement String sql = "INSERT INTO Type_info (Typen AME) VALUES (' "+this.txtnewname.text+" ') "; Create SqlCommand object SqlCommand cmd = new SqlCommand (sql,conn); Open the database Conn. Open (); Receive Insert data return value int i = cmd. ExecuteNonQuery (); Close the database Conn. Close (); Determines whether the data is added successfully if (i > 0) {this . Page.registerstartupscript ("", "<script>alert (' Add success '); </script> "); This.txtNewName.Text = null; Shownewtype ();//Load News type method----This method is written later }}
It is now possible to complete the addition of the news type. Here's how to start showing the added data
Use the GridView control and drag it in.
Home with the GridView Display news type---This is the method above void Shownewtype () {//1. read data String sql = "SELECT * from Type_info"; 2. Data compatibility SqlDataAdapter da = new SqlDataAdapter (SQL, conn); 3.dataset Object DataSet ds = new DataSet (); 4. Fill in the data into DA. Fill (DS); 5. Determine if the data is populated successfully, greater than 0 have data if (ds. Tables[0]. Rows.Count > 0) {gridview1.datasource = ds. Tables[0]; Gridview1.databind (); }}
Put this method, write to the Load event, the data can already be displayed.
form loaded protected void Page_Load (object sender, EventArgs e) {shownewtype ();//Load News type}
Asp. NET News System (add news type)