Solved

Problem writing data from asp.net page to sql database

Posted on 2013-06-26
7
493 Views
Last Modified: 2013-07-01
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.
0
Comment
Question by:ICantSee
7 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 167 total points
ID: 39280834
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
 

Author Comment

by:ICantSee
ID: 39280969
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
 

Author Comment

by:ICantSee
ID: 39281554
Thank you
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 167 total points
ID: 39285460
Is your connection string name is correct as per the web.config i.e.  SqlConnectionString ?
0
 
LVL 11

Assisted Solution

by:lenordiste
lenordiste earned 166 total points
ID: 39286658
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
 

Author Comment

by:ICantSee
ID: 39289712
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now