Re: SqlException deserialization
From: DougBurke (douglas_burke_at_hotmail.com)
Date: 03/24/04
- Next message: Jay B. Harlow [MVP - Outlook]: "Re: Occasional SQL Error"
- Previous message: Girish: "XML metadata?"
- In reply to: William Ryan eMVP: "Re: SqlException deserialization"
- Messages sorted by: [ date ] [ thread ]
Date: 23 Mar 2004 16:24:18 -0800
Thanks for the reply William.
Here is some sample code. Note that the user or the password or the
InitialCatalog are invalid so a login exception is thrown. Other type
of sqlexceptions (such as execution exceptions) will not give the same
behaviour with the stack trace, only this type.
This is a console application in c#.
Regards and Thanks
Doug Burke
------------------------
Code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace SqlExcepConsole
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
using (SqlConnection connection = new SqlConnection("user
id=user;password=;initial catalog=eRStdv99;data
source=melwks209;Connect Timeout=30;Application
Name=eRStd.WorkFlow;"))
{
// this should throw a sqlexception
connection.Open();
// break if we get here
System.Diagnostics.Debugger.Break();
}
}
catch(SqlException exThrown)
{
SerializeToFile(exThrown, @"C:\sqlexcep_before.txt");
Console.WriteLine("The stacktrace before serialisation is: {0}",
exThrown.StackTrace);
SqlException exDeserialized =
DeserializeFromFile(@"C:\sqlexcep_before.txt");
Console.WriteLine("\nThe stacktrace after serial/deserial is:
{0}", exDeserialized.StackTrace);
SerializeToFile(exDeserialized, @"C:\sqlexcep_after.txt");
}
}
private static SqlException DeserializeFromFile(string path)
{
SqlException sqlEx = null;
using(FileStream fs = new FileStream(path, FileMode.Open ) )
{
fs.Position = 0;
SoapFormatter sf = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
sqlEx = (SqlException)sf.Deserialize(fs);
fs.Close();
}
return sqlEx;
}
private static void SerializeToFile(SqlException ex, string path)
{
using ( FileStream fs = new FileStream(path, FileMode.Create ) )
{
System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf =
new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
sf.Serialize(fs, ex);
fs.Close();
}
}
}
}
------------
Output from screen:
The stacktrace before serialisation is: at
System.Data.SqlClient.ConnectionPo
ol.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConn
ectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at SqlExcepConsole.Class1.Main(String[] args) in c:\documents and
settings\bu
rked\my documents\visual studio
projects\sqlexcepconsole\class1.cs:line 19
The stacktrace after serial/deserial is:
---------------
Files serialized and deserialised I can send on request, but execute
the code
"William Ryan eMVP" <bill@NoSp4m.devbuzz.com> wrote in message news:<#TeEGdKEEHA.3568@tk2msftngp13.phx.gbl>...
> Doug:
>
> I've used the SoapFormatter extensively with SQLException(s) and Custom ones
> as well. Never a problem Can you post the serialization code (I'm assuming
> this is the problem b/c if you write it to a file you can verify it there
> without deserialization).
>
> Cheers,
>
> Bill
> "DougBurke" <douglas_burke@hotmail.com> wrote in message
> news:5bd45dd2.0403221804.6fa5a391@posting.google.com...
> > I'm having a problem with a SqlException that I am serializing and
> > deserializing. I do not know if this is the correct place to post,
> > but I thought someone might know.
> >
> > A particular error that occurs, if the connection string does not pass
> > correct login details, is a sql exception with the following message:
> > "Login failed for user 'dbuser'." This exception is of type
> > System.Data.SqlClient.SqlException. This exception has a normal
> > StackTrace property.
> >
> > I serialize the exception, either with a binaryformatter or a
> > soapformatter and then deserialize and the resulting SqlException has
> > a StackTrace of null (I have verified this by re-serializing and
> > inspecting the serialized exception). The problem is in the
> > deserialization, since the first serialized exception (by inspection)
> > has the stacktrace string contained.
> >
> > Has anyone seen this behaviour and is it a bug or am I missing
> > something? In other words, should the stack trace disappear?
> >
> > Doug
> >
> > As an aside, the stack trace only disappears for this specific
> > exception... a different sql exception (with incorrect command sytax
> > for example) does not behave this way.
- Next message: Jay B. Harlow [MVP - Outlook]: "Re: Occasional SQL Error"
- Previous message: Girish: "XML metadata?"
- In reply to: William Ryan eMVP: "Re: SqlException deserialization"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|