简体   繁体   中英

How to link separated class for database operations in website in ASP.net with the pages.aspx

Login Page: when the user logs in I should check if password and user name is true or not through checking the table records in database. Because I'm working using oop concept I created a separate class for DB operations but I face a big problem that the text boxes in the Login.aspx can't be seen in database class. The with Registration.aspx I want to insert data of the new user but I can't see the textboxes to take the strings inside them to add in the database any help or any way to link those classes together.

here's my data Base class code

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using 

System.Data.SqlClient; using System.Configuration; using System.Data.Sql; using System.Data; using System.Web.UI.WebControls; namespace Registration { };

/// /// Summary description for DataBase /// /// //namespace Login.aspx { }; public class DataBase { SqlDataReader rdr = null; public SqlCommand cmd_insert; public String USer=""; public String Pass="";

 SqlConnection conn = null; Login log = new Login(); public void Read_record() { try { //string ID = Request.QueryString["id"]; conn = new SqlConnection("Data Source=SHIMOFCIS-PC\\\\MYSQL;Initial Catalog=WebSite;Integrated 

Security=SSPI");

  SqlCommand cmd; conn.Open(); cmd = new SqlCommand("select UserName,Password from Users ", conn); rdr = cmd.ExecuteReader(); //using (var reader = cmd.ExecuteReader()) //{ if (rdr.Read()) // you don't need while loop { USer = rdr["UserName"].ToString(); Pass = rdr["Password"].ToString(); if (USer == log.UserName && Pass == log.Password) { rdr.Close(); conn.Close(); } } //} } finally { // close the reader if (rdr != null) { rdr.Close(); } // 5. Close the connection if (conn != null) { conn.Close(); } } } public void Insert_rows() { conn = new SqlConnection("Data Source=SHIMOFCIS-PC\\\\MYSQL;Initial Catalog=WebSite;Integrated 

Security=SSPI");

  conn.Open(); cmd_insert = new SqlCommand("INSERT INTO Users (UserName,Password,FullName,Address,Mobile,Email) VALUES (@value1 , 

@value2 , @value3 , @value4 , @value5 , @value6 , @value7)", conn);

 } } 

and this alogin.aspx code

` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

 } protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) { DataBase db = new DataBase(); db.Read_record(); if (db.USer == Login1.UserName && db.Pass == Login1.Password) { Response.Redirect("~/Home.aspx?UserName=" + Login1.UserName); } } }` 

and in regestration.aspx i couldn't use create user control beacuse i have to do specific fields to fill in so i couldn't depend on it to solve the problem of not seein each like i do in login and it although not working quiet well

I would highly suggest using an MVP pattern to separate the code, rather than a database class. The database class won't be able to do it directly. You can use a framework like Nucleo MVP or WebFormsMvp . For more on the MVP pattern, check out Dino Esposito's article .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM