如何编写代码来显示重复的条目? [英] How to write a code to show a duplicate entry?
本文介绍了如何编写代码来显示重复的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的朋友们,
我的程序差不多完成但是我面临的问题是如何写一个重复的代码?
一旦用户在电子邮件重复条目中输入密钥,就会显示错误消息。
以下是我在C#中的代码:
Dear friends,
My program are almost done but what I'm facing an issues is how to write a duplicate code?
It is to show an error message once user had key in the Email duplicate entry.
below are my code in C#:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebPortal
{
public partial class DataEntry : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString;
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = ("select * from staff where Username = ('" + Session["Username"] + "')");
com = new SqlCommand(str, con);
object obj = Session["Username"];
SqlDataReader reader = com.ExecuteReader();
reader.Read();
Label1.Text = reader["Username"].ToString();
}
protected void Button7_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=.\\SQLEXPRESS;"
+ "AttachDbFilename=\"C:\\Users\\KLSIT\\Documents\\Visual Studio 2008\\Projects\\WebPortal\\WebPortal\\App_Data\\emaildata.mdf\";"
+ "Integrated Security=True;"
+ "Connect Timeout=30;"
+ "User Instance=True";
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionString);
System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
{
String FullName;
String FirstName;
String LastName;
cmd.Parameters.Add("Type", SqlDbType.NVarChar);
cmd.Parameters["Type"].Value = this.DropDownList2.Text;
cmd.Parameters.Add("Title", SqlDbType.NVarChar);
cmd.Parameters["Title"].Value = this.DropDownList1.Text;
cmd.Parameters.Add("FirstName", SqlDbType.NVarChar);
cmd.Parameters["FirstName"].Value = this.TextBox1.Text;
cmd.Parameters.Add("LastName", SqlDbType.NVarChar);
cmd.Parameters["LastName"].Value = this.TextBox2.Text;
cmd.Parameters.Add("Fullname", SqlDbType.NVarChar);
cmd.Parameters["Fullname"].Value = this.TextBox1.Text + " " + this.TextBox2.Text;
cmd.Parameters.AddWithValue("Email", SqlDbType.NVarChar);
cmd.Parameters["Email"].Value = this.TextBox3.Text.Trim();
cmd.Parameters.Add("Job_Title", SqlDbType.NVarChar);
cmd.Parameters["Job_Title"].Value = this.TextBox4.Text;
cmd.Parameters.Add("Company", SqlDbType.NVarChar);
cmd.Parameters["Company"].Value = this.TextBox5.Text;
cmd.Parameters.Add("Mobile_Phone", SqlDbType.NVarChar);
cmd.Parameters["Mobile_Phone"].Value = this.TextBox6.Text;
cmd.Parameters.Add("Office_Phone", SqlDbType.NVarChar);
cmd.Parameters["Office_Phone"].Value = this.TextBox7.Text;
cmd.Parameters.Add("Staff", SqlDbType.NVarChar);
cmd.Parameters["Staff"].Value = this.Label1.Text;
FirstName = TextBox1.Text;
LastName = TextBox2.Text;
FullName = TextBox1.Text + " " + TextBox2.Text;
cmd.CommandText = "INSERT INTO [Customer] ([Type], [Title], [Firstname], [Lastname], [Fullname], [Company], [Email], [Mobile_Phone], [Office_Phone], [Job_Title], [Staff]) VALUES (@Type, @Title, @Firstname, @Lastname, @Fullname, @Company, @Email, @Mobile_Phone, @Office_Phone, @Job_Title, @Staff)";
try
{
conn.Open();
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Record Saved Successfully.');window.location='DataEntry.aspx';", true);
}
catch (Exception ex)
{
Response.Write("Oops!! Following error occured: " + ex.Message.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
}
}
请指教,谢谢你非常
Please advice, thank you very much
推荐答案
尝试:
来自员工的SELECT Count(Email),其中Email =参数组通过电子邮件
它将为您提供具有指定电子邮件地址的用户总数。如果该数字为0,则该用户都没有该电子邮件地址。如果它不是0,那么你有一个副本。
Try:
SELECT Count(Email) from staff where Email = parameter group by Email
It will give you the total number of users with the specified e-mail adress. If the number is 0, then none of the user's have that e-mail adress. If it's not 0, then you have a duplicate.
你好b $ b
如果你有重复的emailid你不想插入细节然后你需要在插入命令之前进行此检查
string email = TextBox3.Text.Trim();
string select = select tablename其中Email = @ email limit 1;
command.parameters.addwithvalue(@ email,email);
datatable dt = new datatable();
sqldataadapter da = new sqldatadapter();
da.fill(dt);
if(dt.rows.count> 0)
{
ScriptManager.RegisterStartupScript(this,this.GetType(),popup,alert('Emailid exists'),true);
return;
}
这里id表示你的主键
Hi
if u have duplicated emailid and u dont want to insert the details then u need to do this check before insert command
string email=TextBox3.Text.Trim();
string select=select id from tablename where Email=@email limit 1;
command.parameters.addwithvalue("@email",email);
datatable dt=new datatable();
sqldataadapter da= new sqldatadapter();
da.fill(dt);
if(dt.rows.count>0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Emailid exists'),true);
return;
}
here id means your primary key
这篇关于如何编写代码来显示重复的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文