i moved to codebehind and i am currently getting this error when i invoke my button(s). the string is coming null from my web.config. <BR><BR>web.config<BR>----------<BR><configuration><BR> <appSettings><BR> <add key="sConnection" value=http://aspmessageboard.com/archive/index.php/"data source=;initial catalog=;password=;persist security info=True;user id=;packet size=4096" /><BR> </appSettings> <BR> <system.web><BR> the rest of the web.config..bla bla bla<BR><BR>the codebehind line failing<BR>---------------------------<BR> Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR>----------> Dim sConnString = ConfigurationSettings.appSettings("sConnection")<BR> Dim sConn As New SqlConnection(sConnString)<BR><BR>the error is<BR>------------<BR>System.NullReferenceException: Object variable or With block variable not set<BR><BR>..basically sConnection is being returned as null and i cant figure out why. any ideas?Dim ConfigurationSettings<BR><BR>which basically takes that string and makes it null string again. only reason is i get a not declared when i try and build.did you import the System.Web.Configuration Namespace???Imports System.Data.SqlClient<BR>Imports System.Web.Mail<BR>Imports System.Web.Security<BR>Imports System.Web.Configuration<BR>Imports System.Text<BR>Imports System.Data<BR><BR>Public Class login<BR> Inherits System.Web.UI.Page<BR> Dim sError, sEmpID, sWelcome, empID, ssnum, ConfigurationSettings<BR><BR> Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR> Dim sConnString = ConfigurationSettings.appSettings("sConnection")<BR> Dim sConn As New SqlConnection(sConnString)<BR><BR>dies on that line. if i take out the ConfigurationSettings in the above dim i get build errors, do i need to change the sConnString & sConn?do not Dim ConfigurationSettings!<BR>just do:<BR>Dim sConnString As String = ConfigurationSettings.appSettings("sConnection")<BR><BR>Also, you should strongly type your variables; you are declaring all of your global varaibles as objects. Try something like:<BR>Public Class login<BR>Inherits System.Web.UI.Page<BR>private sError As String<BR>private sEmpID As String<BR>private sWelcome As String<BR>private empID As Integer<BR>private ssnum As String<BR><BR><BR>Imports System.Text<BR>Imports System.Web.Mail<BR>Imports System.Web.Security<BR>Imports System.Web.Configuration<BR>Imports System.Data<BR>Imports System.Data.SqlClient<BR><BR>Public Class login<BR> Inherits System.Web.UI.Page<BR> Private sError As Object<BR> Private sEmpID As Object<BR> Private sWelcome As Object<BR> Private empID As Object<BR> Private ssnum As Object<BR> Private ConfigurationSettings As Object<BR><BR> Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR> Dim sConnString As String = ConfigurationSettings.appSettings("sConnection")<BR><BR>that produces the same error. should i do public? this is freakin pissing me off. thanks for the help so far
Is there a reason you are declaring ConfigurationSettings? I am guessing you are trying to get a value from your web config...so all you should have to do is have that<BR><BR>Dim sConnString As String = ConfigurationSettings.appSettings("sConnection")<BR><BR>line in your code. You should not need to declare ConfigurationSettings. I had a working example and once I added that <BR><BR> Private ConfigurationSettings As Object<BR><BR>line in my code, I got the same exact error you did. Hope that helps.<BR><BR>Timthe only reason i am doing that is everytime i try and build i get the error saying that is not declared.your right, i am trying to get the connection string out of web.config. even if i do not declare ConfigurationSettings ...i still get the same error. <BR><BR><BR><BR>Imports System.Text<BR>Imports System.Web.Mail<BR>Imports System.Web.Security<BR>Imports System.Web.Configuration<BR>Imports System.Data<BR>Imports System.Data.SqlClient<BR><BR>Public Class login<BR> Inherits System.Web.UI.Page<BR> Private sError As Object<BR> Private sEmpID As Object<BR> Private sWelcome As Object<BR> Private empID As Object<BR> Private ssnum As Object<BR><BR> Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR> Dim sConnString As String = ConfigurationSettings.appSettings("sConnection")Imports System.Text<BR>Imports System.Web.Mail<BR>Imports System.Web.Security<BR>Imports System.Web.Configuration<BR>Imports System.Data<BR>Imports System.Data.SqlClient<BR><BR>Public Class login<BR> Inherits System.Web.UI.Page<BR> Protected WithEvents sError As System.Web.UI.WebControls.Label<BR> Protected WithEvents sEmpID As System.Web.UI.WebControls.Label<BR> Protected WithEvents sWelcome As System.Web.UI.WebControls.Label<BR> Protected WithEvents empID As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents ssnum As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents valSum As System.Web.UI.WebControls.ValidationSummary<BR> Protected WithEvents empIDRegexVal As System.Web.UI.WebControls.RegularExpressionValidat or<BR> Protected WithEvents ssnumReqVal As System.Web.UI.WebControls.RequiredFieldValidator<BR> Protected WithEvents passwdRegexBal As System.Web.UI.WebControls.RegularExpressionValidat or<BR> Protected WithEvents ssnum2 As System.Web.UI.WebControls.TextBox<BR> Protected WithEvents ssnum2ReqVal As System.Web.UI.WebControls.RequiredFieldValidator<BR> Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator<BR> Protected WithEvents btnLogin As System.Web.UI.WebControls.Button<BR> Protected WithEvents btnID As System.Web.UI.WebControls.Button<BR><BR>'those above are events that i have in the page..disregard. <BR><BR> Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR> Dim sConnString As String = ConfigurationSettings.appSettings("sConnection")<BR> Dim sConn As New SqlConnection(sConnString)<BR><BR>and still get the same same.I can't figure out what the problem is, but try to evaluate this step by step.<BR>Add a new form to your project and simply put the following code in there: <BR><BR>Imports System.Configuration<BR><BR>Public Class test<BR>Inherits System.Web.UI.Page<BR><BR>Dim strConn as string<BR><BR>Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR>strConn = ConfigurationSettings.appSettings("sConnection")<BR>Dim myConn As New SqlConnection(strConn)<BR><BR>Compile your app and now it probably works. ;-)<BR><BR>Good luck!!!Imports System.Text<BR>Imports System.Web.Mail<BR>Imports System.Web.Security<BR>Imports System.Web.Configuration<BR>Imports System.Data<BR>Imports System.Data.SqlClient<BR>Imports System.Collections.Specialized <-- needed<BR><BR>Public Class login<BR> Public Sub btnLogin_click(ByVal src As System.Object, ByVal e As System.EventArgs)<BR> Dim appSetting As NameValueCollection<BR> Dim sConnString As String<BR> appSetting = CType(Context.GetConfig("appSettings"), NameValueCollection)<BR> sConnSTring = appSetting("sConnection").ToString()<BR> Dim sConn As New SqlConnection(sConnString)<BR><BR><BR><BR>--------------------------------------------------<BR><BR><BR> <appSettings><BR> <add key="sConnection" value="data source=;initial catalog=;password=;persist security info=True;user id=benefits;packet size=4096" /><BR> </appSettings>
