检查数据库上已经存在的记录. [英] put a check on the record already exits on the database.
本文介绍了检查数据库上已经存在的记录.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在数据库表中输入要检查的记录.如果再次输入数据库中已经存在的记录..我想在customer.aspx页面上显示错误..我该如何放置检查
i am entering records in the database table i want to make a check. if the record that is already exits in the database is entered again.. i want to show a error on customer.aspx page.. how can i put a check
推荐答案
您可以在后面的代码中尝试...尝试此代码..
You can try that in code behind...Try this code its work..
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(TextBox1.Text))
{
SqlConnection con;
SqlCommand cmd;
string constr = ConfigurationManager.ConnectionStrings["SomeDataBase"].ToString();
con = new SqlConnection(constr);
cmd = new SqlCommand("Select * from Thi where Username=@Username", con);
con.Open();
cmd.Parameters.AddWithValue("@Username", TextBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
checkusername.Visible = true;
imgstatus.ImageUrl = "Images/NotAvailable.jpg";
Label3.Text = "UserName Already Taken";
if (Label3.Text == "UserName Already Taken")
{
TextBox1.Text = " ";
}
System.Threading.Thread.Sleep(2000);
}
else
{
checkusername.Visible = true;
imgstatus.ImageUrl = "Images/Icon_Available.gif";
Label3.Text = "UserName Available";
System.Threading.Thread.Sleep(1000);
con.Close();
}
}
else
{
checkusername.Visible = false;
}
}
编码愉快.
Happy coding.
这篇关于检查数据库上已经存在的记录.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文