I am tryign to figure out what the best practice for exception handling is in regards to SqlCommand. I am mostly concerned with data exceptions when the command is executed. Here is what I have started on:
Code:
private void CreateOpportunity(string PODoc) { string Conn = "Data Source=server11;Initial Catalog=Main;User ID=uid;Password=pw"; string Account = "Name"; OpportunityID = NewID("opportunity"); AccountID = "A6UJ9A002N65"; using (SqlConnection slxConn = new SqlConnection(Conn)) { string sql = "INSERT INTO sysdba.OPPORTUNITY (OPPORTUNITYID,ACCOUNTID,DESCRIPTION,CLOSED,STATUS,SECCODEID,CREATEUSER,CREATEDATE,MODIFYUSER,MODIFYDATE) VALUES (@opportunityid,@accountid,@description,@closed,@status,@seccodeid,@createuser,@createdate,@modifyuser,@modifydate)"; SqlCommand cmd = new SqlCommand(sql, slxConn); cmd.Parameters.AddWithValue("@opportunityid", OpportunityID); cmd.Parameters.AddWithValue("@accountid", AccountID); cmd.Parameters.AddWithValue("@description", PONum); cmd.Parameters.AddWithValue("@closed", "F"); cmd.Parameters.AddWithValue("@status", "In-Process"); cmd.Parameters.AddWithValue("@seccodeid", "SYST00000001"); cmd.Parameters.AddWithValue("@createuser", "Admin"); cmd.Parameters.AddWithValue("@createdate", DateTime.Now); cmd.Parameters.AddWithValue("@modifyuser", "Admin"); cmd.Parameters.AddWithValue("@modifydate", DateTime.Now); try { slxConn.Open(); cmd.ExecuteNonQuery(); } catch (SqlException ex) { string str; str = "Source:" + ex.Source; str += "\n" + "Message:" + ex.Message; } finally { if (slxConn.State == ConnectionState.Open) { slxConn.Close(); } //method to send error message //SendError(str); } }
Comment