Greetings!!
Having just resolved a query, I had a look at the code to see what I could learn from the exercise and realise that I am not clear on where to declare/instyantiate my objects. Below are two examples, marked CORRECT & WRONG (as I understand them), together with a comment on execution.
WRONG:
protected SqlConnection sqlConnectC;
protected SqlCommand sqlCommandC;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
sqlConnectC = new SqlConnection(conString);
sqlCommandC = new SqlCommand("countContracty
", sqlConnectC);
sqlCommandC.CommandType = CommandType.StoredProcedur
e;
// Input parameter
SqlParameter workParam = new SqlParameter("@startType",
SqlDbType.Int);
workParam.Direction = ParameterDirection.Input;
workParam.Value = 99;
sqlCommandC.Parameters.Add
(workParam
);
// Output parameter
SqlParameter workParam2 = new SqlParameter("@contractCou
nt", SqlDbType.Int);
workParam2.Direction = ParameterDirection.Output;
sqlCommandC.Parameters.Add
(workParam
2);
if (RadioButtonList1.Selected
Index == 0)
{
sqlCommandC.Parameters["@s
tartType"]
.Value = 0;
}
else
{
sqlCommandC.Parameters["@s
tartType"]
.Value = 1;
}
sqlConnectC.Open();
sqlCommandC.ExecuteNonQuer
y();
sqlConnectC.Close();
lblCount.Text = "Count: " +sqlCommandC.Parameters[1]
.Value.ToS
tring();
Execution: App runs 100%!!!
CORRECT:
protected SqlConnection sqlConnectC;
protected SqlCommand sqlCommandC;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
sqlConnectC = new SqlConnection(conString);
sqlCommandC = new SqlCommand("countContracty
", sqlConnectC);
sqlCommandC.CommandType = CommandType.StoredProcedur
e;
// Input parameter
SqlParameter workParam = new SqlParameter("@startType",
SqlDbType.Int);
workParam.Direction = ParameterDirection.Input;
workParam.Value = 99;
sqlCommandC.Parameters.Add
(workParam
);
// Output parameter
SqlParameter workParam2 = new SqlParameter("@contractCou
nt", SqlDbType.Int);
workParam2.Direction = ParameterDirection.Output;
sqlCommandC.Parameters.Add
(workParam
2);
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
if (RadioButtonList1.Selected
Index == 0)
{
sqlCommandC.Parameters["@s
tartType"]
.Value = 0;
}
else
{
sqlCommandC.Parameters["@s
tartType"]
.Value = 1;
}
sqlConnectC.Open();
sqlCommandC.ExecuteNonQuer
y();
sqlConnectC.Close();
Execution:
Server Error in '/Stored_Proc_03_Scalar_Pa
ram' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceExcept
ion: Object reference not set to an instance of an object.
Source Error:
Line 44: if (RadioButtonList1.Selected
Index == 0)
Line 45: {
Line 46: sqlCommandC.Parameters["@s
tartType"]
.Value = 0;
Line 47: }
Line 48: else
Source File: d:\MyStuff - Allan\New Horizons\Training\Course_P
racticals\
ASP_Projec
ts\Stored_
Proc_03_Sc
alar_Param
\Default.a
spx.cs Line: 46