Solved

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

Posted on 2004-09-07
6
2,120 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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