i have make a class
class Class2
{
public DataSet retdataset()
{
SqlConnection con = new SqlConnection("Data Source =.\\SQLEXPRESS;Initial Catalog = schoolmgt; password=; Integrated Security = true;");
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("retrive_all_students_reg_detail",
con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
con.Close();
return (ds);
//dataGridView2.DataSource = ds.Tables[0];
}
}
now i call this on button click event
private void button1_Click(object sender, EventArgs e)
{
Class2 o = new Class2();
dataGridView1.DataSource = o.retdataset();
}
when i run my window application and click the button then no data appear in datagridview
IF I WRITE THE WHOLE CODE UNDER BUTTON CLICK EVENT THEN THIS IS WORKING
LIKE BELLOW CODE
private void button1_Click(object sender, EventArgs e)
{
//Class2 o = new Class2();
//dataGridView1.DataSource = o.retdataset();
SqlConnection con = new SqlConnection("Data Source =.\\SQLEXPRESS;Initial Catalog = schoolmgt; password=; Integrated Security = true;");
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("retrive_all_students_reg_detail",
con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
CAN ANY ONE TELL MY MISTAKE??
PLZ HELP ME
class Class2
{
public DataSet retdataset()
{
SqlConnection con = new SqlConnection("Data Source =.\\SQLEXPRESS;Initial Catalog = schoolmgt; password=; Integrated Security = true;");
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("retrive_all_students_reg
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
con.Close();
return (ds);
//dataGridView2.DataSource = ds.Tables[0];
}
}
now i call this on button click event
private void button1_Click(object sender, EventArgs e)
{
Class2 o = new Class2();
dataGridView1.DataSource = o.retdataset();
}
when i run my window application and click the button then no data appear in datagridview
IF I WRITE THE WHOLE CODE UNDER BUTTON CLICK EVENT THEN THIS IS WORKING
LIKE BELLOW CODE
private void button1_Click(object sender, EventArgs e)
{
//Class2 o = new Class2();
//dataGridView1.DataSource = o.retdataset();
SqlConnection con = new SqlConnection("Data Source =.\\SQLEXPRESS;Initial Catalog = schoolmgt; password=; Integrated Security = true;");
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("retrive_all_students_reg
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
CAN ANY ONE TELL MY MISTAKE??
PLZ HELP ME

Dushan StankovicPosted Sep 23, 2009, 8:26 AM
Try this:
Class2 o = new Class2();
dataGridView1.DataSource =
o.retdataset().DataTable[0];
or use DataMember property.
DataSet is SET of tables, and you must choose table you want to reference to DataGridView
Vikas AhlawatPosted Sep 23, 2009, 8:31 AM
my problem solved
Class2 o = new Class2();
dataGridView1.DataSource = o.retdataset().Tables[0];
Vikas AhlawatPosted Sep 23, 2009, 7:56 AM
Class2 o = new Class2();
dataGridView1.DataSource = o.retdataset()[0];
Error 1 Cannot apply indexing with [] to an expression of type 'System.Data.DataSet' C:\Users\vikas\Documents\Visual Studio 2008\Projects\WindowsFormsApplication4\WindowsFormsApplication4\Form1.cs 35 40 WindowsFormsApplication4
Nir BarPosted Sep 23, 2009, 7:52 AM
Please check "Accepted Answer" if this post helps you
Nir