ICantSee
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(Configuratio nManager.C onnectionS trings("Sq lConnectio nString"). Connection String) " 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
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
I'm stumped. Any help will be aprreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;Init ial Catalog=Service_Forms;Inte grated Security=True")
con.Open()
strsql = "insert into AgencyServicesImpactSurvey (SurveyMedical, SurveyResources, SurveyMeetings, SurveyPaperwork, SurveyGroceries, SurveyPrograms, SurveyVision, SurveyIndependence, SurveySatisfied, SurveyComments) values ('" _
& MedicalDropdownList.Select edValue & "','" _
& ResourcesDropdownlist.Sele ctedValue & "','" _
& MeetingsDropdownlist.Selec tedValue & "','" _
& PaperworkDropdownlist.Sele ctedValue & "','" _
& GroceriesDropdownlist.Sele ctedValue & "','" _
& ProgramsDropdownlist.Selec tedValue & "','" _
& visionDropdownlist.Selecte dValue & "','" _
& IndependenceDropdownlist.S electedVal ue & "','" _
& SatisfiedDropdownlist.Sele ctedValue & "','" _
& CommentsTextbox.Text & "')"
cmd.CommandText = strsql
cmd.Connection = con
cmd.ExecuteNonQuery()
MsgBox("Data Saved")
con.Close()
con.Dispose()
End Sub
End Class
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;Init
con.Open()
strsql = "insert into AgencyServicesImpactSurvey
& MedicalDropdownList.Select
& ResourcesDropdownlist.Sele
& MeetingsDropdownlist.Selec
& PaperworkDropdownlist.Sele
& GroceriesDropdownlist.Sele
& ProgramsDropdownlist.Selec
& visionDropdownlist.Selecte
& IndependenceDropdownlist.S
& SatisfiedDropdownlist.Sele
& CommentsTextbox.Text & "')"
cmd.CommandText = strsql
cmd.Connection = con
cmd.ExecuteNonQuery()
MsgBox("Data Saved")
con.Close()
con.Dispose()
End Sub
End Class
ASKER
The code added is:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim sqlCon As New SqlConnection(Configuratio
Dim sqlComm As New SqlCommand
Dim constring = ConfigurationManager.Conne
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(Configuratio
when I execute the code.