Solved

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

Posted on 2004-09-07
6
2,176 Views
Last Modified: 2012-05-05
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");

}
}
}
0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 5

Accepted Solution

by:
seeflat earned 32 total points
ID: 12002094
I'm assuming that you have created a dataset object outside the context of the formload function. If my assumption is correct, then you need to change the line in the formload function from:
DataSet ds= new DataSet();
to:
ds = new DataSet();

seeflat.
0
 
LVL 15

Assisted Solution

by:Thogek
Thogek earned 62 total points
ID: 12002409
I think this is what seeflat is getting at....
You declared your ds variable inside the Form1_Load method.  This means that as soon as button1_Click finishes, that variable is gone.  If you want your "new DataSet()" to populate an existing class variable (declared outside the methods) so that the value will persist and other methods can use it, remove the leading DataSet declaration from the "DataSet ds = new DataSet();" in the Form1_Load method (and make sure there's a class-level "DataSet ds;" declaration outside the methods).
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12003522
Where are u opening the connection. U have to open the connection

cn.Open();
da.Connection = cn;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Assisted Solution

by:Thogek
Thogek earned 62 total points
ID: 12003591
123654789987,
The SqlDataAdapter.Fill method automatically handles opening and closing the provided SqlConnection, so the SqlConnection needs only to be correctly configured (not opened) before the SqlDataAdapter.Fill method is called.
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatacommondbdataadapterclassfilltopic3.asp
0
 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 31 total points
ID: 12004016
In the Form1_Load method change the lines:

DataSet ds = new DataSet();
da.Fill(ds, "table1");

to

ds = new DataSet();
da.Fill(ds, "table1");
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12006489
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();

}
}
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question