Showing posts with label configurationmanager. Show all posts
Showing posts with label configurationmanager. Show all posts

Thursday, March 22, 2012

ConnectionString Property

I am having trouble initializing my connection. This is the code:

DimDBConnPhone As NewSqlConnection(ConfigurationManager.AppSettings("DBConnPhone"))

Dim DBConnClient As NewSqlConnection(ConfigurationManager.AppSettings("DBConnClient"))

Dim Sqlcomm1 As New SqlCommand

Dim Sqlcomm2 As New SqlCommand

DBConnPhone.Open()

DBConnClient.Open()

Once I start debugging, it stops and give me the error "The ConnectionString Property was not initialized" Any suggestions?

If you want to retrieve connection strings, you should use ConfigurationManager.ConnectionStrings property, not ConfigurationManager.AppSettings. So the code changes to:

Dim DBConnPhone As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnPhone"))

Dim DBConnClient As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnClient"))

...

|||I did that and it said that Value of type 'System.Configuration.ConnectionStringSettings' cannot be converted to 'String'|||Dim DBConnClient As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnClient").ConnectionString)|||I didn't get any errors with that, but I also did not see where the newrecord was inserted into the table. I got to check my code some more.Thanks

Sunday, February 19, 2012

Connection String


public class Class1

{

public string sConType = System.Configuration.ConfigurationManager.AppSettings["conType"].ToString();

public string server = System.Configuration.ConfigurationManager.AppSettings["server"].ToString();

public string UID = System.Configuration.ConfigurationManager.AppSettings["UID"].ToString();

public string PWD = System.Configuration.ConfigurationManager.AppSettings["PWD"].ToString();

public string db = System.Configuration.ConfigurationManager.AppSettings["DB"].ToString();

private string sConStr;

.....

public Class1()

{

try

{

this.sConStr = "Server=" + this.server + ";Database=" + this.db + ";UID=" + this.UID + ";PWD=" + this.PWD + ";";

}

catch

{ }

}

.....

public void OpenConnection()

{

Class1 gr = new Class1();

conn = gr.CreateConnection();

conn.Open(); //ERROR

i'm getting the connection string from web.config file.i checked it with breakpoints.but it isnt working.The error message is "The ConnectionString property has not been initialized."

Where's my mistake?

Depending on which data provider driver you are using, the keywords in the connection string changes.

The keywords that you have specified are for use with the "MSDASQL" provider. I believe it is more common to use the "SQLOLEDB" provider. If you still prefer using the "MSDASQL" provider, you may need to set "Provider" property on the connection object. Please refer to the article below on MSDN for details.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/adosql/adoprg01_0ahx.asp

HTH,

Jimmy