4
Answers

Plz Solve my prob. I m using a mathod in a class which return dataset obj. but it not working.

Vikas Ahlawat

Vikas Ahlawat

15y
1.9k
1
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

Answers (4)