Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# ImportRow for datatable losing data

Posted on 2010-11-08
6
Medium Priority
?
1,648 Views
Last Modified: 2012-05-10
i use the importrow to copy rows from one datatable to another. It was working fine then i added a few more fields to the stored procedure that populates then one datatable. Now i'm missing two columns after the import. I've debugged it and the data is def there from the sql server it, the fields go null after import. I checked and all the field names match up. I'm not sure what else could be wrong. any insight would be appreciated.
foreach (DataRow mebselrow in ds.Tables[0].Rows)
                 {
           mebselrow["Source"] = source;    
           member.ImportRow(mebselrow);
                  }

Open in new window

0
Comment
Question by:national_fulfillment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:abdkhlaif
ID: 34084394
is this an application under development or a running application that you are trying to debug?
in other words, was your application running when you modified the stored procedure? if so, just restart it and the import will be ok.
0
 
LVL 5

Expert Comment

by:JayFromPep
ID: 34084397
Send us a screenshot of the tables from the designer.
0
 

Author Comment

by:national_fulfillment
ID: 34084489
the sql input is a stored procedure (below)  and no this isn't a production app yet.

CREATE PROCEDURE [MemberExport]
@key1 nvarchar(2),
@key2 nvarchar(2)
as
          SELECT     tblKeyDescription.KeyDescription, 'source' as Source,'catdrop' as CatDrop,tblListSelection.MembNumb, Cast(Cast(Year( tblListSelection.orderdate) As varchar(4)) +
      Right('0' + Cast(Month( tblListSelection.orderdate) as varchar(2)), 2) +
      Right('0' + Cast(Day( tblListSelection.orderdate) as varchar(2)), 2) as Int)
 as ROrdDate , MemSql.FirstName, MemSql.LastName, MemSql.Company, MemSql.Opt, MemSql.Address,
                      MemSql.City, MemSql.State, MemSql.Zip, MemSql.Country, MemSql.Phone
FROM         tblListSelection INNER JOIN
                      tblKeyDescription ON tblListSelection.Key1 = tblKeyDescription.Key1 INNER JOIN
                      MemSql ON tblListSelection.MembNumb = MemSql.Membnum
where tblListSelection.key1=@key1 and tblListSelection.key2=@key2 and supress='PULL'
GO

 // this is the datatable 

 DataTable member = new DataTable();

            DataColumn Description = new DataColumn();
            Description.ColumnName = "KeyDescription";
            member.Columns.Add(Description);


            DataColumn Source = new DataColumn();
            Description.ColumnName = "Source";
            member.Columns.Add(Source);

            DataColumn Catdrop = new DataColumn();
            Catdrop.ColumnName = "CatDrop";
            member.Columns.Add(Catdrop);

            DataColumn MembNumb = new DataColumn();
            MembNumb.ColumnName = "MembNumb";
            member.Columns.Add(MembNumb);            

            DataColumn OrderDate = new DataColumn();
            MembNumb.ColumnName = "ROrdDate";
            member.Columns.Add(OrderDate);

            DataColumn Fname = new DataColumn();
            Fname.ColumnName = "FirstName";
            member.Columns.Add(Fname);

            DataColumn LName = new DataColumn();
            LName.ColumnName = "LastName";
            member.Columns.Add(LName);

            DataColumn Company = new DataColumn();
            Company.ColumnName = "Company";
            member.Columns.Add(Company);

            DataColumn Opt= new DataColumn();
            Opt.ColumnName = "Opt";
            member.Columns.Add(Opt);

            DataColumn Address = new DataColumn();
            Address.ColumnName = "Address";
            member.Columns.Add(Address);

            DataColumn City = new DataColumn();
            City.ColumnName = "City";
            member.Columns.Add(City);

            DataColumn State = new DataColumn();
            State.ColumnName = "State";
            member.Columns.Add(State);

            DataColumn Zip = new DataColumn();
            Zip.ColumnName = "Zip";
            member.Columns.Add(Zip);

            DataColumn Country = new DataColumn();
            Country.ColumnName = "Country";
            member.Columns.Add(Country);

            DataColumn Phone = new DataColumn();
            Phone.ColumnName = "Phone";
            member.Columns.Add(Phone);

Open in new window

0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 3

Accepted Solution

by:
abdkhlaif earned 1600 total points
ID: 34084889
ImportRow method works for columns that exist on both tables. Having missing columns means that the table that you are copying to is missing these columns.

You can use DataTable.Clone() method to copy the structure without the data. Then you can add/remove columns from/to the target table to suit your needs. Then you can use ImportRow to import specific or all rows from source table to target table:
DataTable dtSource = new DataTable();
dtSource.Columns.Add("Name");
dtSource.Columns.Add("Age");
// ... add rows here for testing
DataTable dtTarget = dtSource.Clone();
dtTarget.Columns.Add("Address");
dtTarget.Columns.Remove("Age");

foreach (DataRow dr in dtSource.Rows)
	dtTarget.ImportRow(dr); // this will import 'Name' column only

Open in new window

0
 
LVL 10

Assisted Solution

by:Nash2334
Nash2334 earned 400 total points
ID: 34086076
I've had some funny results using ImportRow in the past and have used the "ItemArray" property of the rows instead with better results:

MyDataRow myRow = myDataTable.NewMyDataRow();
myRow.BeginEdit();
myRow.ItemArray = myCloneSourceRow.ItemArray;
myRow.EndEdit();

myDataTable.BeginLoadData();
myDataTable.AddMyDataRow(myRow);
myDataTable.EndLoadData();
0
 

Author Closing Comment

by:national_fulfillment
ID: 34086498
the problem ended up being a mistake with copy/paste

            DataColumn Description = new DataColumn();
            Description.ColumnName = "KeyDescription";
            member.Columns.Add(Description);

 (below should really be Source.Column not Description like the previous Column, so in reality my columns didn't match between the two tables) it wasn't jumping out at me cause i kept focusing on the names of the columns, not that i had flipped columns.
Thanks for everyones quick responses.


            DataColumn Source = new DataColumn();
           Description.ColumnName = "Source";
       member.Columns.Add(Source);

and the proposed alternate solution to use itemarray did work i just like having less lines of code to manage.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question