title: Change sql parameter based on a public variable from an if statement
body: <p>I am currently working in vb.net windows form applications with an sql back end. I am currently having a problem with trying to lean out some code. I have a combobox that is databound to an sql query. I am trying to set up an event for selection change committed that will run an sql query and insert a string dependent upon the selected value of the combobox. I know of a way to do it that would force me to repeat the actual sql statement 6 times but I wanted to do it in a more short hand version. Here is my code:</p>
  
  <pre><code>  Dim MachineEnd as string
          'declare machine end string
      If Combobox.SelectedValue = 5 Then
          MachineEnd = "StringA"
      ElseIf Combobox.SelectedValue = 6 Then
          MachineEnd = "StringB"
      ElseIf Combobox.SelectedValue = 7 Then
          MachineEnd = "StringC"
      ElseIf Combobox.SelectedValue = 8 Then
          MachineEnd = "StringD"
      ElseIf Combobox.SelectedValue = 18 Then
          MachineEnd = "StringE"
      ElseIf Combobox.SelectedValue = 15 Then
           MachineEnd = "StringF"
          End if
  
          'load quick view
          Using conn1 As New SqlConnection(connstring)
              conn1.Open()
              Using comm1 As New SqlCommand("SELECT " _
                                              &amp; "RIGHT('0' + CAST(DATEPART(M, ColA) AS varchar), 2) " _
                                              &amp; "+ RIGHT ('0' + Cast(DATEPART(DD, ColA) AS varchar), 2) " _
                                              &amp; "+ RIGHT('0' + CAST(DATEPART(HH, ColA) AS varchar), 2) " _
                                              &amp; "+ RIGHT('0' + CAST(DATEPART(MINUTE, ColA) AS varchar), 2) AS 'ColNumber', BuildOrder, TimeStamp " _
                                              &amp; "from table1 Where Machine = @Machine AND @MachineFinish IS NULL " _
                                              &amp; "AND Col1 IS NOT NULL", conn1)
              comm1.Parameters.AddWithValue("@Machine", CBMachine.SelectedValue)
              comm1.Parameters.AddWithValue("@MachinEFinish", MachineEnd)
              Dim dt As New DataTable
              Dim sql As New SqlDataAdapter(comm1)
              sql.Fill(dt)
              Datagridview.DataSource = dt
          End Using
              conn1.Close()
          End Using
  </code></pre>
  
  <p>My problem comes in on the "MachineEnd" variable. Due to the end if statement not including the sql query I will leave the "MachineEnd" variable in the parameter statement undefined. However, if I move my end if statement to include the sql query I would have to copy the sql statement 6 times over and I want to try and do it in less lines. I think I need to either make the MachineEnd a public vatiable or use a different type of vb.net command, maybe a select case statement. The reason I am doing this is because I have different column names that match stringA, stringB, etc. etc. and I need to declare them as null when I load the datagridview.</p>
  
  <p>--------------Post Edit-------------------------
  Made changes to match dan's idea. I tried this and it looks like its working, however, I am getting an error that says the "machineEnd" variable is being used before it is assigned a value and could result in a dbnull. But, when I run the code and add a watch to the variable it says that the correct string is tied to the variable. But my datagridview is not populating correctly. I put my sql code into the sql server management studio and the code ran correctly. I am not sure what is going on with this.</p>
  
  <p>-------------------Edit 2----------------------------------
  I found out that using the addwithvalue parameter statement is causing the datagridview to come back blank. If I use a &amp; MachineEnd &amp; in my sql statement the code actually works. considering that the combobox is databound I am pretty sure an sql injection attack is fairly unlikely.</p>