Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my code is....
C#
public static bool SaveChitGroupDetails(ChitGroup obj)
  {
  try
  {
   _dal = DataLayer.GetInstance();
   _dal.Sql = StoredProcedures.SaveChitGroupDetails;
  _dal.CommandType = CommandType.StoredProcedure;
  _dal.AddParameter("_grcode",obj.GrpCode, DbType.String,ParameterDirection.Input);
              _dal.AddParameter("_brid", obj.Brid, DbType.Int16, ParameterDirection.Input);
              _dal.AddParameter("_prdcode", obj.Prdcode, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_valuecode", obj.Valuecode, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_companychit", obj.Companychit, DbType.Boolean, ParameterDirection.Input);
              _dal.AddParameter("_series", obj.Series, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_status", obj.Status, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_bylawnumber", obj.Bylawnumber, DbType.String, ParameterDirection.Input);
              _dal.AddParameter("_registrationdate", obj.RegistrationDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_commencedate", obj.CommenceDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_terminatedate", obj.TerminateDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_maxdiscount", obj.MaxDiscount, DbType.Int16, ParameterDirection.Input);
              _dal.AddParameter("_active", obj.Active, DbType.Boolean, ParameterDirection.Input);
              _dal.AddParameter("_userid",obj.Userid,DbType.String,ParameterDirection.Input);
              _dal.AddParameter("_amndmntdate", obj.AmendmentDate, DbType.Date, ParameterDirection.Input);
              _dal.AddParameter("_amndtrmntdate", obj.AmendTerminateDate, DbType.Date, ParameterDirection.Input);
              
int rtrnid = _dal.ExecuteNonQuery();
              return true;
          }
          
catch (Exception ex)
          {
              throw ex;
              return false;
          }
      
    finally
          {
              if (_dal.Connected)
                  _dal.Connection.Close();
              //return true;
          }
      }
Posted
Updated 11-Sep-15 23:42pm
v2
Comments
Richard MacCutchan 12-Sep-15 5:49am    
What exception do you expect it to catch?
You also have some rather bad code there:

int rtrnid = _dal.ExecuteNonQuery();
return true;

You should not return true without checking the value of rtrnid.
And when you catch an exception you should do something with it rather than just throw it again.
User-11630313 12-Sep-15 5:55am    
if in case database error occurred it does not go to catch block it directly go to final block and returns true..if i did any mistake pls explain clearly
Richard MacCutchan 12-Sep-15 6:22am    
A database error does not necessarily produce an exception. Fix the parts of your code that are broken first, and add someting in your catch block to examine any exception that is thrown. You can then trap that with your debugger to see exactly what is happening.

1 solution

That code won't work: it (correctly) gives a warning that you are ignoring:
Unreachable code detected

This is because your method can never return false - the throw of the exception transfers control to the "closest" catch block immediately, without needing to return to the calling method directly.

Put a breakpoint in your method, and follow exactly what happens: when your database activity fails, execution will move to the catch block, and then leave the method without ever returning to where it was called from. It will not return true if an error occurs.

[edit]Typos[/edit]
 
Share this answer
 
v2
Comments
User-11630313 12-Sep-15 6:39am    
what will i do to solve my problem...please explain @OriginalGriff
OriginalGriff 12-Sep-15 6:56am    
That depends on what you think "working properly" means: it does exactly what you told it to, and I don't know what you wanted it to do! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900