locked
Problem with DAL and retrive result to datagridview RRS feed

  • Question

  • Have got DAL method look like:

     

    #region
            public static DataTable ExecuteDataTableSP(string storedProcedureName, params SqlParameter[] arrParam)
            {
                DataTable dt = new DataTable();
    
                // Open the connection
                using (SqlConnection cnn = new SqlConnection("Data Source=derc;" +
                                                                             "Initial Catalog=sen;" +
                                                                             "User ID=sa;" +
                                                                             "Password=pass"))
                {
                    cnn.Open();
    
                    // Define the command
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection = cnn;
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = storedProcedureName;
    
                        // Handle the parameters
                        if (arrParam != null)
                        {
                            foreach (SqlParameter param in arrParam)
                                cmd.Parameters.Add(param);
                        }
    
                        // Define the data adapter and fill the dataset
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(dt);
                        }
                    }
                }
                return dt;
            }
            #endregion
    

     


    When I want to retrive result to datagrid view by this then it not fill it.. Have no idea its simple thing..

     

    DataTable dt = DAL.Attributes.ExecuteDataTable("GetProducts", null);
                dataGridView1.DataSource = dt;
    


    P.S If it is possible how to convert this method that I can declare in constructor that is stored procedure or just string query

    Sunday, November 20, 2011 8:31 PM

Answers

  • Hi JimmyJimm,

    I didn't find any problem about your code, please run your stored procedure in your sql server and try to find whether it can retrive data.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Alan_chen Monday, November 28, 2011 2:55 AM
    Tuesday, November 22, 2011 2:46 AM
  • Hi

       Try to add Default View on the end of the datatable atribution, like this:

    DataTable dt = DAL.Attributes.ExecuteDataTable("GetProducts", null);
    dataGridView1.DataSource = dt.DefaultView;
    
    

    Hope helps.


    MCP
    • Marked as answer by Alan_chen Monday, November 28, 2011 2:55 AM
    Tuesday, November 22, 2011 9:23 AM

All replies

  • Hi JimmyJimm,

    I didn't find any problem about your code, please run your stored procedure in your sql server and try to find whether it can retrive data.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Alan_chen Monday, November 28, 2011 2:55 AM
    Tuesday, November 22, 2011 2:46 AM
  • Hi

       Try to add Default View on the end of the datatable atribution, like this:

    DataTable dt = DAL.Attributes.ExecuteDataTable("GetProducts", null);
    dataGridView1.DataSource = dt.DefaultView;
    
    

    Hope helps.


    MCP
    • Marked as answer by Alan_chen Monday, November 28, 2011 2:55 AM
    Tuesday, November 22, 2011 9:23 AM
  • Hi JimmyJimm,

    Have you fixed your issue? If you have any problem, please feel free to let me know.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Friday, November 25, 2011 1:48 AM