Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi, i just created a database and created a table and tried to insert data into table using c# program, i written code for insert, update, delete.. but coming to delete code it works correctly...
the work is deleting the values if i enter the name in textbox and showing a message that "values deleted"... but actually i need is
when a name is entered which doesn't exists in table or database it should display a message that "values does not exists"...
the code i written is


C#
private void button3_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string homephone = textBox2.Text;
            string workphone = textBox3.Text;
            string email = textBox4.Text;
            if (name == "")
            {
                MessageBox.Show("please enter name", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                System.Data.SqlClient.SqlConnection sqlConnection3 =
new System.Data.SqlClient.SqlConnection("Persist Security Info=True;User ID=*****;Password=******;Initial Catalog=mydb5;Server=myserver");
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = "if exists (select * from phonebook) delete phonebook where NAME=@NAME;";
                cmd.Parameters.AddWithValue("@NAME", name);
                cmd.Parameters.AddWithValue("@HOMEPHONE", homephone);
                cmd.Parameters.AddWithValue("@WORKPHONE", workphone);
                cmd.Parameters.AddWithValue("@EMAIL", email);
                cmd.Connection = sqlConnection3;
                sqlConnection3.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("values deleted");

            }
        }


additional information copied from non-solution below
hi mr.TADIT DASH.... your answer is good but what i need is a message should be displayed...

see...
when i type a name in textbox field... and click on delete.. it was showing values deleted even if the name does not exists in database....

when i type a name in textbox field... and click on delete.. it was showing deleted if the name exists in database.... and the values with the name were deleting,,,

my query is,,, when ever if i type a name which does not exist in database table... it should show "name doesnot exists in database"

please just check it once and answer me...
Posted
Updated 3-Jun-14 22:58pm
v3
Comments
ridoy 4-Jun-14 4:07am    
Then you should add search as like your insert or delete. Before deciding to delete you should make a search query with the textbox value to your database.

To insert, you are using the below query...
C#
cmd.CommandText = "if exists (select * from phonebook) delete phonebook where NAME=@NAME;";

But before that, you want to check if that really exists or not. For that, you have to run another query to see if it exists or not. You can use COUNT().
C#
cmd.CommandText = "select count(someColumnName) from phonebook where NAME=@NAME;";

Now, use SqlDataReader or SqlAdapter or ExecuteScalar method to read the selected value. If that is > 0, then the name exists.

Note: Replace your existing Column Name for "someColumnName".
 
Share this answer
 
v2
Comments
chaitanya556 4-Jun-14 4:59am    
hi mr.TADIT DASH.... your answer is good but what i need is a message should be displayed...

see...
when i type a name in textbox field... and click on delete.. it was showing values deleted even if the name does not exists in database....

when i type a name in textbox field... and click on delete.. it was showing deleted if the name exists in database.... and the values with the name were deleting,,,

my query is,,, when ever if i type a name which does not exist in database table... it should show "name doesnot exists in database"

please just check it once and answer me...
Hey,

I gave you the Logic so that you will get started. My intention was to get you started.

As I said "If that is > 0, then the name exists.". That means it does not exists, if it does not satisfies this, right... Just a matter of writing the codes, which I did not do for you. But DamithSL did it for you in Solution 4. However, the logic is same.

You accepted that answer, but you can also mark my solution as answer.

Thanks,
Tadit
Nelek 4-Jun-14 4:59am    
OP wanted to chat with you using a non-solution. I have copied the information to the question before the solution 3 gets nuked, have a look
Nelek 4-Jun-14 5:01am    
OK, OP did it correctly at the same time as me
Thanks Nelek, :D

I am getting the notifications sometimes and sometimes not. As I notified for your reply, I saw that DamithSL posted an answer with the same logic :D :D and OP accepted that.

I was like, what !!! and OP replied me that it did not help him/her. :P :P

Regards,
Tadit
get the count first and show the message if not exist, otherwise proceed with delete.. :-)
C#
using (SqlConnection cn = new SqlConnection(sqlConnectionString))
{
    cn.Open();

    using (SqlCommand commandRowCount
        = new SqlCommand("SELECT COUNT(*) FROM phonebook where NAME=@NAME", cn))
    {
        commandRowCount.Parameters.AddWithValue("@NAME", name);
        var countStart = (int)commandRowCount.ExecuteScalar();

        if(countStart< 1)
        {
            MessageBox.Show("Name doesnot exists in database");
            return;
        }
    }
    using (SqlCommand cmdDel
        = new SqlCommand("delete phonebook where NAME=@NAME", cn))
    {
        cmdDel.Parameters.AddWithValue("@NAME", name);
        cmdDel.ExecuteNonQuery();
        MessageBox.Show("values deleted");
    }
}
 
Share this answer
 
Comments
chaitanya556 4-Jun-14 5:24am    
thanks for the answer Mr.DAMITH SL
Before deleting a name 1st u select the data using where condition name;

then put the data in datatable

then

C#
 if(datatable.rows.count>0)
 {
    Your delete query 
 } 

 else
{
  your message;
}
 
Share this answer
 
Comments
Please read the question carefully. OP is not using any DataTable here.

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