如何在注册页面上防止重复用户? [英] How to prevent duplicate user on signup page ?
本文介绍了如何在注册页面上防止重复用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2页登录和注册。我想检查用户是否已经存在。
我尝试过:
i have 2 pages login and signup. and I want to check whether the user is already exist or not.
What I have tried:
//this is my code in registration form
public partial class Login : System.Web.UI.Page
{
string cs = ConfigurationManager.ConnectionStrings["NDBCS"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadTrack();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(cs);
con.Open();
SqlCommand cmd = new SqlCommand("Insert into tbl_User (Firstname, Lastname, Track_id, Username, Email, Password) values (@firstname, @lastname, @track, @username, @email, @password)", con);
cmd.Parameters.AddWithValue("@firstname", txtbox_FN.Text);
cmd.Parameters.AddWithValue("@lastname", txtbox_LN.Text);
cmd.Parameters.AddWithValue("@track", ddl_Track.SelectedValue);
cmd.Parameters.AddWithValue("@username", txtbox_UN.Text);
cmd.Parameters.AddWithValue("@email", txtbox_Email.Text);
cmd.Parameters.AddWithValue("@password", txtbox_PW.Text);
cmd.ExecuteNonQuery();
con.Close();
}
private void LoadTrack()
{
try
{
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("select * from tbl_Track", con);
con.Open();
DataTable table = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(table);
ddl_Track.DataSource = table;
ddl_Track.DataValueField = "Track_id";
ddl_Track.DataTextField = "Track_code";
ddl_Track.DataBind();
ddl_Track.Items.Insert(0, new ListItem("--Select Category--", "0"));
}
}
catch (Exception ex)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Something went wrong!." + ex.Message + "";
Label1.Visible = false;
}
}
推荐答案
检查电子邮件:添加 SQL UNIQUE Constraint [ ^ ]到列(并使用try ... catch
块来处理现有用户)或首先执行SELECT,然后查看是否在那。
我?我会做两个......
但更重要的是,不要这样做: CodeCrime:基于文本的密码 [ ^ ]
请参阅:密码存储:如何操作。 [ ^ ]
Check the email: Either add a SQL UNIQUE Constraint[^] to the column (and use atry...catch
block to handle existing users) or do a SELECT first, and see if it's there.
Me? I'd do both...
But more importantly, don't do it like that: CodeCrime: text based passwords[^]
See here: Password Storage: How to do it.[^]
简单检查用户名和电子邮件是否已经退出数据库..
Simple you check UserName And Email already exits or not in your Database ..
这篇关于如何在注册页面上防止重复用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文