Thursday, March 22, 2012

ConnectionString property not initialized

I am getting an error message that says that my connection string has not been intialized I have initialized it.

Dim AirliquidiConnAsNew SqlClient.SqlConnection(ConfigurationManager.AppSettings("AirliquidiDatabase"))

Any suggestions??

Check out this article on using the connectionStrings element in the configuration file. Good luck!

http://weblogs.asp.net/jgaylord/archive/2005/05/12/406639.aspx

|||

with that line you've only created a connection instance, but have not initialized it. You'll need to actually use it before it will work now.

here's what you need to do. You need to add a "using" statement to the code and handle the data retrieval inside of it.

Dim connString As String = _
ConfigurationManager.ConnectionStrings(connStringName).ConnectionString

'Create a SqlConnection instance
Using myConnection As New SqlConnection(connString)
'Specify the SQL query
Const sql As String = "SELECT * FROM Customers"

'Create a SqlCommand instance
Dim myCommand As New SqlCommand(sql, myConnection)

'Get back a DataSet
Dim myDataSet As New DataSet

'Create a SqlDataAdapter instance
Dim myAdapter As New SqlDataAdapter(myCommand)
myAdapter.Fill(myDataSet)

'Bind the DataSet to the GridView
gvCustomers.DataSource = myDataSet
gvCustomers.DataBind()

'Close the connection
myConnection.Close()
End Using

You can visit http://aspnet.4guysfromrolla.com/articles/110905-1.aspx for a more thorough explanation (the page I got this code from)

|||I have this information in my web.config|||

This is what I have. Would I be putting a using statement in the Page_Load?

ProtectedSub Button1_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles Button1.Click

Dim connAirliquidiAsNew SqlClient.SqlConnection(ConfigurationManager.AppSettings("AirliquidiDatabase"))

Dim cmdInsertLoginAsNew SqlClient.SqlCommand("Insert into Logins(ID, Username, LoginDate) values (@.ID, @.Username,@.LoginDate)", connAirliquidi)

Dim activeUserAsString

Dim IDAs Guid = System.Guid.NewGuid

Dim cmdCheckUserCredentialsAsNew SqlClient.SqlCommand("select * From Usernames where(username=@.username and password=@.password)", connAirliquidi)

'If user did not fill in a username or password

If txtUsername.Text =""Or txtPassword.Text =""Then

Message("Please fill in a username/password")

Else

Try

connAirliquidi.Open()

cmdCheckUserCredentials.Parameters.AddWithValue("@.Username", Trim(txtUsername.Text))

cmdCheckUserCredentials.Parameters.AddWithValue("@.password", Trim(txtPassword.Text))

SelectCase activeUser

CaseTrue

cmdInsertLogin.Parameters.AddWithValue("@.ID", ID)

cmdInsertLogin.Parameters.AddWithValue("@.Username", Trim(txtUsername.Text))

cmdInsertLogin.Parameters.AddWithValue("@.LoginDate",Date.Now())

Session("Username") = Trim(txtUsername.Text)

FormsAuthentication.SetAuthCookie(txtUsername.Text,False)

Session("ID") = ID.ToString

Response.Redirect("ALSI.aspx?ID={" & ID.ToString &"}")

CaseFalse

Message("Inactive user")

CaseElse

Message("Invalid Username and/or Password")

EndSelect

Catch exAs Exception

Message("Error authenticating user. Please try again.")

Finally

connAirliquidi.Close()

EndTry

EndIf

EndSub

|||

you have to open the connection before you use the sql command. moveconnAirliquidi.Open() directly below this line

Dim connAirliquidiAsNew SqlClient.SqlConnection(ConfigurationManager.AppSettings("AirliquidiDatabase"))

which also means you'll have to move the connAirliquidi.close() down to directly above the End Sub line.

|||

I did that and I am still getting the same error message.

|||Can you post a snap from your web.config file where you've actually declared the connection string ?|||<configuration>

<appSettings>

<addkey="customerservice"value="abetha@.airsep.com"/>

<addkey="smtpserver"value=""/>

<addkey="AirliquidiConn"value="Airliquidi id=**;data source=SEPSQL;persist security info=True;initial catalog=Usernames;password=****"/>

</appSettings>

<connectionStrings>

<addname="AirliquidiConn"connectionString="Data Source=SEPSQL;Database=Airliquidi;User ID=**;Password=*****"

providerName="System.Data.SqlClient" />

</connectionStrings>

|||

Correct me if I'm wrong but in your earlier posts you've written the name of your connection string to be "AirliquidiDatabase" and you've set it to be used from appSettings. Now, the snap you've posted is not having a connection string with the name you might be using in your code. Both the connection strings that you've set in your config file are named "AirliquidiConn".

|||

Thank you for catching that. I have changed it to AirliquidiConn and I am still getting the same error.

No comments:

Post a Comment