0
string sel = "select * from PUTIN";
DataSet ds = new DataSet();
DataTable dt = ds.Tables["put"];
DataRow row = dt.NewRow();

This is the code. When I run the line DataRow row = dt.NewRow(); I get an exception:

Object reference not set to an instance of an object

I find dt is null, why and how to slove it?

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • 7
    well, you just created an empty dataset, how do you expect it to contain the "put"-table? – Breeze Mar 04 '16 at 12:41

1 Answers1

2

Your DataSet is empty currently. You need to use a SqlDataAdapter here to fill your DataSet. Like this:

string sel = "select * from PUTIN";
SqlDataAdapter da = new SqlDataAdapter(sel,connection);
DataSet ds = new DataSet();
da.Fill(ds,"put");
DataTable dt = ds.Tables["put"];
DataRow row = dt.NewRow();
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109