开发者

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:

  1. If you are sure, that it can never be 3 or greater, insert an else clause that throws an ArgumentOutOfRangeException
  2. If you are not sure that it can never be 3 or greater, just initialize sqlparam.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜