473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problem importing DataRow[] into DataTable

Hi,

I have a DataTable and I want to get a subset of the rows within it. I use
the Select method to get my subset and the results are in a DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few variations of
the following but no rows are every imported into the datatable:

DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();

foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}

The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?

Thanks in advance,

Pete

Sep 7 '06 #1
6 28285
Pete,
If you get into the habit of wrapping your code in a try / catch block while
developing, and output the exception Message and Stacktrace properties , say
to the output window with Debug.WriteLine, then you will quickly see why this
is happening. It will most likely be an exception message that the rows
already belong to the table. Try / Catch is your friend.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi,

I have a DataTable and I want to get a subset of the rows within it. I use
the Select method to get my subset and the results are in a DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few variations of
the following but no rows are every imported into the datatable:

DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();

foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}

The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?

Thanks in advance,

Pete
Sep 7 '06 #2
Hi Peter,

Thanks for the reply. You are right, try/catch is my friend. But in this
case, it isn't doing much for me. The code executes fine, it never goes into
the catch. My DataTable still doesn't import the row. Any thoughts?

TIA,

Pete

"Peter Bromberg [C# MVP]" wrote:
Pete,
If you get into the habit of wrapping your code in a try / catch block while
developing, and output the exception Message and Stacktrace properties , say
to the output window with Debug.WriteLine, then you will quickly see why this
is happening. It will most likely be an exception message that the rows
already belong to the table. Try / Catch is your friend.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi,

I have a DataTable and I want to get a subset of the rows within it. I use
the Select method to get my subset and the results are in a DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few variations of
the following but no rows are every imported into the datatable:

DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();

foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}

The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?

Thanks in advance,

Pete
Sep 7 '06 #3
Let's illustrate with a short code snippet:

class Program
{
static void Main(string[] args)
{

DataTable dt1 = new DataTable();
dt1.Columns.Add("Name");
DataRow row;
for (int i = 0; i < 10; i++)
{
row = dt1.NewRow();
row["Name"] = i.ToString() + "BLAH";
dt1.Rows.Add(row);
}

DataRow[] datrow = dt1.Select("Name Like '%1%'");
DataTable dt = new DataTable();
foreach (DataRow dr in datrow)
{
dt.ImportRow(dr);
}

Console.WriteLine(dt.Rows.Count.ToString());
Console.ReadLine();

}
}

If you run this, you will see that "dt" ends up with the one row that was
imported.

What this means is that unless your "SELECT" statement comes up with no
rows, it should be working.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi Peter,

Thanks for the reply. You are right, try/catch is my friend. But in this
case, it isn't doing much for me. The code executes fine, it never goes into
the catch. My DataTable still doesn't import the row. Any thoughts?

TIA,

Pete

"Peter Bromberg [C# MVP]" wrote:
Pete,
If you get into the habit of wrapping your code in a try / catch block while
developing, and output the exception Message and Stacktrace properties , say
to the output window with Debug.WriteLine, then you will quickly see why this
is happening. It will most likely be an exception message that the rows
already belong to the table. Try / Catch is your friend.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi,
>
I have a DataTable and I want to get a subset of the rows within it. I use
the Select method to get my subset and the results are in a DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few variations of
the following but no rows are every imported into the datatable:
>
DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();
>
foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}
>
The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?
>
Thanks in advance,
>
Pete
>
Sep 7 '06 #4
P.S. ---
Is "id" an Integer? If so, you need to take id.ToString() to make a proper
select statement.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi Peter,

Thanks for the reply. You are right, try/catch is my friend. But in this
case, it isn't doing much for me. The code executes fine, it never goes into
the catch. My DataTable still doesn't import the row. Any thoughts?

TIA,

Pete

"Peter Bromberg [C# MVP]" wrote:
Pete,
If you get into the habit of wrapping your code in a try / catch block while
developing, and output the exception Message and Stacktrace properties , say
to the output window with Debug.WriteLine, then you will quickly see why this
is happening. It will most likely be an exception message that the rows
already belong to the table. Try / Catch is your friend.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi,
>
I have a DataTable and I want to get a subset of the rows within it. I use
the Select method to get my subset and the results are in a DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few variations of
the following but no rows are every imported into the datatable:
>
DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();
>
foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}
>
The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?
>
Thanks in advance,
>
Pete
>
Sep 7 '06 #5
Turns out the row was in there. I was looking at the wrong Count. Doh! I
did notice that the columns didn't transfer over from the rows. So instead
of:

DataTable dt = new DataTable();

I did:

DataTable dt = ds.Tables[0].Clone();

Which worked fine. Thanks for the help.

Pete
"Peter Bromberg [C# MVP]" wrote:
Let's illustrate with a short code snippet:

class Program
{
static void Main(string[] args)
{

DataTable dt1 = new DataTable();
dt1.Columns.Add("Name");
DataRow row;
for (int i = 0; i < 10; i++)
{
row = dt1.NewRow();
row["Name"] = i.ToString() + "BLAH";
dt1.Rows.Add(row);
}

DataRow[] datrow = dt1.Select("Name Like '%1%'");
DataTable dt = new DataTable();
foreach (DataRow dr in datrow)
{
dt.ImportRow(dr);
}

Console.WriteLine(dt.Rows.Count.ToString());
Console.ReadLine();

}
}

If you run this, you will see that "dt" ends up with the one row that was
imported.

What this means is that unless your "SELECT" statement comes up with no
rows, it should be working.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
Hi Peter,

Thanks for the reply. You are right, try/catch is my friend. But in this
case, it isn't doing much for me. The code executes fine, it never goes into
the catch. My DataTable still doesn't import the row. Any thoughts?

TIA,

Pete

"Peter Bromberg [C# MVP]" wrote:
Pete,
If you get into the habit of wrapping your code in a try / catch block while
developing, and output the exception Message and Stacktrace properties , say
to the output window with Debug.WriteLine, then you will quickly see why this
is happening. It will most likely be an exception message that the rows
already belong to the table. Try / Catch is your friend.
Peter
>
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
>
>
>
>
"Pete Wittig" wrote:
>
Hi,

I have a DataTable and I want to get a subset of the rows within it. I use
the Select method to get my subset and the results are in a DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few variations of
the following but no rows are every imported into the datatable:

DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();

foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}

The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?

Thanks in advance,

Pete
Sep 7 '06 #6
Thanks for this example. I found it very helpful.
I am still learning how to work with the data in C#. This was a nice
clear example.

Thanks!
Dan

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.comwrote in message
news:95**********************************@microsof t.com...
Let's illustrate with a short code snippet:

class Program
{
static void Main(string[] args)
{

DataTable dt1 = new DataTable();
dt1.Columns.Add("Name");
DataRow row;
for (int i = 0; i < 10; i++)
{
row = dt1.NewRow();
row["Name"] = i.ToString() + "BLAH";
dt1.Rows.Add(row);
}

DataRow[] datrow = dt1.Select("Name Like '%1%'");
DataTable dt = new DataTable();
foreach (DataRow dr in datrow)
{
dt.ImportRow(dr);
}

Console.WriteLine(dt.Rows.Count.ToString());
Console.ReadLine();

}
}

If you run this, you will see that "dt" ends up with the one row that was
imported.

What this means is that unless your "SELECT" statement comes up with no
rows, it should be working.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:
>Hi Peter,

Thanks for the reply. You are right, try/catch is my friend. But in this
case, it isn't doing much for me. The code executes fine, it never goes
into
the catch. My DataTable still doesn't import the row. Any thoughts?

TIA,

Pete

"Peter Bromberg [C# MVP]" wrote:
Pete,
If you get into the habit of wrapping your code in a try / catch block
while
developing, and output the exception Message and Stacktrace properties
, say
to the output window with Debug.WriteLine, then you will quickly see
why this
is happening. It will most likely be an exception message that the rows
already belong to the table. Try / Catch is your friend.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Pete Wittig" wrote:

Hi,

I have a DataTable and I want to get a subset of the rows within it.
I use
the Select method to get my subset and the results are in a
DataRow[]. I
want to put those Rows back into a DataTable. I've tried a few
variations of
the following but no rows are every imported into the datatable:

DataRow[] datrow = ds.Tables[0].Select("PersonID = " + id);
DataTable dt = new DataTable();

foreach(DataRow dr in datrow)
{
dt .ImportRow(dr);
}

The DataTable dt always winds up with a count of zero and I know the
DataRow[] datrow has several rows in it. Any thoughts?

Thanks in advance,

Pete

Sep 7 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kelvin | last post by:
Hi All, Due to can't insert new row between row and row while current DataTable looping. In order to solve the problem, I need to clone it as new DataTable, while the DataTable Looping, it also...
3
by: Newbie | last post by:
I am using a Datagrid to show the contents of a DataTable. But it seems like the Datagrid is not getting the contents of the Datatable when the button ( btnAddAnotherLaborCategory) is clicked. ...
0
by: Chris Ericoli | last post by:
Hi, I am working with an 'in session' ado dataset with an asp.net application. My dataset is comprised of two tables, one of which maintains a few calculated datacolumns. For some reason these...
1
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry =...
3
by: Agnes | last post by:
I need to Load 4thousand record into a datagrid for the user to choose. After the user click the row, I will save it. Now, How can I remove the uncheck row. Dim myDelRowArray() As DataRow =...
1
by: BobLaughland | last post by:
What is the best way to import the contents of a CSV file into C# objects? E.G. a CSV file that looks like this a1,b1,c1,d1, a2,b2,c2,d2, a3,b3,c3,d3 Would be loaded up and then turned...
1
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private...
2
by: jereviscious | last post by:
Hi all - Last resort time. I'm importing data from a spreadsheet that I receive from one of my vendor using interop.excel. The date field in excel is entered as 4/7/2006, but when I retrieve...
0
by: Anish G | last post by:
Hi All, I am getting the below given error while running my application in live server. In my local machine, its working fine. Please help me as it is very urgent for me. Exception from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.