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

No comments:

Post a Comment