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
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...