ASP.net declaring a SQL connection across multiple pages/ascx files

Discussion in 'Programming & Software Development' started by ACodingFettish, Jul 9, 2007.

  1. Elyzion

    Elyzion Member

    Joined:
    Oct 27, 2004
    Messages:
    7,449
    Location:
    Singapore
    Almost always the dll's but i have 2 projects at the moment im uploading the code behinds instead. Once those projects are more completed and require less updating ill start uploading the dll's.

    I have my own dedicated server so i don't mind uploading the code behinds.
     
  2. Luke212

    Luke212 Member

    Joined:
    Feb 26, 2003
    Messages:
    10,013
    Location:
    Sydney
    as in, in your office?
     
  3. Elyzion

    Elyzion Member

    Joined:
    Oct 27, 2004
    Messages:
    7,449
    Location:
    Singapore
    It's in Canada? I have 2 Dedicated Servers if you want to include my work one.
     
  4. OP
    OP
    ACodingFettish

    ACodingFettish Member

    Joined:
    Feb 7, 2004
    Messages:
    6,652
    Location:
    Brisbane
    bringing this one back from the dead, I'm trying to do what was advised but can't work it out:

    Web.config:

    PHP:
        <connectionStrings>
        <
    add name="Infobase" connectionString="Server=SERVER;Database=infobase;User ID=name;Password=pw" providerName="System.Data.SqlClient" />
        </
    connectionStrings>
    App_Code/ibcon.cs

    PHP:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;


    /// <summary>
    /// SQL Query
    /// </summary>
    public class ibcon
    {
        public 
    ibcon() { }
            public static 
    SqlConnection ReturnConnection() {

            
    string _connString ConfigurationManager.AppSettings["Infobase"];
            
    SqlConnection _ibcon = new SqlConnection(_connString);

            return 
    _ibcon;
        }
    }
    SQLTesdt.aspx.cs

    PHP:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;

    public 
    partial class SQLtest System.Web.UI.Page
    {
        protected 
    void Page_Load(object senderEventArgs e)
        {
            
    using (SqlConnection _ibcon ibcon.ReturnConnection())
            {

                try
                {

                    
    _ibcon.Open();

     
    SqlCommand InfobaseCommand = new SqlCommand("SELECT * FROM Staff Where CurrentEmploy = 1"_ibcon);
     
    SqlDataReader ResultsReader InfobaseCommand.ExecuteReader();
     while (
    ResultsReader.Read())
     {

         
    String name ResultsReader["FirstName"].ToString();

         
    Response.Write(name);

    }
                  
                }
                catch (
    Exception ex)
                {
                    
    Response.Write("didnt work : " ex);
                }

            }

        }
    }
    Throws this:

    help?
     
  5. chrisorm

    chrisorm Member

    Joined:
    Feb 14, 2002
    Messages:
    856
    Location:
    Sydney
  6. OP
    OP
    ACodingFettish

    ACodingFettish Member

    Joined:
    Feb 7, 2004
    Messages:
    6,652
    Location:
    Brisbane
    string _connString = ConfigurationManager.ConnectionStrings["Connection1"];

    is what effekt suggested - that spits out an error, something about not being able to convert it to a string :confused:

    I'll have a look at that website when I get home, cheers.
     
  7. chrisorm

    chrisorm Member

    Joined:
    Feb 14, 2002
    Messages:
    856
    Location:
    Sydney
    Yeah because what you get back from that isn't a string, it's a "ConnectionString" object that has a property on it that is the connection string as a string, but from memory you can use it directly as well.

    I'm on my mac here which doesn't seem to like loading MSDN pages too quickly so I can't confirm it right now.
     
  8. Elyzion

    Elyzion Member

    Joined:
    Oct 27, 2004
    Messages:
    7,449
    Location:
    Singapore
    <appSettings>
    <add key="Infobase" value="Server=SERVER;Database=infobase;User ID=name;Password=pw" providerName="System.Data.SqlClient" />
    </appSettings>
     
  9. chrisorm

    chrisorm Member

    Joined:
    Feb 14, 2002
    Messages:
    856
    Location:
    Sydney
    That won't work because the appSettings element doesn't allow "providerName" attribute. But without that his previous appSettings code would work, but it's not really the standard since they added the ConfigurationStrings section.
     
  10. Elyzion

    Elyzion Member

    Joined:
    Oct 27, 2004
    Messages:
    7,449
    Location:
    Singapore
    Oh i didn't even see provider name.

    But i would just have something like...

    Code:
            public SqlConnection Connection()
            {
                _connection = new SqlConnection();
    
                _connection.ConnectionString = ConfigurationManager.AppSettings["Infobase"];
                _connection.Open();
    
                return _connection;
            }
    <appSettings>
    <add key="Infobase" value="server=ip;database=dbname;User id=user;Password=pw"/>
    </appSettings>
     
    Last edited: Sep 3, 2007

Share This Page

Advertisement: