looking for remarks on sqlserver code
I haven't used SQLClient yet so this the first code I've written for it and I'm wondering if all the try catch block are REALLY needed and if the failed bool is a good idea. I plan to just throw the exception into an exception declared with the failed bool and use the last finally block to track the error in a log somewhere. This code is called in the Global.asax of a website so I don't want to display an error just track it.
PHP Code:
namespace PKPromo.Helpers.Data
{
/// <summary>
/// DataHandler Class for the UsersOnline Session Information Table.
/// </summary>
public class UsersOnline
{
public static void InitSession()
{
bool failed = true;
try
{ //Attempt to create Session refreance and SqlClient Tools
//Where the hell on the website are we
string sessionID = System.Web.HttpContext.Current.Session.SessionID; //grab our SeesionID
//Where does data come from mommy
string connStr = SQLServer.ConnectionString; //cache connection string
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UserSession_Init", conn);
try
{ //Attempt access database and initialize UserSession
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("nSessionID",System.Data.SqlDbType.VarChar, 400);
cmd.Parameters["nSessionID"].Value = sessionID;
int afct = cmd.ExecuteNonQuery();
if(afct > 0){failed = false;}
}
catch(System.Exception ex)
{
failed = true;
//track error later
}
finally
{
sessionID = null;
cmd.Dispose();
conn.Dispose();
}
}
catch(System.Exception ex)
{
failed = true;
//track error later
}
finally
{
if(failed)
{
//track error later
}
}
}
public static void DropSession()
{
bool failed = true;
try
{ //Attempt to create Session refreance and SqlClient Tools
//Where the hell on the website are we
string sessionID = System.Web.HttpContext.Current.Session.SessionID; //grab our SeesionID
//Where does data come from mommy
string connStr = SQLServer.ConnectionString; //cache connection string
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UserSession_Finalize", conn);
try
{ //Attempt access database and initialize UserSession
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("nSessionID",System.Data.SqlDbType.VarChar, 400);
cmd.Parameters["nSessionID"].Value = sessionID;
int afct = cmd.ExecuteNonQuery();
if(afct > 0){failed = false;}
}
catch(System.Exception ex)
{
failed = true;
//track error later
}
finally
{
sessionID = null;
cmd.Dispose();
conn.Dispose();
}
}
catch(System.Exception ex)
{
failed = true;
//track error later
}
finally
{
if(failed)
{
//track error later
}
}
}
}
}
I know you guys aren't going to belive this but there were errors
So here is the working code. Look good?
PHP Code:
namespace PKPromo.Helpers.Data
{
/// <summary>
/// DataHandler Class for the UsersOnline Session Information Table.
/// </summary>
public class UsersOnline
{
public static void InitSession()
{
bool failed = true;
System.Exception _ex = new System.Exception();;
try
{ //Attempt to create Session refreance and SqlClient Tools
//Where the hell on the website are we
string sessionID = System.Web.HttpContext.Current.Session.SessionID; //grab our SeesionID
//Where does data come from mommy
string connStr = SQLServer.ConnectionString; //cache connection string
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UserSession_Init", conn);
try
{ //Attempt access database and initialize UserSession
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@nSessionID",System.Data.SqlDbType.VarChar, 400);
cmd.Parameters["@nSessionID"].Value = sessionID;
int afct = cmd.ExecuteNonQuery();
if(afct > 0){failed = false;}
}
catch(System.Exception ex)
{
failed = true;
_ex = ex;
//track error later
}
finally
{
sessionID = null;
cmd.Dispose();
conn.Dispose();
}
}
catch(System.Exception ex)
{
failed = true;
_ex = ex;
//track error later
}
finally
{
if(failed)
{ //ok it's later
System.Diagnostics.Debug.Write(_ex.Message + "\r\n" + _ex.Source + "\r\n" + _ex.StackTrace);
}
}
}
public static void DropSession()
{
bool failed = true;
System.Exception _ex = new System.Exception();;
try
{ //Attempt to create Session refreance and SqlClient Tools
//Where the hell on the website are we
string sessionID = System.Web.HttpContext.Current.Session.SessionID; //grab our SeesionID
//Where does data come from mommy
string connStr = SQLServer.ConnectionString; //cache connection string
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UserSession_Finalize", conn);
try
{ //Attempt access database and initialize UserSession
conn.Open();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@nSessionID",System.Data.SqlDbType.VarChar, 400);
cmd.Parameters["@nSessionID"].Value = sessionID;
int afct = cmd.ExecuteNonQuery();
if(afct > 0){failed = false;}
}
catch(System.Exception ex)
{
failed = true;
_ex = ex;
//track error later
}
finally
{
sessionID = null;
cmd.Dispose();
conn.Dispose();
}
}
catch(System.Exception ex)
{
failed = true;
_ex = ex;
//track error later
}
finally
{
if(failed)
{ //ok it's later
System.Diagnostics.Debug.Write(_ex.Message + "\r\n" + _ex.Source + "\r\n" + _ex.StackTrace);
}
}
}
}
}