Click here to Skip to main content
15,888,008 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi there, i have a problem to display my data in textbox, i use this code
C#
StreamReader file = new StreamReader(@"\Folder\y.dat");

         string line;

         while ((line = file.ReadLine()) != null)
         {
             if (line.Contains("ok"))
             {

             }
             else
             {
                 MessageBox.Show("ok not found);
                 goto z;
             }
             textBox1.Text = line;
         }


this code work good if i created a .txt file. but with .dat ot if i change the .dat to .txt i see nothing in my textbox.
i use compact framwork visual studio 2008 smartdevice application.
please could you tell me how we display data of .dat file in a textbox
thanks
Posted
Updated 13-Jun-16 4:17am
v2
Comments
CHill60 13-Jun-16 8:51am    
What are the contents of the file?
If there is more than one line you are just going to overwrite the textbox text on each loop. And if each line does not contain "ok" you are going to not display anything
Why on earth do you have a goto (and where is the target code for it?)
phil.o 13-Jun-16 9:00am    
A .dat file usually contains binary data, i.e. data that is not directly usable and displayable in a text control.
Without knowing the format of your .dat file; it will be hard to help.
Moreover, what if you started a debug session and see what is going on?
Jammes_Ca 13-Jun-16 9:00am    
hi, the textbox is in multiline, in compcat framwork richtextbox does'nt exist.
i said if the file is in .txt i read all teh data, but if the file is in .dat , i can't read all the data
CHill60 13-Jun-16 9:33am    
It doesn't matter if the textbox is multi-line - you are overwriting the entire text on each loop.
If the .dat file is binary then how can ReadLine work? And it is highly unlikely that the check if(line.Contains("ok")) would ever pass.
Without a sample of the data I can't help further
Jammes_Ca 13-Jun-16 9:59am    
i understand now, the data is a char, so how do i to show all those data in my textbox in assci format and searching for ok,

Changing the file extension doesn't do anything to the file content at all. If you change the file extension and suddenly your code can't open the file, did you make the mistake of hard coding the filename in your code and not change the extension in the code?

It sounds like you're trying to read a file and display it like in a hex editor? If so, you cannot use a StreamReader. It treats all files as text files and that's just not the case for all files. "ReadLine" will not work with anything other than a text file. You have to use a FileStream instead. That treats all files the same and you can use the Read methods to get the byte data.
 
Share this answer
 
Comments
Jammes_Ca 13-Jun-16 10:18am    
thanks, could you give me an exemple, in my data there number and i want to search fot those numbers. for example i want to look id the number 2014 exist in my dat file
Dave Kreskowiak 13-Jun-16 10:30am    
It's not as easy as that. Is this a text file or a binary file?
Sergey Alexandrovich Kryukov 13-Jun-16 15:14pm    
Good points, a 5.
—SA
Firstly you need to know what the .dat file contains, and how that data is structured. You cannot just assume it contains lines of text unless you have verified it. Secondly, if it contains more than one line then you need to concatenate all the text together using a StringBuilder or similar construct. When you have read all the text then you can display it in your text box. Of course, if the contents of the file are not simple lines of text then you need to use some transform process first, in order to make it readable.
 
Share this answer
 
Comments
Jammes_Ca 13-Jun-16 10:28am    
my .dat file contain chars.
here is a screen shot of my file http://postimg.org/image/qj21i7tef/
Richard MacCutchan 13-Jun-16 10:36am    
Then you need to find out how all that data is structured and what it is supposed to mean. And the only people who can give you that information are the ones who created the file.
Jammes_Ca 13-Jun-16 10:40am    
i have an information, befero every number "2129" hase owne data so each number have the same number of data and the data equale 52 byte. me i want juste to display all those data in my textbox. after i i look for a number i have to grab all those 52 bytes
Richard MacCutchan 13-Jun-16 10:45am    
Then you need to read each 52-byte block, extract the number and add it to your display. See https://msdn.microsoft.com/en-us/library/system.io.binaryreader.readbytes(v=vs.110).aspx.
Jammes_Ca 13-Jun-16 10:49am    
first of all, how do i look if a number exist ???, for example the number 2036
please help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900