SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=StudentRegistration;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from StudentInfo",con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Student";
why i am getting Object reference not set to an instance of an object in this the code is correct i think.
pls reply guys
why i am getting Object reference not set to an instance of an object in this the code is correct i think.
pls reply guys
Lakshmanan Sethu SankaranarayanPosted Mar 25, 2014, 9:54 AM
Brijendra GautamPosted Mar 25, 2014, 9:32 AM
Lakshmanan Sethu SankaranarayanPosted Mar 25, 2014, 8:53 AM
Brijendra GautamPosted Mar 25, 2014, 8:47 AM
Just remove the last line and it will work.
dataGridView1.DataMember = "Student"; //Remove it.
Munesh SharmaPosted Mar 25, 2014, 7:57 AM
public void displayDataGridView()
{
SqlConnection con = new SqlConnection("server MUNESH\\SQL2008R2;Database=data;UID=sa;Password=123;");
SqlCommand cmd = new SqlCommand("Select * from data1", con);
try
{
con.Open();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
}
Abhay ShankerPosted Mar 25, 2014, 6:05 AM
string connectionString = "Data Source=(local);Initial Catalog=StudentRegistration;Integrated Security=True";
string sql = "SELECT * FROM StudentInfo";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
DataSet ds = new DataSet();
connection.Open();
dataadapter.Fill(ds, "Student");
connection.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "Student";