Click here to Skip to main content
15,868,217 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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#:

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
Posted
Updated 22-Apr-15 23:48pm
v2
Comments
priyadarshini tv 23-Apr-15 3:48am    
what you meant??
you want to show a message when user enters duplicate emailid??
Herman<T>.Instance 23-Apr-15 5:48am    
What's the problem?
BacchusBeale 23-Apr-15 6:26am    
I can't see any code to do with checking the key for an email? But the Database SQL connections etc should in separate class and Staff should be another class calling the database. Also, it's not good to call you UI elements TextBox1, TextBox2 etc, they should have descriptive names.
VandrenSKL 24-Apr-15 0:55am    
Hi guys,

I able to submit the data into database but how to display if any duplicate data especially email?

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.
 
Share this answer
 
Comments
VandrenSKL 24-Apr-15 0:54am    
Hi Webmaster442,

which line do i need to put?

is it under here?

try
{
conn.Open();

cmd.ExecuteNonQuery();

ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Record Saved Successfully.');window.location='DataEntry.aspx';", true);
}
catch (Exception ex)
{
*CHANGE THIS?*>>>>> Response.Write("Oops!! Following error occured: " + ex.Message.ToString());
}
finally
{
conn.Close();
cmd.Dispose();
}
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
 
Share this answer
 
v2
Comments
VandrenSKL 24-Apr-15 3:54am    
Hi,

still not success.

Should I use if else?

As it's still able to submit the data without checking it. I just need to pop out the message after submit and it will check it and not allow to submit.
priyadarshini tv 24-Apr-15 7:24am    
This code is allowing you to insert data without checking???
VandrenSKL 27-Apr-15 2:57am    
yes it doesn't checking on it and still allowing insert data
priyadarshini tv 27-Apr-15 6:40am    
Where u added this code?? Whether u are getting alert like emailid exists when u are entering duplicate mail id??
VandrenSKL 28-Apr-15 3:19am    
Sorry, I'm getting this error "The SelectCommand property has not been initialized before calling 'Fill'."

Below are my code:

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();

// SqlCommand cd = new SqlCommand("EmailDup", conn);

{

String FullName;
String FirstName;
String LastName;

String email = TextBox3.Text.Trim();
String select = select ID from Customer where email=@email limit 1;
cmd.Parameters.AddWithValue("@email", email);

DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Emailid exists')", true);
return;
}


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["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)
{

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900