Results 1 to 7 of 7

Thread: Multiple try catch

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    56

    Multiple try catch

    Hello all, in my app which connects to a database....I have multiple buttons, or places where I need to run a query, im using this to connect and issue a query:
    VB Code:
    1. try
    2. {
    3.    //connect to database and run query
    4. }
    5. catch(Exception ex)
    6. {
    7.    MessageBox.Show(ex.Message);
    8. }
    In my code it seems im just copying and pasting that in multiple spots, but running a different query, anyone have any ideas on how to optimize this? Maybe create a function to connect and such, but i'm not sure how...any suggestions? Thanks

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Multiple try catch

    One, I think you should catch an sqlException or Oledbexception (whatever your case is).

    Second, I also think that you should place your database manipulation functions in a separate class and call them from your form.

    But that's just my opinion, so I'm right.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: Multiple try catch

    Ah yes, Oledbexception makes perfect sense thanks. I had been thinking thats exactly what I should be doing using a class, it just seemed wrong to be using the same code over and over, i'm glad you had some input for me, thanks for the reply.

    Of course you're right...

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Multiple try catch

    He's not right, he can never be right.

    Just use something like
    VB Code:
    1. void NonQuery(string cmdtext,
    2.             sql.Parameters param)
    3.         {
    4.             try
    5.             {
    6.                 sql.Connection.Open(cn);
    7.                 new sql.Command(cmdtext,cn,param).ExecuteNonQuery();
    8.             }
    9.             catch(Exception ex)
    10.             {
    11.                 MessageBox.Show(ex.Message);
    12.             }
    13.             finally
    14.             {
    15.                 sql.Connection.Close(cn);
    16.             }
    17.         }
    where sql.Parameters is a collection of type SqlParameter, something like this
    VB Code:
    1. using System;
    2. using System.Data.SqlClient;
    3. using System.Collections;
    4. namespace lguis_0_1.sql
    5. {
    6.     public class Parameters:CollectionBase
    7.     {
    8.         public Parameters(){}
    9.         public SqlParameter this[int i]
    10.         {
    11.             get{ return (SqlParameter)this.InnerList[i];}
    12.         }
    13.         public void Add(SqlParameter v)
    14.         {
    15.             this.InnerList.Add(v);
    16.         }
    17.     }
    18. }
    and the sql.Command is something like
    VB Code:
    1. using System;
    2. using System.Data;
    3. using System.Data.SqlClient;
    4. namespace lguis_0_1.sql
    5. {
    6.     public class Command
    7.     {
    8.         SqlCommand cm=new SqlCommand();
    9.         public Command(){}
    10.         public Command(string cmdtext,
    11.             SqlConnection cn)
    12.         {
    13.             cm.CommandText=cmdtext;
    14.             cm.CommandType=CommandType.StoredProcedure;
    15.             cm.Connection=cn;
    16.         }
    17.         public Command(string cmdtext,
    18.             SqlConnection cn,
    19.             Parameters param)
    20.         {
    21.             cm.CommandText=cmdtext;
    22.             cm.CommandType=CommandType.StoredProcedure;
    23.             cm.Connection=cn;
    24.             foreach(SqlParameter t in param) cm.Parameters.Add(t);
    25.         }
    26.         public void ExecuteNonQuery()
    27.         {
    28.             cm.ExecuteNonQuery();
    29.         }
    30.         public SqlDataReader ExecuteReader()
    31.         {
    32.             return cm.ExecuteReader();
    33.         }
    34.         public object ExecuteScalar()
    35.         {
    36.             return cm.ExecuteScalar();
    37.         }
    38.     }
    39. }
    and the sql.Connection is
    VB Code:
    1. using System;
    2. using System.Data;
    3. using System.Data.SqlClient;
    4. namespace lguis_0_1.sql
    5. {
    6.     public class Connection
    7.     {
    8.         public Connection(){}
    9.         public static void Close(SqlConnection cn)
    10.         {
    11.             if(cn.State==ConnectionState.Open) cn.Close();
    12.         }
    13.         public static void Open(SqlConnection cn)
    14.         {
    15.             if(cn.State==ConnectionState.Closed) cn.Open();
    16.         }
    17.     }
    18. }
    I could be totally wrong.

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    56

    Re: Multiple try catch

    Thanks alot nebulom! That helps me alot as I started making my database class. I'll post back on how it goes..thanks again!

    But can you both be right!?!?

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Small tip about multiple catch structure : it should fall from the most specific to the most general(generic) . Like you catch different expected db exceptions then the base class Exception .

  7. #7
    Lively Member
    Join Date
    Nov 2003
    Location
    Amsterdam
    Posts
    74

    Re: Multiple try catch

    You can make use of Database Application block provided by Microsoft. That's already a tested,compiled assembly and easy to use. You dont need to write your database classes all over from start.
    I am using that application block and it's quite handy to use. I have created by Data base layer where I am calling the methods provided by the Application Blick to achieve my target.
    Everything I code, is a piece for Museum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width