syntax error why

Posted on 2011-10-02
Last Modified: 2012-05-12
 Protected Sub CheckBox2_CheckedChanged1(sender As Object, e As EventArgs)
        For Each r As GridViewRow In GridView1.Rows
(DirectCast(r.FindControl("CheckBox1"), CheckBox)).Checked = True

    End Sub
Im getting curly on '(DirectCast)
Question by:zachvaldez
    LVL 12

    Accepted Solution

    use this:

    For Each r As GridViewRow In GridView1.Rows
                Dim chkSel As CheckBox = DirectCast(r.FindControl("chkSelect"), CheckBox)
                chkSel.Checked = True

    Open in new window

    LVL 83

    Assisted Solution

    To be on the safe side, first make sure that a control has been found because you will get a NullReferenceException otherwise

    Protected Sub CheckBox2_CheckedChanged1(sender As Object, e As EventArgs)
            Dim ctrl as Object
            For Each r As GridViewRow In GridView1.Rows
               ctrl = r.FindControl("CheckBox1")
               If IsNothing(ctrl) = False Then
                  CType(ctrl, Checkbox).Checked = True
               End If
    End Sub

    Open in new window

    LVL 9

    Assisted Solution

    change this
    (DirectCast(r.FindControl("CheckBox1"), CheckBox)).Checked = True


    DirectCast(r.FindControl("CheckBox1"), CheckBox).Checked = True

    Author Closing Comment

    The answers addressed the problem.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now