如何验证gridview页脚文本框中已插入的记录 [英] How to validate already inserted record from gridview footer textboxes

查看:51
本文介绍了如何验证gridview页脚文本框中已插入的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨Frnds,



我想验证记录,如果已存在则显示消息记录已存在。

如果是新的记录,它必须插入。



在这里我有3个字段作为用户名,员工编号,电子邮件。



不得再次插入现有用户名。

不得再次插入现有。

不得再次插入现有电子邮件。



所以它应该验证,当在gridview页脚中添加按钮点击时。



------------ --------------------



所以,这是我的代码....



Hi Frnds,

I want to validate records, if it already exists show message Record already exists.
If it is new record, it must insert.

In this i have 3 fields as Username,EmployeeNumber, Email.

Existing username must not be inserted once again.
Existing must not be inserted once again.
Existing Email must not be inserted once again.

so it should validate, when add button clicks in gridview footer.

--------------------------------

So, this is my code....

protected void GVRegistrations_RowCommand(object sender, GridViewCommandEventArgs e)
 {
if (e.CommandName.Equals("AddNew Registrations"))
        {
            TextBox TxtUserName= (TextBox)GVRegistrations.FooterRow.FindControl("TxtFOOTERUserName");
            TextBox EmployeeNumber= (TextBox)GVRegistrations.FooterRow.FindControl("TxtFOOTEREmpNo");
            TextBox TxtEmail = (TextBox)GVRegistrations.FooterRow.FindControl("TxtFOOTEREmail");

            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT Count(*) FROM ACRegistrations where UserName= '" + TxtUserName.Text + "'", con);
            int count = int.Parse(cmd.ExecuteScalar() + "");

            if (count == 0)
            {

            cmd = new SqlCommand("INSERT into ACRegistrations(UserName,EmployeeNumber,Email) values('" + TxtUserName.Text + "','" + EmployeeNumber.Text + "','" + TxtEmail.Text + "')", con);
            int result = cmd.ExecuteNonQuery();

           
            if (result == 1)
            {
                BindRegistrations();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Record Added successfully');", true);
               
            }
            }

                     else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Sorry, This Record already exists');", true);
                    
                }

        }
            con.Close();
               
}





我想验证三个字段,一个接一个

请帮助我,提前谢谢。



I want to validate three fields, one after the other
Please help me, Thanks in advance.

推荐答案

下面的查询将检查数据库中是否存在这三个字段(用户名,员工编号,电子邮件)中的任何一个,它将返回计数超过零。请尝试以下查询。





Below query will check if any of these three fields (Username,EmployeeNumber, Email) exist in database it will return count more than zero. Please try below query.


"SELECT Count(*) FROM ACRegistrations where UserName= '" + TxtUserName.Text + "' or EmployeeNumber='" + EmployeeNumber.Text + "' or Email='" + TxtEmail.Text +"'";


< a href =http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html> http://www.aspdotnet-suresh.com/2011/02 /how-to-inserteditupdate-and-delete-data.html [ ^ ]







http:// www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx [ ^ ]



希望此帮助完整
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]



http://www.aspsnippets.com/Articles/Simple-Insert-Select-Edit-Update-and-Delete-in-ASPNet-GridView-control.aspx[^]

Hope this help full


注意:始终对任何值使用修剪()功能(良好实践)。

Note : Always use Trim() feature for any value(Good Practice).
"SELECT Count(*) FROM ACRegistrations where UserName= '" + TxtUserName.Text.Trim() + "' or EmployeeNumber='" + EmployeeNumber.Text.Trim() + "' or Email='" + TxtEmail.Text.Trim() +"'";





如果Count大于1,请不要插入警告用户,否则插入错误消息。



if Count is greater than one dont insert alert user with error message else insert.


这篇关于如何验证gridview页脚文本框中已插入的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆