Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm filling the datagridview from database on button click. I'm retrieving the column of data to be displayed in datagridview using stored procedure

Stored procedure

SQL
CREATE PROCEDURE [dbo].[prcDisplaydataTemplate]
AS
BEGIN
(SELECT DataTable.objectOriginalName FROM dbo.DataTable)
END



Button Click Event

private void btDataTemplate_Click(object sender, EventArgs e)
{
    DataSet dsobbtemp = new DataSet();
    DBConnection objConnection = new DBConnection();
    try
    {
        objConnection.GetConnection();
        dsobbtemp = objConnection.GetDataSet("prcDisplaydataTemplate");
        dgDtParameters.DataSource = dsobbtemp;
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        if (objConnection.sconnConnection.State == ConnectionState.Open)
        {
            objConnection.CloseConnection();
        }
    }
}



GetConnection Method

C#
public void GetConnection()
        {
            try
            {
                sconnConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ToString());
                sconnConnection.Open();
            }
            catch (SqlException) { throw; }


        }



GetDataSet Method

public DataSet GetDataSet(string sql)
       {
           DataSet dataset = new DataSet();
           SqlDataAdapter adapter = new SqlDataAdapter();
           SqlCommand cmd = new SqlCommand(sql, this.sconnConnection);
           adapter.SelectCommand = cmd;
           adapter.Fill(dataset);
           return dataset;
       }



Where I have went wrong my datagridview displays in blue color without any data

Thanks in Advance
Posted
Comments
BoxyBrown 17-Aug-11 8:34am    
May be you should specify Table of DataSet? for ex. dgDtParameters.DataSource = dsobbtemp.Table[0].

C#
dgDtParameters.DataSource = dsobbtemp.Tables[0];


More info see: http://www.dotnetperls.com/datagridview[^]
 
Share this answer
 
v2
I think the problem is in "GetDataSet" method.

change here

C#
cmd.CommandType = CommandType.StoredProcedure
 
Share this answer
 
Comments
CyborgForever 17-Aug-11 8:41am    
No thats not the problem. Only if I need to pass the SQL Parameters I need to mention CommandType
Mention table in dataset dsobjTemplate.Tables[0]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900