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 Stankovic72 reputationPosted Sep 23, 2009, 4:26 AM EDT
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 DataGridViewNir Bar107 reputationPosted Sep 23, 2009, 3:52 AM EDT
private void button1_Click(object sender, EventArgs e) { Class2 o = new Class2(); dataGridView1.DataSource = o.retdataset()[0]; }Please check "Accepted Answer" if this post helps you
Nir
Vikas AhlawatRank #2,648 · 525 reputationPosted Sep 23, 2009, 3:56 AM EDT
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
Vikas AhlawatRank #2,648 · 525 reputationPosted Sep 23, 2009, 4:31 AM EDT
my problem solved
Class2 o = new Class2();
dataGridView1.DataSource = o.retdataset().Tables[0];