Problem importing DataRow[] into DataTable

G

Guest

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
 
G

Guest

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
 
G

Guest

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 said:
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 said:
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
 
G

Guest

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 said:
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 said:
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 said:
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
 
G

Guest

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 said:
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 said:
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 said:
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
 
G

Guest

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 said:
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 said:
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 said:
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




:

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
 
D

Dan Tallent

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 said:
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 said:
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 said:
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




:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top