468,107 Members | 1,347 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

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 27973
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kelvin | last post: by
3 posts views Thread by Newbie | last post: by
reply views Thread by Chris Ericoli | last post: by
1 post views Thread by tangus via DotNetMonster.com | last post: by
3 posts views Thread by Agnes | last post: by
1 post views Thread by BobLaughland | last post: by
1 post views Thread by sapkal.manish | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.