How is this possible

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
 
A

Alan Pretre

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
 
A

Alan Pretre

Sorry for the previous empty post. I pressed the wrong key. GRRR.


Why not set a breakpoint in the exception handler? When you hit it, move the execution point back up again and re-execute the Send().

-- Alan
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
 
S

Srinivas R. Loka

The problem is, there is no way no to know when the error will occur. I just get one or two errors a week. The process runs 24/7 and handles 1000s of messages a day. So I cannot debug. I do not know what is causing the error and the unexplainable behavior.

Thanks
Sorry for the previous empty post. I pressed the wrong key. GRRR.


Why not set a breakpoint in the exception handler? When you hit it, move the execution point back up again and re-execute the Send().

-- Alan
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top