By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,717 Members | 2,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,717 IT Pros & Developers. It's quick & easy.

using DataSet programatically without an sql server database

P: n/a
I need to custom build and use a dataset in c# to use with xml. Does anybody
know where I can find out how to do something like this? I was going to
create a class that generated the dataset and its data to hide all of the
complex (or should I say the huge chuncks of code) from the page itself.

Jun 27 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Hi Andy,

I am not quite sure what your requirement is but if you want to populate
your dataset from some XML and not from some database in the SQL Server then
you can create a new dataset in your code in C# and then read the XML file
into that dataset. Please find the code below.

DataSet ds= new DataSet();
ds.ReadXml("path of the XML file");

Regards,
Manish
www.componentone.com
"Andy B" wrote:
I need to custom build and use a dataset in c# to use with xml. Does anybody
know where I can find out how to do something like this? I was going to
create a class that generated the dataset and its data to hide all of the
complex (or should I say the huge chuncks of code) from the page itself.

Jun 27 '08 #2

P: n/a
I am mainly looking for a way to programatically create and populate columns
and rows manually instead of with xml or a database.
"Manish" <Ma****@discussions.microsoft.comwrote in message
news:3F**********************************@microsof t.com...
Hi Andy,

I am not quite sure what your requirement is but if you want to populate
your dataset from some XML and not from some database in the SQL Server
then
you can create a new dataset in your code in C# and then read the XML file
into that dataset. Please find the code below.

DataSet ds= new DataSet();
ds.ReadXml("path of the XML file");

Regards,
Manish
www.componentone.com
"Andy B" wrote:
>I need to custom build and use a dataset in c# to use with xml. Does
anybody
know where I can find out how to do something like this? I was going to
create a class that generated the dataset and its data to hide all of the
complex (or should I say the huge chuncks of code) from the page itself.


Jun 27 '08 #3

P: n/a
"Andy B" <a_*****@sbcglobal.netwrote in message
news:uN**************@TK2MSFTNGP03.phx.gbl...

[top-posting corrected]
>>I need to custom build and use a dataset in c# to use with xml. Does
anybody
know where I can find out how to do something like this? I was going to
create a class that generated the dataset and its data to hide all of
the
complex (or should I say the huge chuncks of code) from the page itself.

I am not quite sure what your requirement is but if you want to populate
your dataset from some XML and not from some database in the SQL Server
then
you can create a new dataset in your code in C# and then read the XML
file
into that dataset. Please find the code below.

DataSet ds= new DataSet();
ds.ReadXml("path of the XML file");

I am mainly looking for a way to programatically create and populate
columns and rows manually instead of with xml or a database.
using (DataTable objDT = new DataTable())
{
objDT.Columns.Add("First", typeof(string));
objDT.Columns.Add("Second", typeof(string));
objDT.Columns.Add("Third", typeof(string));
objDT.Columns.Add("Fourth", typeof(string));
objDT.Rows.Add(new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
objDT.Rows.Add(new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
objDT.Rows.Add(new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
objDT.Rows.Add(new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
MyGridView.DataSource = objDT;
MyGridView.DataBind();
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #4

P: n/a
using (DataTable objDT = new DataTable())
{
objDT.Columns.Add("First", typeof(string));
objDT.Columns.Add("Second", typeof(string));
objDT.Columns.Add("Third", typeof(string));
objDT.Columns.Add("Fourth", typeof(string));
objDT.Rows.Add(new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
objDT.Rows.Add(new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
objDT.Rows.Add(new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
objDT.Rows.Add(new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
MyGridView.DataSource = objDT;
MyGridView.DataBind();
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
1. What is the using for? and
2. Why add more than 1 row in the same position with the same values?

Jun 27 '08 #5

P: n/a
"Andy B" <a_*****@sbcglobal.netwrote in message
news:Oo**************@TK2MSFTNGP02.phx.gbl...
>using (DataTable objDT = new DataTable())
{
objDT.Columns.Add("First", typeof(string));
objDT.Columns.Add("Second", typeof(string));
objDT.Columns.Add("Third", typeof(string));
objDT.Columns.Add("Fourth", typeof(string));
objDT.Rows.Add(new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
objDT.Rows.Add(new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
objDT.Rows.Add(new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
objDT.Rows.Add(new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
MyGridView.DataSource = objDT;
MyGridView.DataBind();
}

1. What is the using for? and
http://msdn.microsoft.com/en-us/library/yh598w02.aspx
2. Why add more than 1 row in the same position with the same values?
Where did I do that...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #6

P: n/a
Then what does

objDT.Rows.Add(new object[4]...

mean? to be more exact... what does object[4] do
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2*******************@TK2MSFTNGP06.phx.gbl...
"Andy B" <a_*****@sbcglobal.netwrote in message
news:Oo**************@TK2MSFTNGP02.phx.gbl...
>>using (DataTable objDT = new DataTable())
{
objDT.Columns.Add("First", typeof(string));
objDT.Columns.Add("Second", typeof(string));
objDT.Columns.Add("Third", typeof(string));
objDT.Columns.Add("Fourth", typeof(string));
objDT.Rows.Add(new object[4] { "R1C1", "R1C2", "R1C3", "R1C4" });
objDT.Rows.Add(new object[4] { "R2C1", "R2C2", "R2C3", "R2C4" });
objDT.Rows.Add(new object[4] { "R3C1", "R3C2", "R3C3", "R3C4" });
objDT.Rows.Add(new object[4] { "R4C1", "R4C2", "R4C3", "R4C4" });
MyGridView.DataSource = objDT;
MyGridView.DataBind();
}

1. What is the using for? and

http://msdn.microsoft.com/en-us/library/yh598w02.aspx
>2. Why add more than 1 row in the same position with the same values?

Where did I do that...?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #7

P: n/a
"Andy B" <a_*****@sbcglobal.netwrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...

[top-posting corrected]
>>2. Why add more than 1 row in the same position with the same values?

Where did I do that...?

Then what does

objDT.Rows.Add(new object[4]...

mean? to be more exact... what does object[4] do
It passes an object array (containing four elements) as the first argument
of the Add method of the datatable's Rows collection...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #8

P: n/a
Is this the only way you can do it? or is there some other way. I used
NewDataTableRow() before... is this possible without sql server?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:Oy**************@TK2MSFTNGP02.phx.gbl...
"Andy B" <a_*****@sbcglobal.netwrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...

[top-posting corrected]
>>>2. Why add more than 1 row in the same position with the same values?

Where did I do that...?

Then what does

objDT.Rows.Add(new object[4]...

mean? to be more exact... what does object[4] do

It passes an object array (containing four elements) as the first argument
of the Add method of the datatable's Rows collection...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #9

P: n/a
"Andy B" <a_*****@sbcglobal.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...

[top-posting corrected again]
>It passes an object array (containing four elements) as the first
argument of the Add method of the datatable's Rows collection...

Is this the only way you can do it? or is there some other way.
Probably - there's usually more than one way to do most things in the .NET
Framework...
I used NewDataTableRow() before... is this possible without sql server?
Sorry, I don't know. I always use the method I outlined...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #10

P: n/a
I saw it possible on an msdn article. Actually I think it was
DataTable.NewRow method home page in its example. Now, I tried to create a
dataset with a table and 2 columns. When I ran the page I got this problem:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 20: DataColumn IDColumn = new DataColumn("ID");
Line 21: IDColumn.DataType = Type.GetType("System.Int32");
Line 22: DataSet.Tables["Table"].Columns.Add(IDColumn);
Line 23: DataColumn ItemColumn = new DataColumn("Item");
Line 24: ItemColumn.DataType = typeof(String);

Source File: C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Contracts\Web Form1.aspx.cs Line: 22

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
Contracts.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Documents
and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Contracts\Web Form1.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender,
EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433;
ASP.NET Version:2.0.50727.1433

How do I fix it? The references to DataType = Type.GetType("System.Int32")
and DataType=Typeof(String) were left that way on purpose for trying to
figure out the problem. I had both of the columns datatype set to
Typeof(...) but changed the IDColumns to Type.GetType(...) when I originally
got this problem. Any idea what the problem is? Is it possible I can't add
columns after the table is added to the dataset?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"Andy B" <a_*****@sbcglobal.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...

[top-posting corrected again]
>>It passes an object array (containing four elements) as the first
argument of the Add method of the datatable's Rows collection...

Is this the only way you can do it? or is there some other way.

Probably - there's usually more than one way to do most things in the .NET
Framework...
>I used NewDataTableRow() before... is this possible without sql server?

Sorry, I don't know. I always use the method I outlined...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #11

P: n/a
Looks like you did not create the table "Table".

At some point you need to have
DataTable dt = new DataTable();
DataSet.Tables["Table"] = dt;

Something like that.

George.

"Andy B" <a_*****@sbcglobal.netwrote in message
news:Oj*************@TK2MSFTNGP05.phx.gbl...
>I saw it possible on an msdn article. Actually I think it was
DataTable.NewRow method home page in its example. Now, I tried to create a
dataset with a table and 2 columns. When I ran the page I got this problem:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 20: DataColumn IDColumn = new DataColumn("ID");
Line 21: IDColumn.DataType = Type.GetType("System.Int32");
Line 22: DataSet.Tables["Table"].Columns.Add(IDColumn);
Line 23: DataColumn ItemColumn = new DataColumn("Item");
Line 24: ItemColumn.DataType = typeof(String);

Source File: C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Contracts\Web Form1.aspx.cs Line:
22

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
Contracts.WebForm1.Page_Load(Object sender, EventArgs e) in C:\Documents
and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Contracts\Web Form1.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender,
EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433;
ASP.NET Version:2.0.50727.1433

How do I fix it? The references to DataType = Type.GetType("System.Int32")
and DataType=Typeof(String) were left that way on purpose for trying to
figure out the problem. I had both of the columns datatype set to
Typeof(...) but changed the IDColumns to Type.GetType(...) when I
originally got this problem. Any idea what the problem is? Is it possible
I can't add columns after the table is added to the dataset?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>"Andy B" <a_*****@sbcglobal.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...

[top-posting corrected again]
>>>It passes an object array (containing four elements) as the first
argument of the Add method of the datatable's Rows collection...

Is this the only way you can do it? or is there some other way.

Probably - there's usually more than one way to do most things in the
.NET Framework...
>>I used NewDataTableRow() before... is this possible without sql server?

Sorry, I don't know. I always use the method I outlined...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net


Jun 27 '08 #12

P: n/a
Actually, I managed to figure out what the problem was. I was looking at the
DataTable.NewRow() method example on msdn and writing sample code of my own
to see if I could get things to work right. Msdn used "Table" for the table
name and I used "Start". Go figure... So by instinct I used "Table" for all
of the references after naming the table something else....
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:uz**************@TK2MSFTNGP05.phx.gbl...
Looks like you did not create the table "Table".

At some point you need to have
DataTable dt = new DataTable();
DataSet.Tables["Table"] = dt;

Something like that.

George.

"Andy B" <a_*****@sbcglobal.netwrote in message
news:Oj*************@TK2MSFTNGP05.phx.gbl...
>>I saw it possible on an msdn article. Actually I think it was
DataTable.NewRow method home page in its example. Now, I tried to create a
dataset with a table and 2 columns. When I ran the page I got this
problem:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

Source Error:

Line 20: DataColumn IDColumn = new DataColumn("ID");
Line 21: IDColumn.DataType = Type.GetType("System.Int32");
Line 22: DataSet.Tables["Table"].Columns.Add(IDColumn);
Line 23: DataColumn ItemColumn = new DataColumn("Item");
Line 24: ItemColumn.DataType = typeof(String);

Source File: C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Contracts\We bForm1.aspx.cs Line:
22

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
Contracts.WebForm1.Page_Load(Object sender, EventArgs e) in
C:\Documents and Settings\Andy\My Documents\Visual Studio
2008\Projects\EternityRecordsWebsite\Contracts\We bForm1.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender,
EventArgs e) +33
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1436

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.1433;
ASP.NET Version:2.0.50727.1433

How do I fix it? The references to DataType =
Type.GetType("System.Int32") and DataType=Typeof(String) were left that
way on purpose for trying to figure out the problem. I had both of the
columns datatype set to Typeof(...) but changed the IDColumns to
Type.GetType(...) when I originally got this problem. Any idea what the
problem is? Is it possible I can't add columns after the table is added
to the dataset?
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>"Andy B" <a_*****@sbcglobal.netwrote in message
news:eK**************@TK2MSFTNGP06.phx.gbl...

[top-posting corrected again]

It passes an object array (containing four elements) as the first
argument of the Add method of the datatable's Rows collection...

Is this the only way you can do it? or is there some other way.

Probably - there's usually more than one way to do most things in the
.NET Framework...

I used NewDataTableRow() before... is this possible without sql server?

Sorry, I don't know. I always use the method I outlined...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net



Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.