473,394 Members | 1,787 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,394 software developers and data experts.

Attempt to write the DataRow content to Text file

308 256MB
Hi,

Objectives is to write the DataTable content to the text file in the format
Expand|Select|Wrap|Line Numbers
  1. Col1  |  Col2 | Col3
  2. ------|-------|---------
  3. Val11 | Val21 | Val31
  4. Val12 | Val22 | Val32
  5. Val13 | Val23 | Val33
  6.  
(Same way as they appear in DataTable)

I tried this by iterating through each Row in the table.While writing the DataRow content to the text file, the code I wrote is as -
Expand|Select|Wrap|Line Numbers
  1. strArray = new string[row.ItemArray.Length];
  2. row.ItemArray.CopyTo(strArray,0); //<---- error occurs here
  3. string rowForTextFile = string.Join("\t", (string[])row.ItemArray);
  4. //functionality to write the the rowForTextFile  to text file
  5.  
But the error incurred @ above italicized line.The error was
"The error was At least one element in the source array could not be cast down to the destination array type"

Later it is occured to me that of the columns Col1,Col2,Col3 - Col3 is integer.Hence the error.

In order to join separate strings (Call to string.Join() ) I need to copy the object array to string array (Call to row.ItemArray.CopyTo() ) .Is there any solution over this..?

I could have achived the result by iterating through Datacolumn of the datarow.. But I believe that will consume time if the rows count to a lacs or two.Hence I tried implementing it this way.

Please provide inputs.

Thanks!
Nov 23 '09 #1
3 3791
Plater
7,872 Expert 4TB
Why are you jumping through all these hoops?
Just loop through your data printing it out (use the .ToString() function)
Nov 23 '09 #2
madankarmukta
308 256MB
@Plater
Hi,
Thanks for the inputs!

Just to make sure, I am jumping through the necessary rows only.I baffled as you directed to use ToString() function.

I believe use of ToString() will not skip iterations through 49-50 fields of a Datarow.Skiping these iterations may improve performance.

Thanks!
Nov 25 '09 #3
Frinavale
9,735 Expert Mod 8TB
You could just loop through the columns in the DataTable that the DataRow belongs to. For each column, grab the item in the DataRow and use the toString() method for each of them to output the value to a TextFile...

Expand|Select|Wrap|Line Numbers
  1. DataTable table = theDataRow.Table;
  2.  
  3. foreach(DataColumn column in table.Columns)
  4. {        
  5. //  theDataRow.Item[column.ColumnName] .......
  6. }
Nov 25 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jajoo | last post by:
Hi everyone, I am trying to send files with multipart/form-date. Everything is ok with the send. But when I am receiving the files I should specify a directory where the files to be saved. The...
0
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
5
by: AC [MVP MCMS] | last post by:
Any pointers on how to (1) read a Base64 encoded string from a text file and (2) write it to a binary file? I have a ton of files that are being generated from a legacy system. Each file...
3
by: David Thielen | last post by:
Hi; I created a virtual directory in IIS 6.0 and my asp.net app runs fine. But when it tries to write a file I get: Access to the path is denied. - C:\Inetpub\wwwroot\RunReportASP\images ...
1
by: cwl | last post by:
I want to get the content of a webpage containing plain text and write the content to a text file. My code looks like this: Set xmlhttp = CreateObject("Microsoft.XMLHTTP")...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
2
kamill
by: kamill | last post by:
i need to write content of one text file into another text file. My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
12
by: lalou89 | last post by:
Develop a simple text editor program. The program will show the user a menu of choices and will act according to his choice. Use functional decomposition to break the system into small functions that...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.