jeudi 23 avril 2015

Change sql parameter based on a public variable from an if statement

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:

        'declare machine end string
    If Combobox.SelectedValue = 5 Then
        Dim MachineEnd As String = "StringA"
    ElseIf Combobox.SelectedValue = 6 Then
        Dim MachineEnd As String = "StringB"
    ElseIf Combobox.SelectedValue = 7 Then
        Dim MachineEnd As String = "StringC"
    ElseIf Combobox.SelectedValue = 8 Then
        Dim MachineEnd As String = "StringD"
    ElseIf Combobox.SelectedValue = 18 Then
        Dim MachineEnd As String = "StringE"
    ElseIf Combobox.SelectedValue = 15 Then
        Dim MachineEnd As String = "StringF"
        End if

        'load quick view
        Using conn1 As New SqlConnection(connstring)
            conn1.Open()
            Using comm1 As New SqlCommand("SELECT " _
                                            & "RIGHT('0' + CAST(DATEPART(M, ColA) AS varchar), 2) " _
                                            & "+ RIGHT ('0' + Cast(DATEPART(DD, ColA) AS varchar), 2) " _
                                            & "+ RIGHT('0' + CAST(DATEPART(HH, ColA) AS varchar), 2) " _
                                            & "+ RIGHT('0' + CAST(DATEPART(MINUTE, ColA) AS varchar), 2) AS 'ColNumber', BuildOrder, TimeStamp " _
                                            & "from table1 Where Machine = @Machine AND @MachineFinish IS NULL " _
                                            & "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

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.

Aucun commentaire:

Enregistrer un commentaire