Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object reference not set to an instance of an object.

Status
Not open for further replies.

primagic

IS-IT--Management
Jul 24, 2008
476
GB
I am getting the following error on line 26. can someone tell me where I am going wrong. Its a radiobuttonlist I am trying to get the value from

Code:
Line 26:         cmdInsert.Parameters.AddWithValue("@Gender", rdoGender.SelectedItem.Value)
Thanks

Code:
Imports System.Data.SqlClient
Imports System.Data
Imports System.Web.UI.WebControls.RadioButtonList

Partial Class register
    Inherits System.Web.UI.Page
    





    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim conTIBMembers As SqlConnection
        Dim strInsert As String
        Dim cmdInsert As SqlCommand

        Dim rdoGender As System.Web.UI.WebControls.RadioButtonList = Nothing

        conTIBMembers = New SqlConnection("Server=(local);UID=sa;PWD=admin2010;database=TIB")
        strInsert = "Insert dbo.Members (Firstname, Surname, Nationality, Gender, DateofBirth, EmailAddress, Password, MailingList) Values (@Firstname, @Surname, @Nationality, @Gender, @DateofBirth,  @EmailAddress, @Password, @MailingList)"
        cmdInsert = New SqlCommand(strInsert, conTIBMembers)
        cmdInsert.Parameters.AddWithValue("@Firstname", txtFirstname.Text)
        cmdInsert.Parameters.AddWithValue("@Surname", txtSurname.Text)
        cmdInsert.Parameters.AddWithValue("@Nationality", cboNationality.SelectedItem.Text)
        cmdInsert.Parameters.AddWithValue("@Gender", rdoGender.SelectedItem.Value)
        cmdInsert.Parameters.AddWithValue("@DateofBirth", txtDateofBirth.Text)
        cmdInsert.Parameters.AddWithValue("@EmailAddress", txtEmail.Text)
        cmdInsert.Parameters.AddWithValue("@Password", txtPassword.Text)
        cmdInsert.Parameters.AddWithValue("@MailingList", chkMailingList.Checked)
        conTIBMembers.Open()
        cmdInsert.ExecuteNonQuery()
        conTIBMembers.Close()

    End Sub
End Class
 
There does not appear to be anything to initialise rdoGender between
Code:
Dim rdoGender As System.Web.UI.WebControls.RadioButtonList = Nothing
and your reference to the SelectedItem property. I can't remember what Nothing means in .Net, but I doubt if it has a SelectedItem property.

HTH
Simon
 
Why would you define a radiobuttonlist and then set it to nothing? You have to use NEW to initalize a control. And if you do it your way, you still have to add it to the control collection of the page. You probably just want to drop the control on the page and let .net define and initalize it, then you can get the value easily.
 
I can't remember what Nothing means in .Net, but I doubt if it has a SelectedItem property.
Nothing just means a null object, so in this case the rdoGender object will still have a type of RadioButtonList and therefore a SelectedItem property (but one that is null so you can't access it's Value property).

As you point out though, the OP hasn't set the SelectedItem anywhere prior to using it.

Mark,

Darlington Web Design[tab]|[tab]Experts, Information, Ideas & Knowledge[tab]|[tab]ASP.NET Tips & Tricks
 
Thanks for all your replies. I managed to solve it by removing the line

Code:
Dim rdoGender As System.Web.UI.WebControls.RadioButtonList = Nothing

And changing

Code:
cmdInsert.Parameters.AddWithValue("@Gender", rdoGender.SelectedItem.Value)

to

Code:
cmdInsert.Parameters.AddWithValue("@Gender", rdoGender.SelectedValue)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top