locked
Use of unassign local variable??? RRS feed

  • Question

  • User-1193791088 posted
     public static int GetAccountId(string AccountName, int AccountTypeId)
                {
                    SqlCommand _SqlCm = null;
                    SqlConnection _SqlCn = null;
                    SqlDataReader _SqlRd = null;
                    int AccountId;
                    try
                    {
                        _SqlCn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
                        _SqlCn.Open();
                        _SqlCm = new SqlCommand("SELECT [AccountId] FROM [CuAccount] WHERE ([AccountName] = '" + AccountName + "') AND ([AccountTypeId] = '" + AccountTypeId + "')", _SqlCn);
                        _SqlRd = _SqlCm.ExecuteReader();
                        if (_SqlRd.HasRows)
                        {
                            while (_SqlRd.Read())
                            {
                                AccountId = Convert.ToInt32(_SqlRd["AccountId"]);
                            }
                        }
                        else
                        {
                            AccountId = 0;
                        }
                        
                    }
                    catch (Exception Ex)
                    {
                        Reporting.Errors.ExceptionLog(Ex.InnerException.GetType().ToString(), 0, Ex.TargetSite.ToString(), Ex.ToString(), Ex.Source.ToString());
                    }
                    finally
                    {
                        _SqlRd.Close();
                        _SqlCn.Close();
                    }
                    return AccountId;
                }

    why is the last line not valid???

    Monday, February 19, 2018 5:23 AM

All replies

  • User303363814 posted

    Because the compiler does not know that if _SqlRd.HasRow is true that _SqlRd.Read() has to be true at least once.

    Initialise AccountId to 0 and omit the entire else clause.

    ==Or== omit the 'While'.  Do you expect that there will be more than one row?  If there is more than one row do you really want to assign AccountId the value of some random row?

    Monday, February 19, 2018 6:59 AM
  • User-1716253493 posted

    If any fail in try block, the variable will be unassigned

    Try this

     public static int GetAccountId(string AccountName, int AccountTypeId)
                {
                    SqlCommand _SqlCm = null;
                    SqlConnection _SqlCn = null;
                    SqlDataReader _SqlRd = null;
                    int AccountId=0; //default value
                    try
                    {
                        _SqlCn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
                        _SqlCn.Open();
                        _SqlCm = new SqlCommand("SELECT [AccountId] FROM [CuAccount] WHERE ([AccountName] = '" + AccountName + "') AND ([AccountTypeId] = '" + AccountTypeId + "')", _SqlCn);
                        _SqlRd = _SqlCm.ExecuteReader();
                        if (_SqlRd.HasRows)
                        {
                            while (_SqlRd.Read())
                            {
                                AccountId = Convert.ToInt32(_SqlRd["AccountId"]);
                            }
                        }
                    }
                    catch (Exception Ex)
                    {
                        Reporting.Errors.ExceptionLog(Ex.InnerException.GetType().ToString(), 0, Ex.TargetSite.ToString(), Ex.ToString(), Ex.Source.ToString());
                    }
                    finally
                    {
                        _SqlRd.Close();
                        _SqlCn.Close();
                    }
                    return AccountId;
                }

    Monday, February 19, 2018 7:07 AM
  • User-1838255255 posted

    Hi RobertH3,

    According to your description, as far as i know, this issue is because of the local variables aren't initialized. You could try the following method:

    Replace: 
    int AccountId;
    To:
    int AccountId=0;

    Also about the other methods to initialize the variable, please check this link: 

    https://stackoverflow.com/questions/9233000/why-compile-error-use-of-unassigned-local-variable 

    Best Regards,

    Eric Du  

    Saturday, February 24, 2018 8:29 AM