streamReader.ReadLine() returns null
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: streamReader.ReadLine() returns null

  1. #1
    Join Date
    Mar 2010
    Location
    VS2008
    Posts
    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();
    }
    }
    }
    }

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center