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.
ICantSeeAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Meir RivkinConnect With a Mentor Full stack Software EngineerCommented:
run this code.
if u get mesasgebox, u need to check your config file, u might spell the connection string wrong.
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 constring = ConfigurationManager.ConnectionStrings("SqlConnectionString").ConnectionString
if constring is nothing or constring = String.Empty then
MessageBox.Show("Invalid connection string.")
return 
end if

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

0
 
ICantSeeAuthor Commented:
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.
0
 
ICantSeeAuthor Commented:
Thank you
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
Manoj PatilConnect With a Mentor Sr. Software EngineerCommented:
Is your connection string name is correct as per the web.config i.e.  SqlConnectionString ?
0
 
lenordisteConnect With a Mentor Commented:
this piece of code:
ConfigurationManager.ConnectionStrings("SqlConnectionString")

Open in new window

is what is returning a null value which is the bug you are after.

From the looks of it "SqlConnectionString" has:
- not been declared in the correct .config file
- not been declared in the correct .config section
- has been misspelled (remember that this is case sensitive!!!)

here is some info on how to declare the connectionstring correctly:
http://msdn.microsoft.com/en-us/library/ms254494.aspx

send us you app.Config or web.config file so we can double check this.
0
 
ICantSeeAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.