Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
after putting connection string in web.config file what coding i have to do in page.aspx.cs page?
Posted

string connection_string = ConfigurationManager.ConnectionStrings["Your conn string name"].ConnectionString;


use this in code behind page to establish the connection..to database
 
Share this answer
 
I assume you want to retrieve the connection string value from web.config file in your aspx page. If that's the case take a look at the below links for example
How to: Read Connection Strings from the Web.config File[^]
Store connection string in web.config[^]

More examples here[^]
 
Share this answer
 
C#
System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

For this, add reference System.Configuration

http://msdn.microsoft.com/en-us/library/ms178411(v=vs.100).aspx[^]


Hope these helps...
 
Share this answer
 
try this.
C#
string str=ConfigurationManager.ConnectionStrings("keyname").ConnectionString
// keyname = your web.config file connection string key name
 
Share this answer
 
SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["TotalConnectionString"].ToString());
 
Share this answer
 
hi friend...

if your connection with MySQL then...

1. Add Name Space on your page e.g-

using MySql.Data.MySqlClient;

2. then make object of connection class..e.g-

MySqlConnection con = new MySqlConnection();

3.then configure your connection on your load event..e.g-

con.ConnectionString = ConfigurationManager.ConnectionStrings["GP"].ConnectionString;

//["GP"] is ur connection name that you make in web.config page e.g-

JavaScript
<connectionstrings>
		<add name="GP" connectionstring="server=localhost;User Id=root;password=password;database=demo; pooling=false" providername="MySql.Data.MySqlClient" />
</connectionstrings>


and In Sql Server there is minute change in namespace(using System.Data.SqlClient;) and make object of SqlConnection con =new SqlConnection.

Thanks I think It Helps You!!!
 
Share this answer
 
Hi,

String conString=ConfigurationManager.ConnectionStrings["DBName"].ToString()


Connection string in web.config file.
<add name="DBName" connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;User Id=sa;Password=test;MultipleActiveResultSets=True;"/>
 
Share this answer
 
Here is complete code for selecting data to datatable

public DataTable CheckLogin(string UserName, string Password)
        {
string ConnectionString = ConfigurationManager.ConnectionStrings["ConStrName"].ConnectionString;
            DataTable dt = new DataTable();
            string sql = "SELECT USER_ID,USER_NAME FROM USER_LOGIN WHERE (LOGIN_NAME = @LOGINNAME) AND (PASSWORD = @PASSWORD)";
            //Take the connection 
            SqlConnection connection = new SqlConnection(ConnectionString);
            try
            {
                //now open the connection
                connection.Open();
                //Take a command with query and connection
                SqlCommand command = new SqlCommand(sql, connection);
                //say the command type
                command.CommandType = CommandType.Text;
                // set parameter if any in the query 
                command.Parameters.Add("@LOGINNAME", SqlDbType.VarChar, 50).Value = UserName;
                command.Parameters.Add("@PASSWORD", SqlDbType.VarBinary, 50).Value = HashEncript(Password);
                //take a dataadapter for selecting data 
                SqlDataAdapter DataAd = new SqlDataAdapter(command);
                //fill the datatab 
                DataAd.Fill(dt);
                //close the connection 
                connection.Close();
                //return the table 
                return dt;
            }
            catch (Exception ex)
            {
                connection.Close();
                return dt;

                //or you can throw exception 
            }
        }
 
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