C # simple address book for Asp.net

Source: Internet
Author: User

Simple address book features are simple, but include the basic functions for creating a website! Each module can be used as a reference for beginners.


Simple Address Book implementation functions: 1. logon 2. Registration 3. Background Management 4. Foreground logon 5. Create a contact 6. Change the password

Code download: http://download.csdn.net/detail/wyz365889/5773253

The functions are as follows:













The main code is as follows:

1. Underlying data module

Using System; using System. data; using System. configuration; 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; using System. data. sqlClient; using System. data; // <summary> /// DBManage abstract description /// </summary> public class DBManage {public DBManage () {// TODO: here add the constructor logic //} string strConn = @ "Data Source = WYZ-PC \ SQL2005; Integrated Security = SSPI; Initial Catalog = addressBook;"; public SqlConnection conn; public void sqlConn () // connect to the database {try {conn = new SqlConnection (strConn); conn. open () ;}catch {return ;}// read the statement execution result public SqlDataReader readResult (string strSql) {try {SqlCommand sqlComd = new SqlCommand (strSql, conn ); return sqlComd. executeReader () ;}catch {throw ;}// read data to the public bool readData (string strSql, out DataTable dt) {dt = new DataTable (); try {SqlDataAdapter sda = new SqlDataAdapter (strSql, conn); sda. fill (dt); return true;} catch (Exception e) {return false ;}// execute insert, update the statement public bool execSql (string strSql) {SqlCommand SC = new SqlCommand (strSql, conn); try {SC. executeNonQuery (); return true;} catch (Exception e) {return false ;}// query whether data exists in public bool isExistData (string strSql) {bool flag = false; try {using (SqlCommand SC = new SqlCommand () {SC. commandText = strSql; SC. connection = conn; SqlDataReader sr = SC. executeReader (); if (sr. hasRows) {flag = true;} sr. close () ;}} catch (Exception e) {flag = false;} return flag;} public void closeDB () {conn. close (); conn. dispose ();}}

2. logon code

Using system; using system. data; using system. configuration; using system. collections; 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; using system. data. sqlclient; public partial class login: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void Button_login_click (Object sender, eventargs e) {dbmanage DB = new dbmanage (); dB. sqlconn (); // database connection string struserid = textbox?userid.text.trim (); string strpwd = textbox2_pwd.text.trim (); If (struserid = "| strpwd = "") {labeldomainmeg.text = "prompt: the user name or password cannot be blank! ";} Else {string strsql = @" select * From tb_user "; if (! DB. isexistdata (strsql) // if no user exists, add the Default User {db.exe csql (@ "insert into tb_user values ('admin', 'admin ') ");} string strsql2 =" select * From tb_user where userid = '"+ struserid +"' and Pwd = '"+ strpwd +"' "; if (dB. isexistdata (strsql2) {sqldatareader sqlread = dB. readresult (strsql2); sqlread. read (); string strrole = sqlread [3]. tostring (); sqlread. close (); dB. closedb (); // closes the database if (strrol E. Trim (). Equals ("Administrator") // administrator permission {response. Redirect ("Admin. aspx? Userid = "+ struserid);} else if (strrole. Trim () =" normal user ") // normal user permission {response. Redirect (" userinfo. aspx? Userid = "+ struserid) ;}} else {label?meg.text =" prompt: the password or account is incorrect. Please enter it again! "; Textbox=userid.text =" "; textbox2_pwd.text = "";}}}}

3. Registration Code

Using System; using System. data; using System. configuration; using System. collections; 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; public partial class register: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} DBManage db; protected void Button_reg_C Lick (object sender, EventArgs e) {db = new DBManage (); db. sqlConn (); string strUserID = textbox?userid.text.trim (); string strName = TextBox2_name.Text.Trim (); string strPwd = trim (); string strPwd2 = trim (); string strRole = "regular user "; string strSql2 = "insert into tb_user values ('" + strUserID + "', '" + strName + "', '" + strPwd + "', '"+ strRole +"') "; if (d B .exe cSql (strSql2) {Response. Write ("<script> alert ('registration successful! '); Window. location. href = 'login. aspx' </script> "); return ;}}}

4. Background Management Code

Using System; using System. data; using System. configuration; using System. collections; 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; using System. data. sqlClient; public partial class admin: System. web. UI. page {DBManage db; string strUserID; protected void Page_Load (object sender, E VentArgs e) {if (! Page. IsPostBack) {MyBind ("select * from tb_user"); strUserID = Request. QueryString ["userID"]. ToString (); HyperLink1_pwd.NavigateUrl = "~ /Pwd. aspx? UserID = "+ strUserID;} void MyBind (String strSql) {db = new DBManage (); db. sqlConn (); SqlDataAdapter da = new SqlDataAdapter (strSql, db. conn); DataSet ds = new DataSet (); da. fill (ds); GridView1.DataSource = ds; GridView1.DataBind ();} protected void button#click (object sender, EventArgs e) {string strKey = textbox#key.text.trim (); string strID = ""; if (dropdownlistdomainselect.text = "userName") {strID = "userID";} if (dropdownlistdomainselect.text = "name") {strID = "userName ";} if (dropdownlistpolicselect.text = "") {strID = "role ";} string strSql = "select * from tb_user where" + strID + "like '" + strKey + "%'"; MyBind (strSql);} protected void GridView1_RowDeleting (object sender, gridViewDeleteEventArgs e) {db = new DBManage (); db. sqlConn (); string SQL = "delete from tb_user where userID = '" + GridView1.DataKeys [e. rowIndex]. value. toString () + "'"; SqlCommand cmd = new SqlCommand (SQL, db. conn); cmd. executeNonQuery (); db. closeDB (); MyBind ("select * from tb_user"); // call the MyBind () subroutine }}

5. Front-end login display

Using system; using system. data; using system. configuration; using system. collections; 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; using system. data. sqlclient; public partial class userinfo: system. web. UI. page {dbmanage dB; string struserid = ""; protected void page_load (Object Sen Der, eventargs e) {If (! Page. ispostback) {mybind ("select * From tb_info"); struserid = request. querystring ["userid"]. tostring (); hyperlink1_pwd.navigateurl = "~ /Pwd. aspx? Userid = "+ struserid; hyperlink1_new.navigateurl = "~ /Newinfo. aspx? Userid = "+ struserid;} void mybind (string strsql) {DB = new dbmanage (); dB. sqlconn (); sqldataadapter da = new sqldataadapter (strsql, DB. conn); dataset DS = new dataset (); DA. fill (DS); gridview1.datasource = Ds. tables [0]. defaultview; gridview1.databind ();} protected void gridview1_rowdeleting (Object sender, gridviewdeleteeventargs e) {string SQL = "delete from tb_info where num =" + gridview1.datakeys [E. rowindex]. values [0]. tostring () + "and userid = '" + gridview1.datakeys [E. rowindex]. values [1]. tostring () + "'"; DB = new dbmanage (); dB. sqlconn (); sqlcommand cmd = new sqlcommand (SQL, DB. conn); // execute the delete operation cmd. executenonquery (); dB. closedb (); mybind ("select * From tb_info"); // call mybind () subroutine} protected void gridview1_rowediting (Object sender, gridviewediteventargs e) {gridview1.editindex = E. neweditindex; string strsql = "select * From tb_info where num =" + gridview1.datakeys [E. neweditindex]. values [0]. tostring () + "and userid = '" + gridview1.datakeys [E. neweditindex]. values [1]. tostring () + "'"; mybind (strsql);} protected void gridview1_rowcancelingedit (Object sender, gridviewcancelediteventargs e) {gridview1.editindex =-1; mybind ("select * From tb_info");} protected void gridview1_rowupdating (Object sender, gridviewupdateeventargs e) {DB = new dbmanage (); dB. sqlconn (); textbox name, sex, phone, QQ, birthday, remark; name = (textbox) gridview1.rows [E. rowindex]. cells [0]. controls [0]; Sex = (textbox) gridview1.rows [E. rowindex]. cells [1]. controls [0]; phone = (textbox) gridview1.rows [E. rowindex]. cells [2]. controls [0]; QQ = (textbox) gridview1.rows [E. rowindex]. cells [3]. controls [0]; Birthday = (textbox) gridview1.rows [E. rowindex]. cells [4]. controls [0]; remark = (textbox) gridview1.rows [E. rowindex]. cells [5]. controls [0]; string strsql = "Update tb_info set name = '" + name. text + "', sex ='" + sex. text + "', phone ='" + phone. text + "', QQ ='" + QQ. text + "', Birthday =" + birthday. text. substring (0, 9) + ", remark = '" + remark. text + "'where num =" + gridview1.datakeys [E. rowindex]. values [0]. tostring () + "and userid = '" + gridview1.datakeys [E. rowindex]. values [1]. tostring () + "'"; db.exe csql (strsql); gridview1.editindex =-1; mybind ("select * From tb_info"); // call mybind () subroutine} protected void button#click (Object sender, eventargs e) {string strkey = textbox2.text. trim (); string strsql = "select * From tb_info where name like '" + strkey + "%' or phone like '" + strkey + "%' or QQ like '" + strkey + "% '"; mybind (strsql );}}

6. Change the password

Using System; using System. data; using System. configuration; using System. collections; 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; public partial class pwd: System. web. UI. page {string strUserID; DBManage db; protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {strUserID = Request. queryString ["userID"]. toString (); textbox?userid.text = strUserID; textbox=userid.enabled = false;} protected void Button_pwd_Click (object sender, EventArgs e) {db = new DBManage (); db. sqlConn (); string strNewPwd = TextBox4_pwd1.Text.Trim Xt. toString () + "'"; Label1.Text = textbox#userid.text + db. isExistData (strSql ). toString () + strNewPwd. equals (strRPwd ). toString (); if (db. isExistData (strSql) {if (strNewPwd. equals (strRPwd) {string strSql2 = "update tb_user set pwd = '" + strNewPwd + "'where userID ='" + textbox#userid.text.tostring () + "'"; if (db.exe cSql (strSql2) {Label1.Text = "the password is modified successfully! ";}} Else {Label1.Text =" the password entered twice is different! ";}} Db. closeDB ();}}

7. Create a contact

Using System; using System. data; using System. configuration; using System. collections; 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; using System. data. sqlClient; public partial class newInfo: System. web. UI. page {DBManage db; string strUserID; protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack) {textbox?birthday.enabled = false; strUserID = Request. QueryString ["userID"]. ToString (); hyperlink?back.navigateurl = "~ /UserInfo. aspx? UserID = "+ strUserID;} else {strUserID = Request. queryString ["userID"]. toString () ;}} protected void calendar#selectionchanged (object sender, EventArgs e) {textbox#birthday.text = Calendar1.SelectedDate. toString ("yyyy-MM-dd");} protected void Button_reg_Click (object sender, EventArgs e) {db = new DBManage (); db. sqlConn (); string strName = TextBox2_name.Text.Trim (); string strPhone = TextBox Trim (); string strQQ = trim (); string strRemark = TextBox3_remark.Text; string strSex = ""; if (RadioButton1.Checked) {strSex = "male" ;}if (RadioButton2.Checked) {strSex = "female";} string strBir = textbox?birthday.text; string strSql = "select * from tb_info where name = '" + strName + "' or phone = '" + strPhone + "'"; if (db. isExistData (strSql) {Label1.Text = "This information already exists! "; Return;} string strSql2 =" insert into tb_info values ('"+ strUserID +"', '"+ strName +"', '"+ strSex + "', '"+ strPhone +"', '"+ strQQ +"', '"+ strBir +"', '"+ strRemark +"') "; Label1.Text = strSql2; if (db.exe cSql (strSql2) {Label1.Text = "the contact is created successfully! "; Return ;}}}

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.