Click here to Skip to main content
15,891,848 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi to all!

here is my code in which i pass querry to delete record from sql server

on the form i have gridview and two text boxes when i click on particular record to delete that record first ol all display in text boxes and then i press delete button it delete the record

but the problem here is i want to check that record exists their or not if not display message record is not present in data base,

and if record is present the it will re confirm to delete the record

here is my function



{
try
            {
                count = 0;
                int count1 = 0;
                sql_string = ("Data Source=7070270275;Initial Catalog=dsffC;User Id=fssdfadf;Password=fdsfsdfsdf");
                SqlConnection conn = new SqlConnection(sql_string);
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = (str_querry);
                SqlDataReader rdr = command.ExecuteReader();
                if(rdr.Read())
                count1++;
                    
                if (count1 == 0)
                {
                    MessageBox.Show("There is No Such ID present in Data Base");
                    conn.Close();
                }
                else
                MessageBox.Show("Deleted");
                conn.Close();
                conn.Dispose();
                count++;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Error in database connection");
            }
        }
Posted

ExecuteNonQuery[^] returns the number of rows affected. You can check it if it is 0.

int deletedRowsCount = command.ExecuteNonQuery();
if(deletedRowsCount==0) 
{
  MessageBox.Show("There is No Such ID present in Data Base");
}
else
{
  MessageBox.Show(deletedRowsCount.ToString() + "rows deleted.");
}
 
Share this answer
 
Comments
kami124 27-Jul-11 1:43am    
ya you are absolutaly right but still there is one more problem
that before deleting from data base a message box show to re confirm it
if "yes" then delete and if no dont delete
Prerak Patel 27-Jul-11 1:59am    
You can confirm with adding below code in if code in Tajuddin's answer.

var result = MessageBox.Show("Are you sure you want to delete?", "Confirm",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
//delete
}
Hi,

the gridview was binded with available records only right then what is the use of searching again it is there are not ?


any how,

to test wheathere it is there are not and to delete follow the following one,
C#
SqlConnection con=new SqlConnection ("connectionstring");
        con.Open ();
        SqlCommand cmd = new SqlCommand("select count(*) from tableName where cid=1233", con);
        int i = 0;
        i=(int)cmd.ExecuteScalar();
        if (i > 0)
        {
            //record is available
            SqlCommand deleteCmd = new SqlCommand("delete tablename where cid=1233", con);
            int res = 0;
            res=deleteCmd.ExecuteNonQuery();
            if (res > 0)
            {
                //deleted successfully
            }
            else
            {
                //failed to delete
            }
        }
        else
        {
            //there is no record
        }
 
Share this answer
 
v2
Comments
Kim Togo 27-Jul-11 2:03am    
You do not need to check if row exists before deleting.
If ExecuteNonQuery returns more then 0 ( deleteCmd.ExecuteNonQuery() > 0 ), then the record was deleted.
Tajuddin_HYD 27-Jul-11 2:19am    
yeah i know, but he is asking to check before deleting
kami124 27-Jul-11 3:25am    
hi there is some problem here in the above code which i make it
i=(int)cmd.ExecuteScalar();
the error is object refrence not set to an instance of an object

how to handle it

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