Showing posts with label class. Show all posts
Showing posts with label class. Show all posts

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

Tuesday, February 14, 2012

connection string

hi, i was just wondering what's wrong with my code. i have class library to handle all the classes of my asp.net project. when im compiling a class, i am getting an error "Error 1 The name 'ConfigurationManager' does not exist in the current context " . i have this code for that error message:

string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
im not sure about this because im new in asp.net. am i missing a reference here?if yes, what namespace should i include? by the way, i tested this directly in my asp.net age and it is working fine. when i use visual c#.net, that's where the error prompts me, and i cant build that class library.

Make sure this is referenced in the cs file:

using System.Configuration;

|||

Add this at the top of your pageusing System.Configuration;