Solved

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

Posted on 2004-09-07
6
1,828 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
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now