|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
ConnectionString property has not been initializedof the code from my web page where it works fine. I can connect fine from my Sql Query Analyser to both my local Sql Server and off Sql Server machines. But I am getting the following error from my "objConn.Open()". Is this a problem from going from my web to a normal application? The error is: The ConnectionString property has not been initialized My code is: Imports System.Data Imports System.Data.SqlClient .... Dim dbReader As SqlDataReader Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("Persist Security Info=False;Data Source=balair.interez.com;Initial Catalog=InterezData;User ID=xxx;Password=xxxx;") Dim objConn As New SqlConnection(ConnectionString) Dim CommandText As String = "GetCandidateSearchDrops" Dim objCmd As New SqlCommand(CommandText, objConn) objCmd.CommandType = CommandType.StoredProcedure With objCmd.Parameters .Add("@CompanyID", SqlDbType.Int).Value = 153973 End With objConn.Open() dbReader = objCmd.ExecuteReader Thanks, Tom > I can connect fine from my Sql Query Analyser to both my local Sql Server What your code is doing is trying to fetch a key from the .config file whose> and off Sql Server machines. > > But I am getting the following error from my "objConn.Open()". > Dim dbReader As SqlDataReader > Dim ConnectionString As String = > System.Configuration.ConfigurationSettings.AppSettings("Persist Security > Info=False;Data Source=balair.interez.com;Initial Catalog=InterezData;User > ID=xxx;Password=xxxx;") > Dim objConn As New SqlConnection(ConnectionString) name is the content of your connection string. Here: In App.Config: <add key="connectionString" value="your actual connection string here" /> In your VB code: Imports System.Configuration Imports System.Data Imports System.Data.SqlClient Public Class Foo Public Sub DoStuff() Dim cnstr As String = ConfigurationSettings.AppSettings("connectionString") Dim cn As New SqlConnection(cnstr) ' etc. End Class You see, on the first "Dim" line, you are asking for the value of the item whose key is named "connectionString". The value that is returned is whatever is in the value attribute in the config file. Then you can go about your business :) -- Peace & happy computing, Mike Labosh, MCSD MCT "Escriba coda ergo sum." -- vbSensei "Mike Labosh" <mlabosh_at_hotmail.com> wrote in message Catalog=InterezData;Usernews:eXWcY6PHGHA.344@TK2MSFTNGP11.phx.gbl... > > I can connect fine from my Sql Query Analyser to both my local Sql Server > > and off Sql Server machines. > > > > But I am getting the following error from my "objConn.Open()". > > > Dim dbReader As SqlDataReader > > Dim ConnectionString As String = > > System.Configuration.ConfigurationSettings.AppSettings("Persist Security > > Info=False;Data Source=balair.interez.com;Initial Show quoteHide quote > > ID=xxx;Password=xxxx;") So what I need to do in my application code is just put the connection> > Dim objConn As New SqlConnection(ConnectionString) > > > What your code is doing is trying to fetch a key from the .config file whose > name is the content of your connection string. > > Here: > > In App.Config: > > <add key="connectionString" value="your actual connection string here" /> > > > In your VB code: > > Imports System.Configuration > Imports System.Data > Imports System.Data.SqlClient > > Public Class Foo > > Public Sub DoStuff() > > Dim cnstr As String = > ConfigurationSettings.AppSettings("connectionString") > Dim cn As New SqlConnection(cnstr) > ' etc. > > End Class > > You see, on the first "Dim" line, you are asking for the value of the item > whose key is named "connectionString". The value that is returned is > whatever is in the value attribute in the config file. Then you can go > about your business :) > string directly into the variable. I tried that and it worked. Thanks, Tom Show quoteHide quote > > -- > Peace & happy computing, > > Mike Labosh, MCSD MCT > "Escriba coda ergo sum." -- vbSensei > > |
|||||||||||||||||||||||