Link to home
Start Free TrialLog in
Avatar of newbie_girl
newbie_girl

asked on

Going mad here, can someone please assist? Object reference not set to an instance of an object

Hi there,

I'm sure this is an easy one but I'm stumped and going mad I think!  I'm getting the Object reference not set to an instance of an object error on the line: "If chkNewClient1.checked THEN" - here is the code:
Protected existingCDQ As DataGrid
      Protected createCDQ As DataList
      Protected lblClient1_1 As Label
      Protected lblNewCDQClient1 As Label
      Protected chkNewClient1 As Checkbox
      
      Public strSQL as string
      Public ds as DataSet
      Public Con as New SqlConnection
      Public added as integer
Sub cmdInsert_Click(sender as object, e as EventArgs)
      If chkNewClient1.checked THEN -------------error on this line here
      
      Dim strInsert As String
            strInsert = "Insert into CDQ ("blah blah"')"
                  
      Dim con as New SqlConnection(connectionstring)
      Dim cmd as New SqlCommand(strInsert, con)
      
      Try
            Con.Open()
            Dim added as Integer
            Added = cmd.ExecuteNonQuery
            lblNewCDQClient1.Text = added.ToString() & " Record updated"
      Catch err As Exception
            lblNewCDQClient1.Text = "Error updating the record "
            lblNewCDQClient1.Text &=Err.Message
      Finally
            If Not (con is Nothing) Then
                  Con.Close()
            End If
      End Try
      If added > 0 Then
ExistingCDQ_BindGrid(ds)
      End If
End If
End Sub

------------------------------------------
essentials on the ASP page:
<asp:DataList runat="server" ID="CreateCDQ">
      <asp:CheckBox
runat="server"
ID="chkNewClient1"
TextAlign="Left"
Text="Yes, I would like to create a new CDQ for <b>Client 1</b>" />
      
<asp:LinkButton
OnClick="cmdInsert_Click"
Text="CREATE"
runat="server"
CssClass="btn"/>
      </itemtemplate>
      </asp:DataList>


Avatar of bele04
bele04

Hi,

I can see that your checkbox is inside a datalist.  You cannot access controls directly in a datalist. you should try using the findcontrol method of the datalist instead like this:

Sub cmdInsert_Click(sender as object, e as EventArgs)
     If CreateCDQ.FindControl("chkNewClient1") <> null THEN
         If (CreateCDQ.FindControl("chkNewClient1")).checked THEN -------------error on this line here
     
         Dim strInsert As String
              strInsert = "Insert into CDQ ("blah blah"')"

         .............
    End If
End Sub




ASKER CERTIFIED SOLUTION
Avatar of vinodhsomasekharan
vinodhsomasekharan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newbie_girl

ASKER

thanks, I'm breathing again!

This worked:

If (CType(e.Item.FindControl("chkNewClient1"), CheckBox)).checked Then

I can't accept two answers can I?  Bele04, I"ll have to give thte points to Vinu.

thanks heaps

newbie
hai

yeah., of course u can accept both if u find both are correct.

By the way, vinu is on fire. Keep it up Mr.Vinu.


rgds
jp