none
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 = 
    DATA_LAYER.get_emails_proc(TextBox6.Text).Tables[0].Rows[0][0].ToString();  

    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;
                    //adapter.InsertCommand
                    DataSet myDataSet = new DataSet();
                    try 
                    {
                        adapter.Fill(myDataSet);
                        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 !


    GeeMann

    Friday, July 6, 2012 9:41 PM

Answers

  • 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: http://geek-goddess-bonnie.blogspot.com/2010/04/create-xsd.html).

    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:
        this.MyData.Clear();
    
        try 
        {
            adapter.Fill(this.MyData);
        }
        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
    get_emails_proc(TextBox6.Text);
    
    // 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]

    geek-goddess-bonnie.blogspot.com

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