-
March 23rd, 2010, 04:09 PM
#1
streamReader.ReadLine() returns null
Hello all,
Need help with streamReader... I've written small C# program to read tcp port to capture scale weight from vendor... My problem is program works but after 10 minutes (consistently every 10 minutes), the string value is null causing the program to jump to catch block and exit out of loop... How do I handle null returned in the string??
I have tried this statement:
while ((outputString = streamReader.ReadLine()) != null)
but it hangs and doesn't continue to read...
Thanks in advance...
Here is current partial code:
// Setup stream to read socket data
NetworkStream networkStream = socketForServer.GetStream();
System.IO.StreamReader streamReader = new System.IO.StreamReader(networkStream);
// Setup connection to database and open the connection
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["CnStr"].ToString());
cn.Open();
// Set error variable
Boolean _err = false;
// Process data from socket
while (true)
{
// Try to process data
try
{
// read the data from the host
string outputString;
outputString = streamReader.ReadLine();
// Write data to command window
Console.WriteLine(outputString);
// Split data into an array
string[] a = new string[9];
if (outputString.Contains("|"))
{
a = outputString.Split('|');
// Fix station number, remove when fixed by vendor
if (a[1] == "0")
{
a[1] = WS;
}
// Setup SQL command to process
SqlCommand cmd = new SqlCommand();
string cmdStr = @"Insert into SocketScaleData(RecType, ScaleNo, TimeStamp, Accepted, MaterialNo, Weight, LowerLimit, UpperLimit, TareWeight) values('" +
a[0] + "','"
+ a[1] + "','"
+ a[2] + "','"
+ a[3] + "','"
+ a[4] + "','"
+ a[5] + "','"
+ a[6] + "','"
+ a[7] + "','"
+ a[8] + "')";
cmd.CommandText = cmdStr;
// Open connection if closed
if (cn.State != System.Data.ConnectionState.Open)
{
cn.Open();
}
// Process SQL command
cmd.Connection = cn;
cmd.ExecuteNonQuery();
}
}
catch (Exception Ex)
{
// Write error log
_err = true;
//Console.WriteLine("Lost port connection: automatically retrying: " + IPAddress + ":23");
//Console.WriteLine("");
//WriteToErrorLog(Ex.Message, Ex.StackTrace.ToString(), "Error");
// Write email
//string FromEmail = System.Configuration.ConfigurationManager.AppSettings.Get("FromEmailID").ToString();
//string ToEmail = System.Configuration.ConfigurationManager.AppSettings.Get("ToEmailID").ToString();
//Mail.SendMail(FromEmail, ToEmail, "Socket Scale Data Error - " + IPAddress, Ex.Message.ToString() + "\r\n Stack Trace: " + Ex.StackTrace.ToString());
// Exit while loop
break;
//Console.WriteLine("Exception:" + Ex.Message.ToString());
//Console.ReadLine();
}
finally
{
// Close all connections for restart
if (_err == true)
{
cn.Close();
networkStream.Close();
socketForServer.Close();
}
}
}
}
-
March 24th, 2010, 02:59 AM
#2
Re: streamReader.ReadLine() returns null
With simple combination of if and String.IsNullOrEmpty().
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|