1

I am new to Vb.net and Sql Server(don't be harsh). Can anybody check my code, i feel it is wrong way to do. The purpose is to show query only if check-box is clicked and shows that query. Thank you very much.

 Private Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click

    CheckValue = 0 'set to value 0 
    'check if any checkbox is checked.
    For Each C As Control In Me.Controls
        If C.GetType Is GetType(CheckBox) Then
            Dim cb As CheckBox = DirectCast(C, CheckBox)
            If cb.Checked Then
                CheckValue = CheckValue + 1 'only works if value is above 1
            End If
        End If
    Next


    If CheckValue <> 0 Then   'only if check box is checked then rest will run

        Try
            If chkboxId.Checked = True Then 'assign value to checkbox
                chkboxIdstr = "userid"
            Else
                chkboxIdstr = ""
            End If

            If chkboxName.Checked = True Then
                chkboxNamestr = "username"
            Else
                chkboxNamestr = ""
            End If

            'to insert comma, only if both are true.
            If chkboxId.Checked = True And chkboxName.Checked = True Then
                SQLcomma = " , "
            Else
                SQLcomma = ""
            End If

            'so if both are checked we get a comma
            SQL2 = chkboxIdstr + " " + SQLcomma + " " + chkboxNamestr
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Try 'sql connect and display
            sqlCon.Open()
            SQL = "SELECT " + SQL2 + " FROM user_info"

            Dim dadapter As New SqlDataAdapter(SQL, sqlCon)
            Dim dset As New DataSet
            sqlCon.Close()
            dadapter.Fill(dset)
            'fill datagrid
            dgv.DataSource = dset.Tables(0)

        Catch ex As Exception
            MsgBox(ex.Message)
            sqlCon.Close()
        End Try
    Else
        MsgBox("Plesase do check any box to filter ")
        Exit Sub

    End If
End Sub

-- I learn by doing small application(trail and error), mostly google search for solutions.

5
  • You dont need to open the connection if u are using SqlDataAdapter, because it does it byitself Commented Dec 30, 2014 at 11:08
  • Just a question. What would you do if you have to pass an value to your Select statement? Lets say you have to select a row where the userid is 45. How would you pass the 45? Commented Dec 30, 2014 at 11:14
  • Manuchao. I have no idea. I will check it. Commented Dec 30, 2014 at 11:21
  • Well I will tell you just for the case, because you cant avoid it :). "SELECT userid FROM user_info where userid = @value" user_info"dadapter.SelectCommand.Parameters.AddWithValue("@value",45) Commented Dec 30, 2014 at 11:26
  • Thank you Manuchao. But i will try it. Thanks. Commented Dec 30, 2014 at 11:30

2 Answers 2

1

I don't think you need all that code. Just test your checkbox one by one and build incrementally the list of columns to add to the SELECT query....

Dim selectColumns As String
Try

    If chkboxId.Checked = True Then 
        selectColumns = "userid,"
    End If

    If chkboxName.Checked = True Then
        selectColumns &= "username,"
    End If

    ' Other columns???? follow the same pattern

    ' If we have something in selectColumns then we could execute the query
    ' but before we trim away the last comma
    If selectColumns.Length > 0 Then 
       selectColumns = selectColumn.TrimEnd(",")

       sqlCon.Open()
       SQL = "SELECT " & selectColumns & " FROM user_info"
       .....
    Else
       ' no data in selectColumns? Message for you user
       MsgBox("Plesase do check any box to filter ")
       Exit Sub
    End If    
Catch ex As Exception
    MsgBox(ex.Message)
    sqlCon.Close()
End Try
Sign up to request clarification or add additional context in comments.

2 Comments

Had an error "Object reference not set to an instance of an object". after giving value for selectColumns its ok .
Steve, Thank you so much. Learning programming in my spare time.
1

Try this:

Private Sub btnFilter_Click(sender As Object, e As EventArgs) Handles btnFilter.Click

    If chkboxId.Checked OrElse chkboxName.Checked Then
        SQL2 = String.Empty
        If chkboxId.Checked Then
            SQL2 = SQL2 + "userid"
        EndIf

        If chkboxId.Checked AndAlso chkboxName.Checked Then
            SQL2 = SQL2 + " , "
        End If

        If chkboxName.Checked Then
            SQL2 = SQL2 + "username"
        End If

        Try 'sql connect and display
            sqlCon.Open()
            SQL = "SELECT " + SQL2 + " FROM user_info"

            Dim dset As New DataSet
            Using dadapter As New SqlDataAdapter(SQL, sqlCon)
                dadapter.Fill(dset)
            End Using

            'fill datagrid
            dgv.DataSource = dset.Tables(0)

          Catch ex As Exception
             MsgBox(ex.Message)
          Finally
             sqlCon.Close()
          End Try
    Else
        MsgBox("Please do check any box to filter ")
    End If

End Sub

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.