Chapter 1 of asp.net-ado.net tutorial of Jilin University

Source: Internet
Author: User

The default. aspx has the following controls:
Text Box:
Id, name, pwd, pwd2
-------------------------
The radiobutton control is in the same group.
Nan, nv
--------------------------
Button:
Button1
------------------------
Three verification controls
One non-empty control is verified,
Verify that the database has two identical data controls: id and name.
Verify that the pwd and pwd2 characters are the same.
------------------------------------------------------------------
Grid datagrid Control
------------------------------------ Default. aspx. cs source file ---------------------------------------------------------------
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;

Namespace ado
{
/// <Summary>
/// _ Default abstract description.
/// </Summary>
Public class _ default: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. TextBox name;
Protected System. Web. UI. WebControls. TextBox pwd;
Protected System. Web. UI. WebControls. TextBox id;
Protected System. Web. UI. WebControls. RadioButton nan;
Protected System. Web. UI. WebControls. RadioButton nv;
Protected System. Web. UI. WebControls. Button Button1;
Protected System. Web. UI. WebControls. RequiredFieldValidator RequiredFieldValidator1;
Protected System. Web. UI. WebControls. CustomValidator CustomValidator2;
Protected System. Web. UI. WebControls. TextBox pwd2;
Protected System. Web. UI. WebControls. CompareValidator CompareValidator1;
Protected System. Web. UI. WebControls. DataGrid DataGrid1;
Protected System. Web. UI. WebControls. CustomValidator CustomValidator1;
 
Private void Page_Load (object sender, System. EventArgs e)
{
// Place user code here to initialize the page
This. filldb ();
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Button1.Click + = new System. EventHandler (this. button#click );
This. CustomValidator1.ServerValidate + = new System. Web. UI. WebControls. ServerValidateEventHandler (this. CustomValidator1_ServerValidate );
This. CustomValidator2.ServerValidate + = new System. Web. UI. WebControls. ServerValidateEventHandler (this. CustomValidator2_ServerValidate );
This. Load + = new System. EventHandler (this. Page_Load );

}
# Endregion

Private void CustomValidator1_ServerValidate (object source, System. Web. UI. WebControls. ServerValidateEventArgs args)
{
String pid = args. Value; // specify the pid Value.
If (db. fiedname (pid) // if the fiename returned by the db class is false, the value is true; otherwise, the value is false.
{
Args. IsValid = true;
}
Else
{
Args. IsValid = false;
}
}

Private void CustomValidator2_ServerValidate (object source, System. Web. UI. WebControls. ServerValidateEventArgs args)
{
String name = args. Value; // same as above. This is to restrict the second text box from inputting the same string
If (db. finname (name ))
{
Args. IsValid = true;
}
Else
{
Args. IsValid = false;
}
}
Public void filldb () // you can use this method to bind multiple pages.
{
This. DataGrid1.DataSource = db. selectAll (); // specifies the data source of the DataGrid1
This. DataGrid1.DataBind (); // bind
}
Private void button#click (object sender, System. EventArgs e) // insert data
{
Db p = new db ();
P. pid = this. id. Text;
P. pname = this. name. Text;
P. ppwd = this. pwd. Text;
If (this. nan. Checked) // if nan is selected
{
P. Male x = "male ";
}
Else
{
P. Category X = "female ";
}
If (db. insertcmd (p ))
{
Response. Write ("inserted successfully ");
This. filldb (); // call the binding upon successful insertion
}
Else
{
Response. Write ("failed" + e. ToString (); // if it fails, the captured error message is displayed.
}
}
}
}
-------------------------------------- Db. cs class source file ---------------------------------------------------
Using System;
Using System. Data;
Using System. Data. SqlClient;

Namespace ado
{
/// <Summary>
/// Summary of the database.
/// </Summary>
Public class db
{
// Define several attributes
Public string pid;
Public string pname;
Public string ppwd;
Public string comment X;
Public db ()
{
//
// TODO: add the constructor logic here
//
}
// Define the connection string
Public static SqlConnection createCon ()
{
SqlConnection con = new SqlConnection ("server = ..; database = usern; uid = sa; pwd = 980123 ;");
Return con;
}
Public static bool fiedname (string id) // you can check whether the same ID exists in the database.
{
SqlConnection con = db. createCon ();
Con. Open ();
SqlCommand cmd = new SqlCommand ("select count (*) from proe where uid = '" + id + "'", con );
Int count = Convert. ToInt32 (cmd. ExecuteScalar (); // return the first column of the First row
If (count> 0)
{
Return false;
}
Else
{
Return true;
}
}
Public static bool finname (string name) // same as above
{
SqlConnection con = db. createCon ();
Con. Open ();
SqlCommand cmd1 = new SqlCommand ("select count (*) from proe where uname = '" + name + "'", con );
Int count = Convert. ToInt32 (255.1.executescalar ());
If (count> 0)
{
Return false;
}
Else
{
Return true;
}
}
Public static bool insertcmd (db p) // insert operation
{
Try // judge. If the insert operation is successful, the result is true.
{
SqlConnection con = db. createCon ();
Con. Open ();
SqlCommand cmdi = new SqlCommand ("insert into proe values (@ pid, @ pname, @ ppwd, @ psex)", con );
SqlParameter para = new SqlParameter ("@ pid", SqlDbType. VarChar, 10 );
Para. Value = p. pid;
Cmdi. Parameters. Add (para );
Para = new SqlParameter ("@ pname", SqlDbType. VarChar, 50 );
Para. Value = p. pname;
Cmdi. Parameters. Add (para );
Para = new SqlParameter ("@ ppwd", SqlDbType. VarChar, 10 );
Para. Value = p. ppwd;
Cmdi. Parameters. Add (para );
Para = new SqlParameter ("@ psex", SqlDbType. VarChar, 2); // instantiate SqlParameter ("command object", data type, data size)
Para. Value = p. Returns X; // returns the Value of p. Returns X.
Cmdi. Parameters. Add (para); // Add p. Parameters X to the para set.
Cmdi. ExecuteNonQuery (); // execute the insert operation
Con. Close (); // Close the connection
Return true;
}
Catch (Exception e) // otherwise false is returned and an error is caught
{
Return false;
}
}
Public static DataTable selectAll () // declare a static retrieval object
{
SqlConnection con = db. createCon (); // specify the connection. You do not need to open the connection below. If the connection is not enabled, the following statement has a key and will open it on its own.
SqlDataAdapter sda = new SqlDataAdapter (); // create a data adapter object
Sda. SelectCommand = new SqlCommand ("select * from proe", con); // instantiate the SelectCommand object for data retrieval
DataSet ds = new DataSet (); // specify a DataSet
Sda. Fill (ds, "proe"); // fill the local virtual table with Fill
Return ds. Tables ["proe"]; // return to the page
}
}
}
 

---------------------------------------

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.