S
SRLoka
I was getting a "Specified cast is not valid" error when sending to a MSMQ. This happens very occasionally and so cannot reproduce to debug. So I modified my function as shown below.
sb is a private stringbuilder within that class and is not modified by any threads or other processes at same time. Bytes from a network stream are appended to it and then this function is called. Line and Len variables were added just to find the error.
private void ProcessDataReceived()
{
int Line = 0, Len = 0;
string data = "";
try
{
Line++;
if ( sb.Length > 0 )
{
Line++; data = sb.ToString() ;
Len = sb.Length;
Line++; sb.Length = 0 ; // Clear buffer
Line++; Controller.MSMQ.Send(data);
}
}
catch(Exception E)
{
if(sb == null)
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>sb is null");
else if(data == null)
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>data is null");
else
{
if(data.Length > 0) //to see if any wacky characters from Network
{
string s = "";
foreach(char c in data)
{
s += System.Convert.ToInt32(c).ToString() + "**";
}
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>" + Controller.MSMQ.Path + "<BR>sb Len=" + Len.ToString() + "<BR>data =" + s );
}
else
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>" + Controller.MSMQ.Path + "<BR>sb Len=" + Len.ToString() + "<BR>data =" + data);
}
}
}
Here is the error I got emailed
Specified cast is not valid.
Line#: 4
..\Private$\WebTechGPS
sb Len=0
data =
I cannot figure out how this is possible. Line#: 4 implies it went into the if clause and the error was caused by MSMQ.Send. How can Len value be 0 ?
Sorry for the HTML format. Its easy to highlight this way.
Any suggestions appreciated. I am clueless.
Thanks
Srinivas Loka
sb is a private stringbuilder within that class and is not modified by any threads or other processes at same time. Bytes from a network stream are appended to it and then this function is called. Line and Len variables were added just to find the error.
private void ProcessDataReceived()
{
int Line = 0, Len = 0;
string data = "";
try
{
Line++;
if ( sb.Length > 0 )
{
Line++; data = sb.ToString() ;
Len = sb.Length;
Line++; sb.Length = 0 ; // Clear buffer
Line++; Controller.MSMQ.Send(data);
}
}
catch(Exception E)
{
if(sb == null)
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>sb is null");
else if(data == null)
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>data is null");
else
{
if(data.Length > 0) //to see if any wacky characters from Network
{
string s = "";
foreach(char c in data)
{
s += System.Convert.ToInt32(c).ToString() + "**";
}
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>" + Controller.MSMQ.Path + "<BR>sb Len=" + Len.ToString() + "<BR>data =" + s );
}
else
EMailErr("MSMQ Send Error",E.Message.ToString() + "<BR>Line#: " + Line.ToString() + "<BR>" + Controller.MSMQ.Path + "<BR>sb Len=" + Len.ToString() + "<BR>data =" + data);
}
}
}
Here is the error I got emailed
Specified cast is not valid.
Line#: 4
..\Private$\WebTechGPS
sb Len=0
data =
I cannot figure out how this is possible. Line#: 4 implies it went into the if clause and the error was caused by MSMQ.Send. How can Len value be 0 ?
Sorry for the HTML format. Its easy to highlight this way.
Any suggestions appreciated. I am clueless.
Thanks
Srinivas Loka