Reputation: 383
I have used the example from MSDN for adding a new record to my DataSet. I have even used the same variable names to keep it simple.
C# Code
NorthwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();
newCustomersRow.CustomerID = "5";
newCustomersRow.CompanyName = "Alfreds Futterkiste";
northwindDataSet1.Customers.Rows.Add(newCustomersRow);
The error I am getting is The name 'northwindDataSet1' does not exist in the current context
I find this odd as I am using the code straight from MSDN.
My DataSet is called NorthwindDataSet, the table is called Customers. I have tried northwindDataSet
but still the same error.
Upvotes: 0
Views: 693
Reputation: 4963
Define it first like
NorthwindDataSet northwindDataSet1 = new NorthwindDataSet();
NorthwindDataSet.CustomersRow newCustomersRow =
northwindDataSet1.Customers.NewCustomersRow();
newCustomersRow.CustomerID = "5";
newCustomersRow.CompanyName = "Alfreds Futterkiste";
northwindDataSet1.Customers.Rows.Add(newCustomersRow);
Upvotes: 0
Reputation: 1502696
The code example snippet in MSDN isn't designed to be complete. You need a variable called northwindDataSet1
in order to use this snippet.
For example, you could just use:
NorthwindDataSet northwindDataSet1 = new NorthwindDataSet();
... although more likely you'd want to fetch it from a database with a data adapter, or some such.
It's important that you try to really understand the code being presented. Even if you're new to typed data sets, it should be clear that this is trying to use an existing variable - which means that in order to use the code, you have to have that variable.
If you're sufficiently new to C# that you don't understand the syntax being used here (and there's nothing wrong with being new, of course) I would suggest that you start off learning the basics of C# before you move onto database access. That way when you're learning about more advanced topics, you'll be in a better position. Learning one thing at a time is much more efficient than trying to learn everything in one go.
My DataSet is called NorthwindDataSet
What exactly do you mean by that? Do you mean your data set type, or do you have a property called NorthwindDataSet
somewhere? Basically something needs to create an instance of the data set type at some point... it's not clear how far you've got with that.
Upvotes: 4