Skip to content

How to compare 2 values are equal or not

Durga Velusamy

Durga Velusamy

Asked Jul 7, 2015, 1:38 AM EDT4 answers
Share
LinkedInX
the below code shows message as deleted successfully after delete data from database its fine. but
also it shows same message even the data not in the table.
 
now i need compare empno from database  and my value of empno i.e passing from frontend textbox are equal.
 
if equl means then only permit to delete query to delete.
else
means it need to show  message as Data Not in Table
 
 
[WebMethod(EnableSession = true)]
C#
        public List<Emptab> Delete(string[] datas)
        {
            List<Emptab> tDataList = new List<Emptab>();
            Emptab tdata;

            try
            {
                tdata = new Emptab();
                tdata.empno = datas[0];
                tdata.gender = datas[1];
                tdata.eaddress = datas[2];
                tdata.street = datas[3];
                tdata.city = datas[4];
                tdata.state = datas[5];
                tdata.pin = datas[6];
                tdata.mobile = datas[7];
                tdata.mail = datas[8];
                tdata.cnumber = datas[9];
                tdata.ename = datas[10];
                tdata.dob = datas[11];
                tdata.age = datas[12];
                tdata.qualification = datas[13];
                tdata.depart = datas[14];
                tdata.design = datas[15];

                string ls_query = "";

                ls_query = "delete from emptab where empno ='" + datas[0] + "'";
                odcon = new OdbcConnection(con);
                odcon.Open();
             if (tdata.empno == datas[0])
                    {
                                comm = new OdbcCommand(ls_query, odcon);
                                comm.ExecuteNonQuery();

                                tdata.dsexecstatus = "Success";
                                tdata.dsmsg = "Deleted Successfully";
                                tDataList.Add(tdata);
                            }
                            else
                            {
                                tdata = new Emptab();
                                tdata.dsexecstatus = "FAIL";
                                tdata.dsmsg = "CANNOT ABLE TO DELETE ";
                                tDataList.Add(tdata);

                }
            }
            catch (Exception)
            {

                throw;
            }
            odcon.Close();
            return tDataList;
        }

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Manoj Bhoir
    0
    Hi,
     
    SqlCommand.ExecuteNonQuery Method Executes a Transact-SQL statement against the connection and returns the number of rows affected.
     
    Instead of this code :
     
    C#
                 if (tdata.empno == datas[0])
                        {
                                    comm = new OdbcCommand(ls_query, odcon);
                                    comm.ExecuteNonQuery();
                                    tdata.dsexecstatus = "Success";
                                    tdata.dsmsg = "Deleted Successfully";
                                    tDataList.Add(tdata);
                                }
                                else
                                {
                                    tdata = new Emptab();
                                    tdata.dsexecstatus = "FAIL";
                                    tdata.dsmsg = "CANNOT ABLE TO DELETE ";
                                    tDataList.Add(tdata);
    
                    }
    Try this :
    Code
                                    comm = new OdbcCommand(ls_query, odcon);
                                    int rows = comm.ExecuteNonQuery();
    if (rows > 0)
    {
    Code
                                    tdata.dsexecstatus = "Success";
                                    tdata.dsmsg = "Deleted Successfully";
                                    tDataList.Add(tdata);
    Code
    }
    else
    {
    Code
                                    tdata = new Emptab();
                                    tdata.dsexecstatus = "FAIL";
                                    tdata.dsmsg = "CANNOT ABLE TO DELETE ";
                                    tDataList.Add(tdata);
    }
    0
  • Durga Velusamy
    0
    Your right sir, but there is one problem
    is that if i have 4 rows in my table the condition is satisfied right then  
     
    1 st field empno=1 
                 empno=2
                empno=3
               empno=4 
     
     how can i delete 5 without empno=5 data not in table in this condition it need to show message as "data not in table"
    but the below  code cannot show this message  but run  show message as "Deleted Successfully"
     
    so need to compare with table data with our passing value if that value there in table means then delete otherwise show message as data not table.
     
    Code
    comm = new OdbcCommand(ls_query, odcon);
    int rows = comm.ExecuteNonQuery();
                           here how to compare the values
    if (rows > 0)
    {
    Code
    tdata.dsexecstatus = "Success";
    tdata.dsmsg = "Deleted Successfully";
    tDataList.Add(tdata);
    Code
    }
    else
    {
    Code
    tdata = new Emptab();
    tdata.dsexecstatus = "FAIL";
    tdata.dsmsg = "CANNOT ABLE TO DELETE ";
    tDataList.Add(tdata);
    }
    0
  • Durga Velusamy
    0
    thank u Manoj Bhoir sir its working correctly and condition also satisfied thank u sir.
    0
  • 1 short reply
    • Manoj Bhoir
      0
      Welcome.
      0