如何默认插入用户(客户端)条目?



我有

管理端和客户端

我只有一个用于管理员登录和客户端登录的表,请参阅参考图像

当用户登录时,然后以用户身份插入条目?

参考:

https://i.stack.imgur.com/BqEu0.jpg

我想例如:

数据库条目

用户类型: 用户
电子邮件 ID: benssok@gmail.com
密码: bens1234
名字: 尼克斯
姓氏: 安德鲁

法典:

C# 注册客户端:

protected void Button1_Click(object sender, EventArgs e)
{
string firstname = txtFirstName.Text;
string lastname = txtLastName.Text;
string emailid = txtEmailId.Text;
string password = txtclientpassword.Text;
ClientLogin_Click(firstname, lastname, emailid, password);
}`enter code here`
void ClientLogin_Click(string firstname,string lastname,string emailid,string Password)
{
string conn = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection cn = new SqlConnection(conn);
string Insertquery = "Insert into tbladminclient(FirstName,LastName,EmailId,Password) values(@FirstName,@LastName,@EmailId,@Password)";
SqlCommand cmd = new SqlCommand(Insertquery, cn);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@FirstName", firstname);
cmd.Parameters.AddWithValue("@LastName", lastname);
cmd.Parameters.AddWithValue("@EmailId", emailid);
cmd.Parameters.AddWithValue("@Password", Password);
try
{
cn.Open();
int validateOperation = cmd.ExecuteNonQuery();
if (validateOperation > 0)
{
Response.Write("successfully Registration");
Response.Redirect("ClientLogin.aspx");
}
else
{
Response.Write("Not successfully Registration");
}
}
catch (SqlException e)
{
Response.Write("error");
}
finally
{
cn.Close();
}
}

AdminLoginpage//Problem 發生時相同的登入 (AdminSide( 和 sameLogin(ClientSide( 有什么区别?如何解决这个问题?如何识别用户(客户端(和管理员(管理员端(登录??

protected void Button1_Click(object sender, EventArgs e)
{
string userName = txtEmailId.Text;
string Password = txtUserPassword.Text;
Login_Click(userName, Password);
}
void Login_Click(string emailid, string Password)
{
string conn = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from tbladminclient where EmailId=@EmailId and Password=@Password", cn);
cn.Open();
cmd.Parameters.AddWithValue("@EmailId", emailid);
cmd.Parameters.AddWithValue("@Password", Password);
SqlDataReader dr = cmd.ExecuteReader(); //data read from the database 
if (dr.HasRows == true)   //HasRows means one or more row read from the database
{
Response.Write("successfully Login");
}
else
{
Response.Write("Not successfully Login");
}
cn.Close();
}

问题是当相同的Login(AdminSide)sameLogin(ClientSide)有什么区别?如何解决这个问题?如何识别用户(客户端(和管理员(管理员端(登录??

首先,当用户(客户端(注册表填写时,转到数据库并执行查询

ALTER TABLE [tbladminclient]
ADD CONSTRAINT df_UserType
DEFAULT 'User' FOR UserType; //whenever you insert then Bydefault Entry is User: 

如何识别用户是客户端还是管理员

法典: 用户登录

protected void Button1_Click(object sender, EventArgs e)
{
string emailid = txtemailId.Text;
string password = txtPassword.Text;
Client_Login(emailid,password);
}
void Client_Login(string emailid,string password)
{
string conn = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from tbladminclient where EmailId=@EmailId and Password=@Password", cn);
cn.Open();
cmd.Parameters.AddWithValue("@EmailId", emailid);
cmd.Parameters.AddWithValue("@Password", password);
SqlDataReader dr = cmd.ExecuteReader(); //data read from the database 
if (dr.HasRows == true)//HasRows means one or more row read from the database
{
if (dr.Read())
{
if (dr["UserType"].ToString() == "User")
{
Response.Write("successfully Client Login");
}
else
{
Response.Write("Not successfully Client Login");
}
}
}
else
{
Response.Write("Not Found");
}
cn.Close();
}

管理员登录

protected void Button1_Click(object sender, EventArgs e)
{
string userName = txtEmailId.Text;
string Password = txtUserPassword.Text;
Login_Click(userName, Password);
}
void Login_Click(string emailid, string Password/*string UserType*/)
{
string conn = ConfigurationManager.ConnectionStrings["connstr"].ToString();
SqlConnection cn = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand("select * from tbladminclient where EmailId=@EmailId and Password=@Password", cn);
cn.Open();
cmd.Parameters.AddWithValue("@EmailId", emailid);
cmd.Parameters.AddWithValue("@Password", Password);
SqlDataReader dr = cmd.ExecuteReader(); //data read from the database 
if (dr.HasRows == true)//HasRows means one or more row read from the database
{
if (dr.Read())
{
if (dr["UserType"].ToString() == "admin")
{
Response.Write("Successfully Admin Login");
}
else
{
Response.Write("Not successfully Admin Login");
}
}
}
else
{
Response.Write("Not Found");
}
cn.Close();
}

最新更新