Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-09-07
6
Medium Priority
?
2,365 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 128 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 248 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Assisted Solution

by:Thogek
Thogek earned 248 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 124 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

596 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