using DataSet programatically without an sql server database

  • Thread starter Thread starter Andy B
  • Start date Start date
A

Andy B

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.
 
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
 
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();
}

1. What is the using for? and
2. Why add more than 1 row in the same position with the same values?
 
Then what does

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

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


Mark Rae said:
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...?
 
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 said:
[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...
 
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\WebForm1.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\WebForm1.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(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 said:
[top-posting corrected again]
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...
 
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 said:
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\WebForm1.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\WebForm1.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(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 said:
[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...
 
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 said:
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 said:
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\WebForm1.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\WebForm1.aspx.cs:22
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o,
Object t, EventArgs e) +15
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(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 said:
[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...
 
Back
Top