1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

ASPX connect to database

Discussion in 'Databases' started by texanweb, Apr 19, 2009.

  1. #1
    OK... I have a database and some ASPX files.... how can I connect the 2 together?

    BTW, I am using Microsoft Visual Studio 2008 to edit the ASPX files and Microsoft Office Access 2007 foir the DB files.
     
    texanweb, Apr 19, 2009 IP
  2. feras80

    feras80 Well-Known Member

    Messages:
    302
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    110
    #2
    you have to define it in the webconfig file, then call it from your code.
    here is the script ( Config File):

    <configuration>
    <connectionStrings>

    <add name="dbcon"
    connectionString="Server=MyServer; Database=databasename; User Id=username; password= Pass"
    providerName="System.Data.SqlClient" />
    </connectionStrings>
    </configuration>

    and the usage:

    string dbConn = ConfigurationManager.ConnectionString["dbcon"].ToString();


    this is for SQL Sever database.
    For Access Database change the providerName to:
    System.Data.OleDb
    and the connectionString to the physical path of your access file.




     
    feras80, Apr 19, 2009 IP
  3. websolutions_777

    websolutions_777 Banned

    Messages:
    249
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello there,

    Connection string must be used in the web.config for connection

    Regards,

    WS
     
    websolutions_777, Apr 20, 2009 IP
  4. ChaosTrivia

    ChaosTrivia Active Member

    Messages:
    2,093
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    65
    #4
    It is prefered that the connection string will be in the web.config, but not necessary.
     
    ChaosTrivia, Apr 20, 2009 IP
  5. texanweb

    texanweb Active Member

    Messages:
    377
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Can yall give me examples so I can study them... I am brand new to asp, so I am still learning them.
     
    texanweb, Apr 20, 2009 IP
  6. mjad

    mjad Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This will help you for sure.
    just add this code in Web.config

    <configuration>
    <appSettings>

    <add key="Constr" value="User ID=sa; Password=xyz;Data Source=xyz;Initial Catalog=xyz; " />
    </appSettings>


    then here is the code to established the connection to database.
    private string ConnString()
    {
    try
    {

    return ConfigurationSettings.AppSettings["Constr"].ToString();
    }
    }
    catch ( Exception e )
    {
    throw (e);
    //return e.ToString ();
    }
    finally{}
    }


    here is the code block to execute reader.
    public SqlDataReader ReturnDataReader (string Sql)
    {
    try
    {
    objConn = new SqlConnection(this.ConnString());
    objConn.Open ();
    SqlCommand myCommand=new SqlCommand (Sql,objConn );
    aReader=myCommand.ExecuteReader();
    return aReader;
    }
    catch (Exception ex )
    {

    throw (ex);
    }
    }


    Let me know in case of any issue(s).
     
    mjad, Apr 22, 2009 IP