Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

C#: Value cannot be null, Parameter name 'dataset'

I keep recieving an error: when I click on my "Update" button. So, I'm guessing the DataSet (ds) in the "Click Event" method dosen't have any data in it. The datagrid Does fill via the Load Method though.....

Q. Can you see the problem????????

private void Form1_Load(object sender, System.EventArgs e)
{
try
{
string SQL = " SELECT col1, col2 FROM table1 ";
SqlConnection cn = new SqlConnection("integrated security=SSPI;data source=local; persist security info=False;initial catalog=master ");
SqlDataAdapter da = new SqlDataAdapter(SQL,cn);
            
DataSet ds = new DataSet();
da.Fill(ds, "table1");

dataGrid1.DataSource = ds;
dataGrid1.SetDataBinding(ds, "table1");

}
catch(Exception er)
{
MessageBox.Show(er.ToString());
}
}
      

private void button1_Click(object sender, System.EventArgs e)
{
string SQL = " SELECT * FROM table1 ";
SqlConnection cn = new SqlConnection("integrated security=SSPI;data source=local; persist security info=False;initial catalog=master ");
SqlDataAdapter da = new SqlDataAdapter(SQL,cn);

da.Update(ds,"table1");

}
}
}
ASKER CERTIFIED SOLUTION
Avatar of seeflat
seeflat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 123654789987
123654789987

Where are u opening the connection. U have to open the connection

cn.Open();
da.Connection = cn;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kvnsdr

ASKER

Upon your advice to drop the DataSet to ds I was able to get much further. However I then recieved errors concerning inadequate 'update' statement . So upon further study of SQLCommandBuilder, here is the final working code..... Take note, this may help you some day as well. Also, I really appreciate all your help, thank you....


private void button1_Click(object sender, System.EventArgs e)
{
string SQL = " SELECT * FROM table1 ";
SqlConnection cn = new SqlConnection("integrated security=SSPI;data source=local; persist security info=False;initial catalog=master ");
SqlDataAdapter da = new SqlDataAdapter(SQL,cn);

SqlCommandBuilder cb = new SqlCommandBuilder(da);

da.Update(ds,"table1");
ds.AcceptChanges();

}
}
}