Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 19th, 2004, 11:33 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default CheckBox checked as parameters to insert into sql

I am learning from a few books and I am using their examples but I am not sure if I am getting the complete example.

What is wrong with this?
I get error:
 Object reference not set to an instance of an object.

at:
insertCmd.Parameters("@ASP").Value = sASP.ToString

__________________________________________________ __________________________________


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Dim myCONN As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))

        Dim sSql As String = "INSERT INTO Member (ASP, VB) VALUES (@ASP, @VB)"

        Dim insertCmd As New SqlCommand(sSql, myCONN)
        insertCmd.CommandType = CommandType.Text
        insertCmd.Connection = myCONN

        insertCmd.Parameters.Add("@ASP", SqlDbType.NVarChar, 5)
        insertCmd.Parameters.Add("@VB", SqlDbType.NVarChar, 5)


        insertCmd.Parameters("@ASP").Value = sASP.ToString
        insertCmd.Parameters("@VB").Value = sVB.ToString

        Dim bSuccess As Boolean
        bSuccess = True

        Try
            myCONN.Open()
            insertCmd.ExecuteScalar()
            myCONN.Close()

        Catch ex As Exception
            bSuccess = False
            Response.Write("Failed")
            Response.End()

        Finally
            If myCONN.State = ConnectionState.Open Then
                myCONN.Close()
            End If
        End Try


        If bSuccess = True Then
            Response.Write("Success")
        End If





    End Sub


    Private Sub ck1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ck1.CheckedChanged

        If ck1.Checked Then
            sVB = "True"
        Else
            sVB = "False"
        End If

    End Sub

    Private Sub ck2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ck2.CheckedChanged
        If ck2.Checked Then
            sASP = "True"
        Else
            sASP = "False"
        End If
    End Sub
End Class

Thanks.

 
Old August 20th, 2004, 07:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Where is sASP defined? What happens if no checkboxes are checked? Then sASP has no defaults? If sASP is global, assign a default value in the Page_Load. And the values would need to be stored in the Session or Cache, because the values are destroyed after page_unload.

Brian
 
Old August 20th, 2004, 09:45 AM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello
I didn't post the code defining sASP. sASp and sVB are defined.
What do my error code mean?
I get error:
 Object reference not set to an instance of an object.

at:
insertCmd.Parameters("@ASP").Value = sASP.ToString



 
Old August 20th, 2004, 01:11 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

It means the object reference is not set to an instance of an object. So either sASP or insertCMD isn't defined.. Most likely sASP isn't defined properly

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old August 20th, 2004, 01:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Remember, unless you are using Session or Cache, then the variables aren't containing a reference. Could you post the creation of sASP and sVB to verify?

Session or Cache are usually restablished at the last moment or in Page_Load.

Brian
 
Old August 20th, 2004, 03:57 PM
Friend of Wrox
 
Join Date: Jun 2004
Posts: 245
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the full code:

Dim bVB As Boolean
Dim bASP As Boolean

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Dim myCONN As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("C onnectionString"))

        Dim sSql As String = "INSERT INTO Member (ASP, VB) VALUES (@ASP, @VB)"

        Dim insertCmd As New SqlCommand(sSql, myCONN)
        insertCmd.CommandType = CommandType.Text
        insertCmd.Connection = myCONN

        insertCmd.Parameters.Add("@ASP", SqlDbType.NVarChar, 5)
        insertCmd.Parameters.Add("@VB", SqlDbType.NVarChar, 5)


        insertCmd.Parameters("@ASP").Value = sASP.ToString
        insertCmd.Parameters("@VB").Value = sVB.ToString

        Dim bSuccess As Boolean
        bSuccess = True

        Try
            myCONN.Open()
            insertCmd.ExecuteScalar()
            myCONN.Close()

        Catch ex As Exception
            bSuccess = False
            Response.Write("Failed")
            Response.End()

        Finally
            If myCONN.State = ConnectionState.Open Then
                myCONN.Close()
            End If
        End Try


        If bSuccess = True Then
            Response.Write("Success")
        End If
    End Sub


    Private Sub ck1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ck1.CheckedChanged

        If ck1.Checked Then
            sVB = "True"
        Else
            sVB = "False"
        End If

    End Sub

    Private Sub ck2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ck2.CheckedChanged
        If ck2.Checked Then
            sASP = "True"
        Else
            sASP = "False"
        End If
    End Sub
End Class

Help me please.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkbox checked all mateenmohd Javascript 1 January 2nd, 2007 06:19 AM
checkbox checked by default by html:checkbox sachin.tathod Struts 3 December 4th, 2006 03:41 PM
Checkbox checked? ninel ASP.NET 2.0 Professional 1 April 12th, 2006 10:50 PM
allow only one checkbox to be checked! morpheus HTML Code Clinic 5 April 15th, 2004 10:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.