I have the following application:
namespace WindowsApplication1
{
public delegate Source ReadCusip(Int32 id);
public partial class Form1: Form
{
public static String connectionString;
public Form1()
{
InitializeComponent();
}
private void Form1_Click(object sender, EventArgs e)
{
connectionString = "Data Source=.;Initial Catalog=Scratch;Integrated
Security=True";
// Source test = GetCusip(1);
// DestinationDB.SaveCusip(co
nnectionSt
ring, test);
ReadCusip rc = new ReadCusip(GetCusip);
IAsyncResult iftAR = rc.BeginInvoke(2, new AsyncCallback(SaveCusip), null);
}
public static void SaveCusip(IAsyncResult itfAR)
{
Console.WriteLine("Startin
g SaveCusip");
AsyncResult ar = (AsyncResult) itfAR;
ReadCusip rc = (ReadCusip)ar.AsyncDelegat
e;
Source source = rc.EndInvoke(itfAR);
Console.WriteLine(source);
DestinationDB.SaveCusip(co
nnectionSt
ring, source);
}
public static Source GetCusip(Int32 id)
{
return DestinationDB.GetCusip(con
nectionStr
ing, id);
}
}
}
The DestinationDB source is as follows:
public static Source GetCusip(String connectionString, Int32 id)
{
SqlConnection conn = null;
Source source = new Source();
try
{
conn = new SqlConnection(connectionSt
ring);
List<DBParam> paramList = new List<DBParam>();
paramList.Add(new DBParam("@id", SqlDbType.BigInt, id));
IDataReader reader = DBUtil.RunProcedure(conn, "SCRATCH_GetCusip", paramList);
if (reader.Read())
{
source.Cusip = reader["Cusip"].ToString()
;
source.Name = reader["Name"].ToString();
source.Amount = Double.Parse(reader["Amoun
t"].ToStri
ng());
}
}
catch (Exception sqle)
{
Console.WriteLine(sqle.Mes
sage + "," + sqle.StackTrace);
// EventLogger.LogEvent(Syste
m.Diagnost
ics.EventL
ogEntryTyp
e.Error, sqle.Message, "DestinationDB");
}
finally
{
if (null != conn)
conn.Dispose();
conn = null;
}
return source;
}
When I run the code straight, i.e., not asynchronously, the GetCusip will return the appropriate information. However, when I run this as shown above, I get the following:
"The type initializer for 'System.Data.SqlClient.Sql
Connection
' threw an exception."
when trying to call GetCusip.
Any ideas?
Thanks,
Start Free Trial