ssou
ssou

Reputation: 23

how to properly pass a datatable back to calling method and use it?

I am not familiar with c# or best practices, so I would like to know how to send a datatable filled in one method back to the calling method. (I assume this is the proper way of using a datatable in c#.)

What I have:

public class Utilities
    {
        public void Runproc()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString2"].ConnectionString;

            using (SqlConnection sqlcon = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("spGetAgeClass", sqlcon))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();

                        da.Fill(dt);
                    }
                }
            }
        }
    }

In my main form I have a datagrid that I would like to fill with the contents of dt. How is this done?

    private void FillFormDataGrid()
    {
        //How do I get the contents of datatable dt from utilities to use here in the form's datagrid?
            Utilities utilities = new Utilities();
            //??
    }

Thank you in advance.

Upvotes: 0

Views: 88

Answers (1)

ssou
ssou

Reputation: 23

This is what I used, though not sure it is the best approach since it swallows error. I did not want to return a null table and only raise a simple error notification, but as mentioned, I am not a c# developer. I have read that the use of a using statement is the preferred approach. However, it gave me an error when I didn't return anything during error, so I used the null table. I am open to hearing about what is wrong with this approach.

  public DataTable GetLogActivity(string Loc, DateTime StartDate)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString2"].ConnectionString;
        try
        {
            using (SqlConnection sqlcon = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("spLogActivity", sqlcon))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Location", SqlDbType.VarChar).Value = Loc;
                    cmd.Parameters.AddWithValue("@CheckStartDate", SqlDbType.DateTime).Value = StartDate;

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();

                        da.Fill(dt);
                        return dt;
                    }

                }


            }


        }
        catch
        {
            MessageBox.Show("Error retrieving data from database.");
            return null;
        }
    }



void PopulateDataGridView()
        {
            AssignRescueActivity ara = new AssignRescueActivity();
            DateTime StartDate = DateTime.Now;
            DataTable dtbl = ara.GetLogActivity("Main Kennel", StartDate);
            dgvMainKennelLogActivity.DataSource = dtbl;


        }

Upvotes: 1

Related Questions