Solved

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

Posted on 2004-09-07
6
1,902 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Image(7) 1 53
Learning Processes involved in building a asp.net mvc (C#)  application that uses an Oracle database 8 75
Problem to error 4 46
Angular JS Route 3 54
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…
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.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

20 Experts available now in Live!

Get 1:1 Help Now