?
Solved

Problem writing data from asp.net page to sql database

Posted on 2013-06-26
7
Medium Priority
?
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 501 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 19

Assisted Solution

by:Manoj Patil
Manoj Patil earned 501 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 498 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

Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
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…

771 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