Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a table where ID column is unique not null, i want to check if i enter duplicate entry in my ID column so a message box should come.i tried many but still not get result. please help me sir
please give me some hint.

What I have tried:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
           con.Open();
           string query = "select code from countermaster2";
           SqlDataAdapter da = new SqlDataAdapter(query,con);
           DataSet ds = new DataSet();
         if( ds.Tables["countermaster2"].Rows.Find(ds.Tables["countermaster2"].Rows)==comboBox2.SelectedItem )
           {
               MessageBox.Show("Code is already exsist");
           }
          else
           {
               MessageBox.Show("enter new records");
           }
Posted
Updated 1-May-17 20:49pm
Comments
Debarshi Chakraborty 2-May-17 2:35am    
so is your current code not working? What are you looking for? better design or correct syntax?
faizy001 2-May-17 2:40am    
sir i want to my code should be run properly. i have tried many times but still not working. please do my code in correct format .

I think you should do it at SQL side.
SQL
IF NOT EXISTS(SELECT COLUMNNAME FROM TABLENAME WHERE COLUMNNAME=@COLUMNNAME)
BEGIN

-- do your stuff here
SELECT 'Record saved successfully' as Response

END
ELSE
BEGIN
  SELECT 'This value exists already' as Response

END


OR

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
           con.Open();
           string query = "select code from countermaster2 where code = @code";
           SqlDataAdapter da = new SqlDataAdapter(query,con);
           DataSet ds = new DataSet();
        //Pass @code value in command

            if(ds.Table[0].Rows.Count > 0)
           {
               MessageBox.Show("Code is already exsist");
           }
          else
           {
               MessageBox.Show("enter new records");
           }


Excuse Typo mistakes

Let me know if still you have any query or concern for same.
 
Share this answer
 
Comments
faizy001 2-May-17 7:33am    
hello sir . i applied this codes like that but it is not working . please check where . am wrong


SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
con.Open();
string query1 = "insert into countermaster2(code, name, type) values(@code, @name, @type)";
SqlCommand cmd = new SqlCommand(query1,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
cmd.Parameters.AddWithValue("@code", comboBox2.SelectedItem);
cmd.Parameters.AddWithValue("@name", textBox2.Text);
cmd.Parameters.AddWithValue("@type", comboBox1.SelectedItem);

if (ds.Tables["countermaster2"].Rows.Count>0)
{
MessageBox.Show("Code is already exsist");
}
else
{
MessageBox.Show("enter new records");
}



cmd.ExecuteNonQuery();


MessageBox.Show("Saved");
Nirav Prabtani 2-May-17 7:38am    
You are just inserting values.

You have to perform as i suggest in above solution.
faizy001 2-May-17 7:34am    
sir this is very important for me please solve the problem
bool existDuplicate= (from DataRow dr in ds.Tables["countermaster2"].Rows where (string)dr["code"] == comboBox2.SelectedItem select dr).Any();
 
Share this answer
 

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