从gridview页脚文本框添加时,不要在数据库中插入重复值 [英] donot insert duplicate values in database when added from gridview footer textboxes
问题描述
我正在使用asp.net c#sqlserver 2005数据库。
在我的网页上我有一个gridview,在这个gridview我有4个字段(用户名,密码) ,电子邮件,指定)和页脚中的4个文本框输入记录。和添加按钮以添加记录。
如果记录已存在于数据库中,则显示消息用户名已存在。
先谢谢。
这是我在gridview的行命令中的代码。
I am working on asp.net c# sqlserver 2005 database.
on my webpage i have a gridview, in this gridview i have 4 fields (Username,Password, email,designation) and 4 textboxes in footer to enter the records. and a add button to add the record.
if the record already exists in database, display a message Username already exists.
Thanks in advance.
This is my code in row command in gridview.
protected void GVRegistrations_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew Registrations"))
{
TextBox TxtUsername = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterUsername");
TextBox TxtPassword = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterPassword");
TextBox TxtEmail = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterEmail");
TextBox TxtDesignation = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFooterDesignation");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT into ACRegistrations(Username,Password,Email,Designation) values('" + TxtUsername.Text + "','" + TxtPassword.Text + "','" + TxtEmail.Text + "','" + TxtDesignation.Text + "')", con);
int result = cmd.ExecuteNonQuery();
con.Close();
if (result == 1)
{
BindRegistrations();
ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Record Added successfully');", true);
}
}
}
这里只是我想添加到代码,如果用户名已经存在。如果我输入相同的用户名onceagain。
here just i want to add to code, that if username already exists. if i enter the same username onceagain.
推荐答案
执行选择查询以检查用户名是否已存在。使用以下查询选择
SELECT *
来自ACRegistrations
WHERE用户名= TxtUsername.text
如果不存在则执行插入查询。
Do a select query for checking whether user name already present. Use below query for select
SELECT *
FROM ACRegistrations
WHERE Username = TxtUsername.text
If its not present then do an insert query.
这篇关于从gridview页脚文本框添加时,不要在数据库中插入重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!