how do I handle an expected dataset return within a function RRS feed

  • Question

  • Hi;

    I am trying to add some exception handling code to some code that I maintain

    and I am a loss as to how to handle an exception within a function.

    The code is within a button handler and it populates a series of textboxes with

    a dataset if everything goes ok.

    TextBox7.Text = 

    And the function looks like so :

      public static DataSet get_emails_proc(string LoanNumber)
                    // Get the ConnectionStrings collection.
                    ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["connSomeDatabase"];
                    //Set up a adapter
                    SqlConnection conn = new SqlConnection(connectionStringSettings.ConnectionString);
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    //String TheSQLString = "SELECT A, B FROM GIANT.CODES WHERE MYKEY = @MYKEY";
                    //set the stored proc[bigProc].[sproc_Get_A_and_B_Emails]
                    adapter.SelectCommand = new SqlCommand("[GIANT].[sproc_Get_A_and_B_Emails]", conn);
                    adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                    //adapter.SelectCommand = new SqlCommand(TheSQLString, conn);
                    //set the parameters
                    adapter.SelectCommand.Parameters.Add("@MYKEY", SqlDbType.VarChar, 20).Value = Entered_Number;
                    DataSet myDataSet = new DataSet();
                        return myDataSet;
                    catch (Exception ex)
                        Trace.WriteLine(ex.ToString(),"Could not create dataSet");
                        return null;

    Can anyone see a better way to handle the exception ?

    Thanks for your insights !


    Friday, July 6, 2012 9:41 PM


  • A better method is to use databinding, rather than setting the individual TextBox.Text field. Also, this would work even better if you had a pre-defined DataSet (could even be a Typed DataSet, which I recommend. See my blog post about how to created a Typed DataSet without all the hassle of TableAdapters getting thrown in the mix:

    Do something like this:  

    // define a DataSet member:
    private MyDataSet MyData = new MyDataSet();
    // Change your method to simply Fill the MyData member 
    // it will be an empty DataSet in the case of an exception
    public static void get_emails_proc(string LoanNumber)
        // the same Connection and DataAdapter stuff you had before
        // get rid of this, you won't need it
        // DataSet myDataSet = new DataSet();
        // But, depending on whether or not you call this to refill the DataSet
        // you could do this instead, to start with an empty DataSet:
        catch (Exception ex)
            Trace.WriteLine(ex.ToString(),"Could not create dataSet");
    // Now, in either your Form's constructor (after the InitializeComponent()) 
    // or in the Form's Load eventhandler, have code like this:
    // this assumes a column name of "A"
    this.TextBox7.DataBindings.Add("Text", this.MyData.Tables[0], "A");
    // continue with whichever other controls you need to populate with this DataSet
    // Then, in your button click, you only need to call the method to fill the DataSet
    // Since TextBox7 has already been databound, it will show the column if there's data
    // and it will be empty if there isn't.

    ~~Bonnie Berent DeWitt [C# MVP]

    • Marked as answer by GeeMann Tuesday, July 10, 2012 10:55 PM
    Saturday, July 7, 2012 11:12 PM