473,890 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Attempt to write the DataRow content to Text file

308 Contributor

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
(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
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.C opyTo() ) .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.

Nov 23 '09 #1
3 3823
7,872 Recognized Expert Expert
Why are you jumping through all these hoops?
Just loop through your data printing it out (use the .ToString() function)
Nov 23 '09 #2
308 Contributor
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.

Nov 25 '09 #3
9,735 Recognized Expert Moderator Expert
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;
  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

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 problem is that Tomcat saves the files into %CATALINA_HOME%/bin directory. I am wondering why? Why not into my application directory? The application directory is %CATALINA_HOME%/webapps/picture. As a parameter of directory I am giving "." – which...
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 correct, but giving error says " file is accessed by other process cannot access" I have some logic inbetween and write the info using response.write in to html format Ex: Response.Write("<td>" & "client_no" & "</td>"). I am getting the error at :...
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 contains a (1) content type one one line, a (2) filename, and (3) the encoding type (seems to always be Base64, followed by the encoded string. A process will be to read the content type and filename, then create a new binary file and write out the...
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 C:\Inetpub\wwwroot\RunReportASP is the directory of my virtual directory for my app. In it's properties I did check "Write" as a permission. What else do I need to do?
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") xmlhttp.setRequestHeader "Content-type","text/txt" xmlhttp.Open "get", "http://www.mysite.com/default.asp", False xmlhttp.Send text = xmlhttp.responseText dim fs, fname
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 OleDbParameterCollection" whenI try to write a new record. Delete and Modify work fine its just the add record function causes the error.
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 out of that directory(where my application reside). how can i overcome to it. my code is this.. Form to select two files <form method="post" action="" enctype="multipart/form-data">
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
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 are accessed by the main program to provide the system functionality. Using the text editor, the user can: • Input the name of a file • The program will check if a file with this name exists or not. If yes, it will tell the user “This File...
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,...
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...
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...
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,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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...
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();...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.