0

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 dsReport = 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.

1
  • Are all celles of sqlparam nun null? Commented Jul 20, 2011 at 8:39

1 Answer 1

4

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.