Asp. NET News System (add news type)

Source: Internet
Author: User

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)

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.