1

Can you help me about the syntax in Visual studio C# i want my phone directory that if you enter same data in primary key,it will display a Show message Box that display "already exist" if it is not, it will proceed to normal functions.. well im always trying but i can't figure it out...

con.Open();
       if (!dataGridView1.Equals(textBox3.Text)) 
        { 
        SqlCommand cmd = new SqlCommand (@"INSERT INTO Amer
                     (First, Last, Mobile, Email, Category)
                     VALUES        ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + comboBox1.Text + "')",con);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Saved!");
        textBox1.Text = "";
        textBox2.Clear();
        textBox3.Text = "";
        textBox4.Clear();
        comboBox1.SelectedIndex = -1;
        textBox1.Focus();
        Display();


       }
            else
            {
                MessageBox.Show("Mobile Number Already Exist!");
                textBox1.Text = "";
                textBox2.Clear();
                textBox3.Text = "";
                textBox4.Clear();
                comboBox1.SelectedIndex = -1;
                textBox1.Focus();
                Display();
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
Jaavatar
  • 11
  • 2

1 Answers1

0

Can u please tell what is this datagridView1 here coz i think the way you are comparing the text with textbox3, is not correct way.

I would suggest a good logic if it suits you:

Suppose you enter a value in a textbox. Then on button click, user submits the page, u can call a stored procedure, which will check if this value exists in the database. If it exists, then return from the stored procedure immediately with any status value, say -1 else continue to save the data and return 1. On the UI end, just check the value for -1 or 1 and display the message accordingly.

Refer the following link for using the logic I am suggesting:

Read return value from Stored procedure

Community
  • 1
  • 1
Tech Jay
  • 404
  • 1
  • 6
  • 16
  • If you mean it is a kind of grid control, then you cannot simply compare the grid to the textbox value. You need something like using the events like Itemcommand. In the events, you will find the control in which you have entered the value (Primary key value). Then you can compare and perform the operations. Following links will be more helpful for you in understanding how u can do this: http://www.codeproject.com/Articles/851092/ASP-NET-GridView-CRUD-Operations OR http://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx – Tech Jay Jul 26 '15 at 11:12
  • dataGridView1 is where you can see all the data that is entered... And when i enter a primary key data that is already in database it will gonna have an exemption,,,What i want sir is a syntax that will identify if the data is already in data base....if it contains,it will show a message that it is already in database,or if its not,it will add all the data.... – Jaavatar Jul 26 '15 at 11:15
  • Can u share the detail that the texbox, in which user is adding this Primary key value, is inside datagridView1 or outside the control ?? Even if the textbox is inside the dataGridView1 or not, the way u are comparing the values will not work. You must use something like the concept here http://www.aspdotnet-suresh.com/2011/03/how-to-check-username-availability.html – Tech Jay Jul 26 '15 at 11:22