Click here to Skip to main content
15,923,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Good day

I want to display my tables data in a datagridview using a Stored Procedure that I have created in Sql server 2005.

My Stored Procedure is working and executing. All my data is displaying in Sql from the Stored Procedure.

Here are my C# code: I have written a method inside of my class.

public string display()
       {
          SqlDatabase sqldb = new SqlDatabase(ConnectionString);
          IDataReader reader = sqldb.ExecuteReader("pSEL_mLearner", new Object[] { });


          return reader.ToString();
       }


And here is the code that I use to call my method in the main unit on my Display button.

C#
private void btnDisplay_Click(object sender, EventArgs e)
       {
           dataGridView1.DataSource = login.display();


No data is displayed inside my datagridview.

What is wrong?
Posted
Updated 23-Jan-12 0:30am
v2

public string display()



instead of return string value


U can return DATASET and Then Try...
I Hope It Will WOrk..
 
Share this answer
 
Hi,
dataGridView1.DataSource = login.display()

After writting the above line you forgot the binding the data to grid view.Thats why It will not display the data to grid view.
At your this code after that line you insert below line.It will shows data to the grid view.
dataGridView1.DataBind()
 
Share this answer
 
v2
Hi, here is the Solution to my question

public DataTable display()
       {

 SqlDatabase sqldb = new SqlDatabase(ConnectionString);
 DataTable dt = new DataTable();
 IDataReader reader = sqldb.ExecuteReader("pSEL_mLearner", new object[] { });

 dt.Load(reader);

return dt;
       }



C#
private void btnDisplay_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = login.display();

                dataGridView1.DataSource = dt;
        }
 
Share this answer
 
Here is another why how you can do it

public DataTable display()
        {
     DataSet dsGlobalConfig = null;
     SqlDatabase sqldb = new SqlDatabase(ConnectionString);
     dsGlobalConfig = sqldb.ExecuteDataSet("pSEL_mLearner", new object[] { });

     return dsGlobalConfig.Tables[0];                   
 
       }


private void btnDisplay_Click(object sender, EventArgs e)
      {
          DataTable dt = new DataTable();
          dt = login.display();

          dataGridView1.DataSource = dt;
      }
 
Share this answer
 

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