Use of unassigned local variable 'strSql' Error Message

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenniekuah
    New Member
    • Oct 2006
    • 126

    Use of unassigned local variable 'strSql' Error Message

    Hi Good Guy,

    I am very surprised of the coding generate error message
    Error 1 Use of unassigned local variable 'strSql'

    even though the strSQL variable was declared. Not sure what is wrong with the overall coding;


    Here are the coding:

    Code:
     private void FLoadDataGridView()
        {
           string strSql;
    
           if (this.radioBtnAll.Checked == true)
           {
           strSql = "Select * from Customers Order by CustomerID";
           }
    
          if (this.radioBtnSome.Checked == true)
           {
           strSql = "Select * from Customers ";
           strSql += "Where CustomerID like 'this.txtSome.Text%'";
           strSql += "Order by CustomerID ";
          }
     
           sqlConn = new SqlConnection(connStr);
           sqlConn.Open();
         [B]  DA = new SqlDataAdapter(strSql, sqlConn);  <---- Error [/B]          
           DS = new DataSet("DS");            
           DA.Fill(DS,"Customer");
                       
          dataGridView1.DataSource = DS.Tables["Customer"];
          dataGridView1.ClearSelection();
                    
           sqlConn.Close();
           DS.Dispose();            
               
    }

    Thank You.

    Cheers,
    Lennie
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You never set it to anything unless one of the radio buttons is checked, but always try to use it. You should probably only do the Sql work if the button is checked?

    Comment

    • Joseph Martell
      Recognized Expert New Member
      • Jan 2010
      • 198

      #3
      The error has nothing to do with the fact that your variable has been declared or not. It is in reference to the fact that it is possible for you to get to the line

      Code:
      DA = new SqlDataAdapter(strSql, sqlConn);
      Without assigning a value to strSql and therefor strSql could be null.

      This error can be avoided by declaring strSql as:

      Code:
      string strSql = "";
      but that only hides the problem. Even initializing strSql to "" would still generate an error if you try to use it. You need to have some sort of default case. Assuming that only one of the radio buttons (radioBtnAll and radioBtnSome) can be checked and 1 MUST be checked, then you could do something like this, using radioBtnAll as your default case:

      Code:
      if (radioBtnSome.Checked)
      {
          strSql = "Select * from Customers ";
          strSql += "Where CustomerID like 'this.txtSome.Text%'";
          strSql += "Order by CustomerID ";
      }
      else
      {
          strSql = "Select * from Customers Order by CustomerID";
      }
      This would guarantee initialization of strSql before use (even if a new radio button was added later on) and gets rid of the warning.

      Comment

      • lenniekuah
        New Member
        • Oct 2006
        • 126

        #4
        Hullo Plater and IBM1313,

        Thanks to both of you for sharing your sample coding and advices. I have followed your advices and coding and now my coding is running very well.

        Both of you are just awesome and so glad to meet both of you here. This FORUM is awesome with good helpers of you 2 here.

        Comment

        Working...