Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

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>


0
newbie_girl
Asked:
newbie_girl
1 Solution
 
bele04Commented:
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




0
 
vinodhsomasekharanCommented:
Hai,

 Hope u r using the checkbox inside a datalist.
so u would have "n" no. of checkboxes for "n" no.of rows

So u have to loop through the datalist items collection ( ie Rows collection ) and find the checkbox control and then get its checked value..

ie
for example
dim myChkBox  as  System.Web.UI.WebControls.CheckBox
For rowCounter As Integer = 0 To CreateCDQ.Items.Count - 1
  myChkBox  = CType(gridDocument.Items(rowCounter).FindControl("chkPerm"), System.Web.UI.WebControls.CheckBox)
'Here u can get myChkBox.checked value
Next


Hope this helps u.

Regards
Vinu
0
 
newbie_girlAuthor Commented:
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
0
 
RJeyaPrakashCommented:
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
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now