Connecting Tech Pros Worldwide Help | Site Map

using DataSet programatically without an sql server database

Andy B
Guest
 
Posts: n/a
#1: Jun 27 '08
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.



=?Utf-8?B?TWFuaXNo?=
Guest
 
Posts: n/a
#2: Jun 27 '08

re: using DataSet programatically without an sql server database


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:
Quote:
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.
>
>
>
>
Andy B
Guest
 
Posts: n/a
#3: Jun 27 '08

re: using DataSet programatically without an sql server database


I am mainly looking for a way to programatically create and populate columns
and rows manually instead of with xml or a database.


"Manish" <Manish@discussions.microsoft.comwrote in message
news:3FB4A54E-96D6-489E-84C4-0741B1C4CE09@microsoft.com...
Quote:
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:
>
Quote:
>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.
>>
>>
>>
>>

Mark Rae [MVP]
Guest
 
Posts: n/a
#4: Jun 27 '08

re: using DataSet programatically without an sql server database


"Andy B" <a_borka@sbcglobal.netwrote in message
news:uN9RlTKvIHA.4912@TK2MSFTNGP03.phx.gbl...

[top-posting corrected]
Quote:
Quote:
Quote:
>>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

Andy B
Guest
 
Posts: n/a
#5: Jun 27 '08

re: using DataSet programatically without an sql server database


using (DataTable objDT = new DataTable())
Quote:
{
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?



Mark Rae [MVP]
Guest
 
Posts: n/a
#6: Jun 27 '08

re: using DataSet programatically without an sql server database


"Andy B" <a_borka@sbcglobal.netwrote in message
news:OoA3t1OvIHA.1236@TK2MSFTNGP02.phx.gbl...
Quote:
Quote:
>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
Quote:
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

Andy B
Guest
 
Posts: n/a
#7: Jun 27 '08

re: using DataSet programatically without an sql server database


Then what does

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

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


"Mark Rae [MVP]" <mark@markNOSPAMrae.netwrote in message
news:%23$Wy%23%23OvIHA.548@TK2MSFTNGP06.phx.gbl...
Quote:
"Andy B" <a_borka@sbcglobal.netwrote in message
news:OoA3t1OvIHA.1236@TK2MSFTNGP02.phx.gbl...
>
Quote:
Quote:
>>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
>
Quote:
>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

Mark Rae [MVP]
Guest
 
Posts: n/a
#8: Jun 27 '08

re: using DataSet programatically without an sql server database


"Andy B" <a_borka@sbcglobal.netwrote in message
news:OemvaQPvIHA.5832@TK2MSFTNGP02.phx.gbl...

[top-posting corrected]
Quote:
Quote:
Quote:
>>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

Andy B
Guest
 
Posts: n/a
#9: Jun 27 '08

re: using DataSet programatically without an sql server database


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]" <mark@markNOSPAMrae.netwrote in message
news:OyG9vaPvIHA.2032@TK2MSFTNGP02.phx.gbl...
Quote:
"Andy B" <a_borka@sbcglobal.netwrote in message
news:OemvaQPvIHA.5832@TK2MSFTNGP02.phx.gbl...
>
[top-posting corrected]
>
Quote:
Quote:
>>>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

Mark Rae [MVP]
Guest
 
Posts: n/a
#10: Jun 27 '08

re: using DataSet programatically without an sql server database


"Andy B" <a_borka@sbcglobal.netwrote in message
news:eKowu6PvIHA.5472@TK2MSFTNGP06.phx.gbl...

[top-posting corrected again]
Quote:
Quote:
>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...
Quote:
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

Andy B
Guest
 
Posts: n/a
#11: Jun 27 '08

re: using DataSet programatically without an sql server database


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]" <mark@markNOSPAMrae.netwrote in message
news:%23oAe3IQvIHA.3384@TK2MSFTNGP03.phx.gbl...
Quote:
"Andy B" <a_borka@sbcglobal.netwrote in message
news:eKowu6PvIHA.5472@TK2MSFTNGP06.phx.gbl...
>
[top-posting corrected again]
>
Quote:
Quote:
>>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...
>
Quote:
>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

George Ter-Saakov
Guest
 
Posts: n/a
#12: Jun 27 '08

re: using DataSet programatically without an sql server database


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_borka@sbcglobal.netwrote in message
news:Ojr5ZaQvIHA.576@TK2MSFTNGP05.phx.gbl...
Quote:
>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]" <mark@markNOSPAMrae.netwrote in message
news:%23oAe3IQvIHA.3384@TK2MSFTNGP03.phx.gbl...
Quote:
>"Andy B" <a_borka@sbcglobal.netwrote in message
>news:eKowu6PvIHA.5472@TK2MSFTNGP06.phx.gbl...
>>
>[top-posting corrected again]
>>
Quote:
>>>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...
>>
Quote:
>>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
>
>

Andy B
Guest
 
Posts: n/a
#13: Jun 27 '08

re: using DataSet programatically without an sql server database


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-nsp@cardone.comwrote in message
news:uzS0HxQvIHA.1504@TK2MSFTNGP05.phx.gbl...
Quote:
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_borka@sbcglobal.netwrote in message
news:Ojr5ZaQvIHA.576@TK2MSFTNGP05.phx.gbl...
Quote:
>>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]" <mark@markNOSPAMrae.netwrote in message
>news:%23oAe3IQvIHA.3384@TK2MSFTNGP03.phx.gbl...
Quote:
>>"Andy B" <a_borka@sbcglobal.netwrote in message
>>news:eKowu6PvIHA.5472@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
>>
>>
>
>

Closed Thread