Problem in Declaration and using SqlParameter[].ERROR:Use of unassigned local variable 'sqlparam'
I have to Declare a SqlParameter[]. It's size is dynamic based on the dropdownlist selection. I have implemented a method as below
protected void BindGrid()
{
SqlParameter[] sqlparam;
string SP = string.Empty;
if (ddlKPIType.SelectedIndex == 0)//For Overall
{
sqlparam = PrepareSqlParams();
}
else if (ddlKPIType.SelectedIndex == 1)//For Parameterwise
{
sqlparam = PrepareSqlParamsForParameterWise();
}
else if (ddlKPIType.SelectedIndex == 2)//For Measurement Criteria wise
{
sqlparam = PrepareSqlParamsForMeasurmentCriteriaWise();
}
DataSet dsRep开发者_运维技巧ort = GetReportDataSet(sqlparam, SP);
if (isDataSetValid(dsReport))
gridMonthlyReport.DataSource = dsReport.Tables[0];
gridMonthlyReport.DataBind();
}
But in this code when I pass the sqlparam as a Parameter to GetReportDataSet() it is showing the error"Use of unassigned local variable 'sqlparam'.
The methods :PrepareSqlParams()
,
PrepareSqlParamsForParameterWise() and
PrepareSqlParamsForMeasurmentCriteriaWise()
will return SqlParameter[].
Where I am making a mistake? Any one please help me. Thanks in advance.
sqlparam
is not guaranteed to be assigned, because there is no else
clause. If ddlKPIType.SelectedIndex
is 3 or greater, sqlparam
will not be assigned, because none of the branches of your if-else if
clause will be executed.
You can overcome this in several ways:
- If you are sure, that it can never be 3 or greater, insert an
else
clause that throws anArgumentOutOfRangeException
- If you are not sure that it can never be 3 or greater, just initialize
sqlparam
.
精彩评论