ASP. NET entry data

Source: Internet
Author: User

For beginners of website programming, they will always find some source code on the Internet, but over time they will stay at the code change stage and do not understand how to write a complete website program. I started to write such an article (c #) As I can see it. Please criticize and correct the shortcomings.

Database Connection

View the WEB. config configuration file in the Web project. Add the following code in the configuration line to connect to the SQL server.

<Deleetask>
<! -- Database connection string -->
<Add key = "ConnStr" value = "Data Source = localhost; database = company; UID = sa; Password =; Persist Security Info = True;"/>
</AppSettings>

Data list display,

Using System;
Using System. Data;
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;

// Reference namespace: SQL hosting, configuration file
Using System. Data. SqlClient;
Using System. Configuration;


Public partial class _ Default: System. Web. UI. Page
{
Protected SqlConnection myconn = new SqlConnection (ConfigurationSettings. etettings ["ConnectionString"]);
// Read the database connection string in the web. config configuration file and connect to the specified database

Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack) // determine whether the page is running for the first time
{

String strsql = "select * from Product"; // defines the query string of a database.
DataSet ds = new DataSet ();
Myconn. Open (); // Open the database connection
SqlDataAdapter command = new SqlDataAdapter (strsql, myconn); // indicates a set of data commands used to fill the DataSet and update the SQL Server database and connect to a database
Command. Fill (ds, "Product ");
ProductList. DataSource = ds. Tables [0]. DefaultView;
ProductList. DataBind ();
Ds. Clear ();
Myconn. Close (); // Close the database connection
}
}

Protected void grid_ItemDataBound (object sender, DataGridItemEventArgs e)
{
Foreach (System. Web. UI. WebControls. HyperLink link in e. Item. Cells [7]. Controls)
{
Link. Attributes. Add ("onClick", "if (! Window. confirm ('Do you really want to delete this record? ') {Return false ;}");
}
}

}

Add data

Protected void btnAdd_Click (object sender, EventArgs e)
{
String ProductId = this.txt ProductId. Text;
String CategoryId = this.txt CategoryId. Text;
String Name = this.txt Name. Text;
String Description = this.txt Description. Text;
String Price limit this.txt Price. Text;

String SQL _Exeits = "select * from Product where ProductId = '" + ProductId + "'";
SqlCommand pai_exeits = new SqlCommand (SQL _Exeits, myconn );
Myconn. Open ();
SqlDataReader rdr = pai_exeits.executereader ();
While (rdr. Read ())
{
Response. Write ("<script language = 'javascript '> ");
Response. Write ("alert ('Sorry, this product number already exists! ')");
Response. Write ("</script> ");
This.txt CategoryId. Text = "";
This.txt Description. Text = "";
This.txt Name. Text = "";
This.txt Price. Text = "";
This.txt ProductId. Text = "";
Return;
}
Rdr. Close ();


String SQL _add = "insert into Product (ProductId, CategoryId, Name, Description, Price) values ('" + ProductId + "', '" + CategoryId + "', '"+ Name +"', '"+ Description +"', '"+ Price + "')";
SqlCommand cmd_add = new SqlCommand (SQL _add, myconn); // SqlCommand: A Transact-SQL statement or stored procedure to be executed on the SQL Server database
Pai_add.executenonquery (); // execute the Transact-SQL statement on the connection and return the affected number of rows. For UPDATE, INSERT, and DELETE statements, the returned value is the number of rows affected by the command. For all other types of statements, the return value is-1. If a rollback occurs, the returned value is-1.
Myconn. Dispose ();
Myconn. Close ();
}
[/CODE


[COLOR = Red] data display [/COLOR]
[CODE]
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
String id = Request. Params ["id"];
If (id = null | id. Trim () = "")
{
Response. Redirect ("default. aspx ");
Response. End ();
}
Else
{
String SQL _show = "select * from Product Where ProductId =" + id;
SqlCommand cmd_show = new SqlCommand (SQL _show, conn );
Conn. Open ();
SqlDataReader rd_show = pai_show.executereader (); // use the SqlDataReader object to read and return a record set
Shows. DataSource = rd_show; // point to the data source
Shows. DataBind (); // bind data
Rd_show.Close (); // close SqlDataReader
}
}
}

Data Modification

Protected void btnAdd_Click (object sender, EventArgs e)
{
String ProductId = this. lblProductId. Text;
String CategoryId = this.txt CategoryId. Text;
String Name = this.txt Name. Text;
String Description = this.txt Description. Text;
Decimal Price = decimal.Parse(this.txt Price. Text );

String SQL _edit = "update Product set CategoryId = '" + CategoryId + "', Name = '" + Name + "', Description = '" + Description + "', price = '"+ Price +"' where ProductId = "+ ProductId;
SqlCommand cmd_edit = new SqlCommand (SQL _edit, conn );

Conn. Open ();
Pai_edit.executenonquery ();
Conn. Close ();
Response. Write ("<script language = javascript> window. alert ('saved successfully! ') </Script> ");
Response. Redirect ("show. aspx? Id = "+ ProductId );
}

Data deletion

Protected void Page_Load (object sender, EventArgs e)
{
If (! Page. IsPostBack)
{
String ProductId = Request. Params ["id"];
String SQL _del = "delete from Product where ProductId =" + ProductId;
SqlCommand cmd_del = new SqlCommand (SQL _del, conn );
Conn. Open ();
Performance_del.executenonquery ();
Conn. Close ();
Response. Redirect ("default. aspx ");
}
}

 

Download example

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.