检查值是否重复 [英] To check if values are duplicate or not
本文介绍了检查值是否重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在点击提交按钮之前检查插入文本框中的值是否重复
我尝试了以下代码但是我没有这样做
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Data.SqlClient;
public partial class _Default:System.Web.UI.Page
{
SqlConnection con = new SqlConnection( @ 数据源= NEENA-PC\SQLEXPRESS;初始目录=实践;集成安全性=真);
public void custom()
{
string select = 从[insert]中选择名称;
SqlCommand cmd = new SqlCommand( select ,con);
SqlDataReader sdr;
con.Open();
sdr = cmd.ExecuteReader();
// if(!IsPostBack)
// {
if (sdr.Read())
{
if (TextBox1.Text == sdr [ 0 ]。ToString ())
{
Label1.Text = error;
TextBox1.Text = ;
TextBox1.Focus();
// CustomValidator1.ErrorMessage =erroe;
}
else
{
Label1.Text = 确定跨度>;
// CustomValidator1.ErrorMessage =scd;
}
}
// }
con.Close();
}
受保护 void Page_Load( object sender,EventArgs e)
{
}
protected void Button1_Click( object sender,EventArgs e)
{
custom();
Response.Write( Hiii);
}
}
谢谢。
解决方案
< blockquote>只需发出一个包含以下内容的查询,并提供要查找为变量的字符串:从[insert]中选择*,其中name = @ name
。如果你得到任何非空结果,你已经有了这个名字。
你可以做一个ajax回调来从客户端检查这个。
试试这个
System.Collections.IEnumerator ir = ds.Tables [0] .Rows.GetEnumerator();
while(ir.MoveNext())
{
DataRow dr = ir.Current as DataRow;
TextBox1.Text = dr [ 状态]
}
I want to check if value inserted in a text box is duplicate or not before clicking on submit button
I tried the following code but I failed doing so
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=NEENA-PC\SQLEXPRESS;Initial Catalog=practice;Integrated Security=True");
public void custom()
{
string select = "Select name from [insert]";
SqlCommand cmd=new SqlCommand(select,con);
SqlDataReader sdr;
con.Open();
sdr = cmd.ExecuteReader();
//if (!IsPostBack)
//{
if (sdr.Read())
{
if (TextBox1.Text == sdr[0].ToString())
{
Label1.Text = "error";
TextBox1.Text = "";
TextBox1.Focus();
//CustomValidator1.ErrorMessage = "erroe";
}
else
{
Label1.Text = "ok";
//CustomValidator1.ErrorMessage = "scd";
}
}
// }
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
custom();
Response.Write("Hiii");
}
}
Thanks.
解决方案
Just issue a query with following content, and supplying the string to look for as variable:Select * from [insert] where name=@name
. If you get any non-empty result, you have the name already.
You can make an ajax callback to check this from client side.
Try like this
System.Collections.IEnumerator ir = ds.Tables[0].Rows.GetEnumerator();
while (ir.MoveNext())
{
DataRow dr = ir.Current as DataRow;
TextBox1.Text = dr["Status"]
}
这篇关于检查值是否重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文