Skip to content

Try Cath , throw Error

Loeurt

Asked Jun 24, 2009, 8:38 PM EDT1 answer
Share
LinkedInX
Please our member help me...(SQL server 2005 ,and C# language)
I want to check duplicate Id in tblStaffDetail before insert data to this table ..
so i write this methord:

public bool IsexistID(String IsID, StaffDetailInfo objInfo, SqlTransaction sqlTran)
        {
            bool blresutl = false;
            String StrSQL = "SELECT StName FROM TblStaffDetail \r\n" +
                            "WHERE StID=@StID";
            
            try
            {
                SqlCommand sqlCom = DBHelper.SQL_CONNECTION.CreateCommand();
                sqlCom.CommandText = StrSQL;
                sqlCom.Transaction = sqlTran;
                CommandHelper(sqlCom, objInfo);//Make Object from other Methord
                GetStaffName = sqlCom.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
               ex.ToString()              
            }
            if (GetStaffName == null)
            {
                blresutl = false;
            }
            else
            {
                blresutl = true;
            }
            return blresutl;
        }

this methord is working if the ID is really duplicated , but if ID is not Exist it will catch error , and i can't insert new record ..
     I want to kill this catch error ..and not catch if input new data .Please help me

1 Answer

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

  • Niradhip Chakraborty
    0
    Try This:
    public bool IsexistID(String IsID, StaffDetailInfo objInfo, SqlTransaction sqlTran)
            {
                bool blresutl = false;
                if( IsID != null || IsID !="")
                {
                   String StrSQL = "SELECT StName FROM TblStaffDetail \r\n" +
                                "WHERE StID=@StID";
               
                     try
                      {
                        SqlCommand sqlCom = DBHelper.SQL_CONNECTION.CreateCommand();
                        sqlCom.CommandText = StrSQL;
                        sqlCom.Transaction = sqlTran;
                        CommandHelper(sqlCom, objInfo);//Make Object from other Methord
                        GetStaffName = sqlCom.ExecuteScalar().ToString();
                      }
                 catch (Exception ex)
                 {
                       ex.ToString()              
                 }
                           
                 if (GetStaffName == null)
                 {
                     blresutl = false;
                 }
                 else
                 {
                      blresutl = true;
                 }
                }
               else
               {
                   blresutl = false;
               }
                return blresutl;
            }
    0