Click here to Skip to main content
12,447,096 members (50,507 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
after putting connection string in web.config file what coding i have to do in page.aspx.cs page?
Posted 1-Jan-13 19:54pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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


use this in code behind page to establish the connection..to database
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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...
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

try this.
string str=ConfigurationManager.ConnectionStrings("keyname").ConnectionString
// keyname = your web.config file connection string key name
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

SqlConnection Cn = new SqlConnection(ConfigurationManager.ConnectionStrings["TotalConnectionString"].ToString());
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

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-

<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!!!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

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;"/>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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 
            }
        }
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160811.3 | Last Updated 2 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100