Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello all,

i am creating a windows application. in which i want to use sql database...for this i want to place connection string in a file (as like web.cinfig in web application).

but dont know where to place. Can any one suggest me the best ways to do that....??
Posted

Assuming that you already have a config file with the connection string value set,

C#
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings      ["MyConnectionString"].ToString();
DataTable dt2 = new DataTable();
SqlCommand cmd = new SqlCommand("dbo.SP_GetData", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);

adapter.Fill(dt2);


Now dt2 contains the data from the Db.

Your config file shouldbe like this
XML
<configuration>
  <connectionStrings>
    <add name="MyConnectionString" connectionString="Data Source=xxxx;Initial Catalog=xxxxx;Integrated Security=True;";/>
  </connectionStrings>
</configuration>
 
Share this answer
 
Comments
abhishekagrwl25 15-Apr-13 5:37am    
i am using this code but still haveing an exception tha t "Object reference not set to an instance of an object.".
Naz_Firdouse 15-Apr-13 5:40am    
Debug your code and see where it throws that exception..
It mainly happens when you are assigning some value to an object before initializing it
abhishekagrwl25 15-Apr-13 5:51am    
SqlConnection con = new SqlConnection();
ConfigurationManager.RefreshSection("connectionstring");
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();


when compiling the last line...it showing error

this is my code....
Naz_Firdouse 15-Apr-13 5:54am    
are you sure that the name in the config file is "ConnStr"
abhishekagrwl25 15-Apr-13 6:01am    
<connectionstrings>
<add name="ConnStr" connectionstring="Data Source=SUSHIL\ABHISHEKSQLDB;Initial Catalog=test;User ID=sa;Password=hpbose@123;Pooling=False">


this is the code for app.config..
App.config has similar support like web.config, but for winforms.
Look here[^]
 
Share this answer
 
Normally, you put it in a standard configuration file of the application. Please read this: http://msdn.microsoft.com/en-us/library/ms254494.aspx[^].

Good luck,
—SA
 
Share this answer
 
 
Share this answer
 
Comments
abhishekagrwl25 15-Apr-13 5:17am    
i have created connection string in app.config file...but now when i write code to use conenction string, i am getting "object reference set to null exception."

my code is..:

SqlConnection con = new SqlConnection();
ConfigurationManager.RefreshSection("connectionstring");
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();


suggest me the solution..
add app.config file in your application
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900