473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Person ID = " + 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 28328
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("Person ID = " + 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("Person ID = " + 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(ro w);
}

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

Console.WriteLi ne(dt.Rows.Coun t.ToString());
Console.ReadLin e();

}
}

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("Person ID = " + 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("Person ID = " + 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(ro w);
}

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

Console.WriteLi ne(dt.Rows.Coun t.ToString());
Console.ReadLin e();

}
}

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("Person ID = " + 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*******@yaho o.nospammin.com wrote in message
news:95******** *************** ***********@mic rosoft.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(ro w);
}

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

Console.WriteLi ne(dt.Rows.Coun t.ToString());
Console.ReadLin e();

}
}

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("Person ID = " + 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
5832
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 insert both of new row and existing datarow into new DataTable, but I don't know how to do that, Here is my souce code, Please advise. if ( myTable.Rows.Count > 0 )
3
2104
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. private void Page_Load(object sender, System.EventArgs e) { DataTable dataTable; DataColumn dataColumn; DataRow dataRow; dataTable = new DataTable("ratesTable");
0
3169
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 datacolumns do not trigger their expression when other columns from which the expressions are derived are updated. Below is a basic example of what I am doing. User enters values into an asp.net form and clicks a button. Retrieve dataset from...
1
3895
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 = New DirectoryEntry("LDAP://domain") Dim mySearcher As New DirectorySearcher(enTry) Dim resEnt As SearchResult mySearcher.Filter = ("(objectClass=*)") mySearcher.SearchScope = SearchScope.Subtree
3
7160
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 = dtRvDetail.Select("check = false ") Me.dtRvDetail.Rows.Remove(myDelRowArray) <-- I know there is error, since i cannot delete the datarowarray. Any simple and easy way ?? thanks a lot
1
2056
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 into 3 objects
1
3163
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 WithEvent DataTable as New DataTable
2
10060
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 the value, it returns a double 38449.0. I can't figure out how to convert this value into a proper date. I have no Idea what excel is doing with this date. Also, when I enter in 38449 into
0
3008
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 HRESULT: 0x800A03EC 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:...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10090
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.