Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem writing data from asp.net page to sql database

Posted on 2013-06-26
7
Medium Priority
?
529 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:
Meir Rivkin 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

604 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