how can i create the app.config file in windows applications? [Resolved]

Posted by Sudheep.grandhe under C# on 1/4/2012 | Points: 10 | Views : 6471 | Status : [Member] | Replies : 13
Hi,

how can i create app.config file.I am following the previous project in that app.config file was created i just use the same thing .but it shows the error.

previous project app.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="provider" value="System.Data.SqlClient"/>
<add key ="cnStr"
value="Initial Catalog =PetStore09;Data Source =P2C00946;uid=trg09;pwd=trg09"/>
</appSettings>
</configuration>

my project app.config file.I am using sql server 2005 and data source=mars-104,database=petstore09,username=sa,password=password;how can i create the app.config file.please do the needful.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings >
<add key ="Provider" value ="System.Data.SqlClient"/>
<add key ="CnStr" value ="Initial Catalog=petstore09;Data Source=mars-104;User ID=sa;Password=password"/>
</appSettings>
</configuration>

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 1/4/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi

Are you using the below code

string provider = ConfigurationManager.AppSettings["Provider"].ToString();
string conStr = ConfigurationManager.AppSettings["CnStr"].ToString();
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = conStr;
return connection;

this is properly working


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 1/4/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
What is the Problem ?

Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

DbConnection con = DBConnection.GetConnection(); in this line it wont display the getconnection();

this is my problem

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 1/4/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
let me see the definition of this function

GetConnection()

Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Here is the coding of the getconnection().please find it.

public static class DBConnections
{
static string provider = ConfigurationManager.AppSettings["Provider"].ToString();
static string conStr = ConfigurationManager.AppSettings["CnStr"].ToString();
public static DbConnection connection = null;
public static DbCommand command = null;
static DbProviderFactory factory;

public static DbConnection GetConnection()
{
string provider = ConfigurationManager.AppSettings["Provider"].ToString();
string conStr = ConfigurationManager.AppSettings["CnStr"].ToString();
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = conStr;
return connection;
}

public static void CloseConnection()
{
if (connection != null)
connection.Close();
}

public static DbCommand GetCommand()
{
string provider = ConfigurationManager.AppSettings["provider"].ToString();
factory = DbProviderFactories.GetFactory(provider);
DbCommand command = factory.CreateCommand();
command.Connection = connection;
return command;
}


Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 1/4/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
This

 ConfigurationManager.AppSettings["Provider"].ToString(); 


needs to be

ConfigurationManager.AppSettings.Get("Provider");


Change the others also

Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Thanks for replying to me.If changed over there it wont work.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 1/4/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Could you please provide the error that you are getting. I didn't get any error while trying the same code.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

Thanks for replying to me.DbConnection con = DBConnection.GetConnection(); in this line it wont display the getconnection(); after dbconnection it wont display the getconnection.i wrote the connection in dbconneciton.cs in above i posted the dbconneciton.cs coding.here is my app.config file code


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings >
<add key ="petstore09" value ="C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data.mdb"/>
<add key ="petstore09" value ="Initial Catalog=petstore09;Data Source=mars-104;User Name=sa;Password=password"/>
</appSettings>
</configuration>

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/4/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

it shows the error are

Error 1 Non-invocable member 'System.Configuration.ConfigurationManager.AppSettings' cannot be used like a method.


Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/4/2012 [Member] Starter | Points: 25

Up
0
Down
HI,

For me it's not working.I am using the same code.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 1/5/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi,

What was the issue.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 1/5/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

In this line string conStr = ConfigurationManager.AppSettings["CnStr"].ToString();.i have to giving the databasename as "petstore09".i am giving the other one.that's all my issue was resolved.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response