Link to home
Start Free TrialLog in
Avatar of ICantSee
ICantSeeFlag for United States of America

asked on

Problem writing data from asp.net page to sql database

I'm getting a "Object reference not set to an instance of an object" error  referencing line 17 "Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString) " in the app that I am writing.

It simply needs to collect the selected values of 8 dropdown lists and the text from one text box and insert it into my database.

Here is the code from the vb codebehind page
Imports System.Data.SqlClient
Partial Class forms_CW_ImpactSurvey
    Inherits System.Web.UI.Page

    Private _strsql As String

    Private Property strsql As String
        Get
            Return _strsql
        End Get
        Set(value As String)
            _strsql = value
        End Set
    End Property

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString)
        Dim sqlComm As New SqlCommand

        sqlCon.Open()
        strsql = " insert into AgencyServicesImpactSurvey (SurveyMedical, SurveyResources, SurveyMeetings, SurveyPaperwork, SurveyGroceries, SurveyPrograms, SurveyVision, SurveyIndependence, SurveySatisfied, SurveyComments) values ('" _
        & MedicalDropdownList.SelectedValue & "','" _
        & ResourcesDropdownlist.SelectedValue & "','" _
        & MeetingsDropdownlist.SelectedValue & "','" _
        & PaperworkDropdownlist.SelectedValue & "','" _
        & GroceriesDropdownlist.SelectedValue & "','" _
        & ProgramsDropdownlist.SelectedValue & "','" _
        & visionDropdownlist.SelectedValue & "','" _
        & IndependenceDropdownlist.SelectedValue & "','" _
        & CommentsTextbox.Text & "')"
        sqlComm.CommandText = strsql
        sqlComm.Connection = sqlCon
        sqlComm.ExecuteNonQuery()
        MsgBox("Data Saved")
        sqlCon.Close()
        sqlCon.Dispose()
    End Sub

End Class

Open in new window

I'm stumped. Any help will be aprreciated.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

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 ICantSee

ASKER

Thank you for your response,

The code added is:


    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString)
        Dim sqlComm As New SqlCommand
        Dim constring = ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString

        If constring Is Nothing Or constring = String.Empty Then
            MsgBox("Invalid connection string.")
            Return
        End If

I had to change MessageBox.show to MsgBox to get rid of a "MessageBox is inaccessible due to its protection level error

I still receive the original error, "Object reference not set to an instance of an object" error  referencing line 17 "Dim sqlCon As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString) " 
when I execute the code.
Thank you
SOLUTION
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
SOLUTION
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
Thank you for all of your replies,

I ended up changing the code to the following. Are there any gotchas I should know about it?

How difficult would it be to have an email sent to a predetermined recipient after the info is written to the DB?

Imports System.Data.SqlClient
Partial Class forms_CW_ImpactSurvey
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Private _strsql As String
    Private Property strsql As String
        Get
            Return _strsql
        End Get
        Set(value As String)
            _strsql = value
        End Set
    End Property


    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        con = New SqlConnection("Data Source=WEBSQL\WEBDATA;Initial Catalog=Service_Forms;Integrated Security=True")
        con.Open()
        strsql = "insert into AgencyServicesImpactSurvey (SurveyMedical, SurveyResources, SurveyMeetings, SurveyPaperwork, SurveyGroceries, SurveyPrograms, SurveyVision, SurveyIndependence, SurveySatisfied, SurveyComments) values ('" _
        & MedicalDropdownList.SelectedValue & "','" _
        & ResourcesDropdownlist.SelectedValue & "','" _
        & MeetingsDropdownlist.SelectedValue & "','" _
        & PaperworkDropdownlist.SelectedValue & "','" _
        & GroceriesDropdownlist.SelectedValue & "','" _
        & ProgramsDropdownlist.SelectedValue & "','" _
        & visionDropdownlist.SelectedValue & "','" _
        & IndependenceDropdownlist.SelectedValue & "','" _
        & SatisfiedDropdownlist.SelectedValue & "','" _
        & CommentsTextbox.Text & "')"
        cmd.CommandText = strsql
        cmd.Connection = con
        cmd.ExecuteNonQuery()
        MsgBox("Data Saved")
        con.Close()
        con.Dispose()
    End Sub

End Class