K
ken yup
Guest
i tried to log the C# program exception to sql server,logic is very simply ,i try to call a function when the exception occurs,but it failed.
try
{
//do sth;
}
catch(exception ex)
{
logtheexp(theip, theclino, ex);//call the log exception function
}
public void logtheexp(string theip,string theclino,string expdetail)
{
if (theip == null)
theip = "";
if (theclino == null)
theclino = "";
string connstring = @"server =192.168.0.25;database=db;uid=sa;pwd=";
string sqlquery = @"insert cli_errlog (cli_jf_username,cli_jf_ip,exp_detail,err_date) values (@myclino,@myip,@myexpdetail,getdate()) ";
SqlConnection conn = null;
try
{
conn.ConnectionString = connstring;
conn.Open();
SqlCommand cmdqry = conn.CreateCommand();
cmdqry.CommandText = sqlquery;
cmdqry.Parameters.Clear();
cmdqry.Parameters.Add("@myclino", SqlDbType.Char, 5);
cmdqry.Parameters.Add("@myip", SqlDbType.Char, 20);
cmdqry.Parameters.Add("@myexpdetail", SqlDbType.Char, 1000);
cmdqry.Parameters["@myclino"].Value = theclino;
cmdqry.Parameters["@myip"].Value = theip;
cmdqry.Parameters["@myexpdetail"].Value = expdetail;
cmdqry.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception("exception occurs when logging,exception message:"+ex.ToString());
}
finally
{
if (conn!=null)
conn.Close();
}
}
ArgumentNullException occurs in conn.ConnectionString = connstring;
many thanks
http://msdn.microsoft.com/en-us/library/system.argumentnullexception.aspx
Continue reading...
try
{
//do sth;
}
catch(exception ex)
{
logtheexp(theip, theclino, ex);//call the log exception function
}
public void logtheexp(string theip,string theclino,string expdetail)
{
if (theip == null)
theip = "";
if (theclino == null)
theclino = "";
string connstring = @"server =192.168.0.25;database=db;uid=sa;pwd=";
string sqlquery = @"insert cli_errlog (cli_jf_username,cli_jf_ip,exp_detail,err_date) values (@myclino,@myip,@myexpdetail,getdate()) ";
SqlConnection conn = null;
try
{
conn.ConnectionString = connstring;
conn.Open();
SqlCommand cmdqry = conn.CreateCommand();
cmdqry.CommandText = sqlquery;
cmdqry.Parameters.Clear();
cmdqry.Parameters.Add("@myclino", SqlDbType.Char, 5);
cmdqry.Parameters.Add("@myip", SqlDbType.Char, 20);
cmdqry.Parameters.Add("@myexpdetail", SqlDbType.Char, 1000);
cmdqry.Parameters["@myclino"].Value = theclino;
cmdqry.Parameters["@myip"].Value = theip;
cmdqry.Parameters["@myexpdetail"].Value = expdetail;
cmdqry.ExecuteNonQuery();
}
catch(Exception ex)
{
throw new Exception("exception occurs when logging,exception message:"+ex.ToString());
}
finally
{
if (conn!=null)
conn.Close();
}
}
ArgumentNullException occurs in conn.ConnectionString = connstring;
many thanks
http://msdn.microsoft.com/en-us/library/system.argumentnullexception.aspx
Continue reading...