Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I've made a connection in web.config.
HTML
<configuration>
	<appsettings />
  <connectionstrings>
    <add name="connection" connectionstring="server=.\sqlexpress; AttachdbFilename=Database.mdf; integrated security=true; user instance=true" />
    
  </connectionstrings></configuration>


Now how can I make a connection on page and access the database using buttons.
Posted

C#
Public void btnsearch_Click(object sender, EventArgs e)
 
         
    {
       SqlConnection con =  new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
        SqlCommand cmd = new SqlCommand("Your query", con);
        cmd.Parameters.AddWithValue("@id",Your parameters value if any);
        cmd.CommandType = CommandType.Text;
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
       
          //data retrieved in dt use it and do further functionality  
        
        
    }



http://forums.asp.net/t/1463330.aspx/1[^]
 
Share this answer
 
Comments
RaviRanjanKr 5-Nov-11 11:10am    
Nice, My 5+
P.Salini 6-Nov-11 23:40pm    
Thank you Ravi
you can create a class as
C#
 public class Db
{
	public Db()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static SqlConnection GetConnection()
    {
        SqlConnection cn = new SqlConnection();
        cn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["cnn"].ToString();
        cn.Open();
        return cn;
    }


}


and Use Of Connection
C#
string strid = "select max(id) from FileRequest";
            SqlCommand cmdid = new SqlCommand(strid,Db.GetConnection());
            SqlDataReader drid = cmdid.ExecuteReader();
            if (drid.Read())
            {
                id = Int32.Parse(drid[0].ToString());
            }
cmdid.Connection.Close();
 
Share this answer
 
You can connect to database through following code.

C#
string strCon=ConfigurationManager.ConnectionStrings["connection"].ToString();

SqlConnection con =  new SqlConnection(strCon);

strCon.open();
 
Share this answer
 
v2
you can call the connection string inside class file and call it in page like:-

in class file write a function

C#
public static string strcon = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString.ToString();

public static SqlConnection OpenConnection()
       {
           try
           {
               SqlConnection cn = new SqlConnection(strcon);
               if (cn.State == ConnectionState.Closed)
               {
                   cn.Open();
               }
               return cn;

           }
           catch (Exception ex)
           {
    
               throw new Exception(ex.Message);


           }
       }

just create object of this class and call this method you will make a connection
 
Share this answer
 
v2
You can connect to database through following code.
C#
string strCon=ConfigurationManager.ConnectionStrings["connection"].ToString();

SqlConnection con =  new SqlConnection(strCon);

strCon.open();
 
Share this answer
 
v2
you can use configuration manager as:
C#
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["strconnection"].ToString());
 
Share this answer
 
v3
Comments
RaviRanjanKr 5-Nov-11 11:13am    
[Edited]Corrected short text word to Normal[/Edited]
Suggestion :- hey Please don't use any kind of text short word like u instead of YOU.
member60 8-Nov-11 5:49am    
agreed Ravi.

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