Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am display data which is coming at COM Port. I am saving that incoming data to a String and then displaying sub string from that string.

I need to save substrings with TimeStamp to a SQL Server Database. What i need to do for it ?



C#
private void DisplayData(MessageType type, string msg)
        {
           richTextBox1.Invoke(new EventHandler(delegate
            {

                string substring = string.Empty;
              if (msg.Length > 54)
                    substring = msg.Substring(54, 3);
                 richTextBox1.AppendText(substring) ;

            }));
        }
        #endregion
Posted
Updated 15-Jan-13 19:03pm
v2
Comments
Sergey Alexandrovich Kryukov 31-Dec-12 2:48am    
Do you use Invoke because you call DisplayData in a separate thread? That would be right thing to do.
So, I don't see the problem. Probably you need to show how you read the data. It's very usual that the length of string is different, but String.Length can also be different...
—SA
Zoltán Zörgő 31-Dec-12 2:49am    
How could we give you an answer without knowing what is the format of your input string, ant what do you want to cut from the string? So, if you can not formulate the logic of it in English, at least give us several examples of the input string and the substring you need.
ontheline89 31-Dec-12 2:53am    
7E 45 00 FF FF 00 01 0A 00 93 00 00 01 00 00 01 00 13 18 75 51 9D 7E
This is a string.
But number of values in strings are changing with time.
How can i set indexes of a string when its indexes are changing with time.
Zoltán Zörgő 31-Dec-12 2:56am    
But what is the logic in it? Is it a sequence, let's say every first starts with 6, every second with 9, every third with 7? Or the substring starting index is constant, only the length of the input varies?
ontheline89 31-Dec-12 3:00am    
The length of a string is varying.
It gives error " Index and length must refer to a location within the string. "

1 solution

All you need to check is change the length checking from 54 to 54+3 57 which make sure's that you have total 57 strings and it executes the parse only when 54 +3 char's are in msg string.

/Edit
was it like this?
C#
if (msg.Length > 57)
           substring = msg.Substring(54, 3); 
it shuld work for sure
 
Share this answer
 
v2
Comments
ontheline89 31-Dec-12 2:56am    
i have checked it, string length is changing.
Most of time it gives about 57 value long string, but after some time, its length reduces.
Zoltán Zörgő 31-Dec-12 2:58am    
So you don't know the logic? What are you trying to achieve with this program anyway?
Jibesh 31-Dec-12 3:01am    
change the code if (msg.Length > 54) to if (msg.Length > 57) and if length below 57 you can discard because system should respond only the valid values not to other junk data.
ontheline89 31-Dec-12 3:03am    
Jibesh i did this. But it still gives error after displaying some values correctly.
Index and length must refer to a location within the string. ERROR
Jibesh 31-Dec-12 3:05am    
can you paste the code.. i guess something you did wrong.

was it like this?
if (msg.Length > 57)
substring = msg.Substring(54, 3); it shuld work for sure

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