Link to home
Start Free TrialLog in
Avatar of martgriff
martgriff

asked on

Connecting to an ODBC DNS Data Source

I am trying to connect to an Access mdb file using ODBC DNS, Can you please take alook at the code attched and see if you can see where im going wrong.

when i run the web app and press the button that calls the conection i get the following error on "conn.ConnectionString = strConnectionString;":
An OLE DB Provider was not specified in the ConnectionString.  An example would be, 'Provider=SQLOLED

All commects welcome.........................
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.OleDb; // added library
using Microsoft.Data.Odbc; //added libary
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void btnGo_Click(object sender, EventArgs e)
        {
 
            // set the connection string
            //note: the @ symbol is needed before the string to prevent from having to use double back slashes
            string strConnectionString = "@DNS=PHJ;Uid=;Pwd=;";
 
                        // get the search contents
            string strSearchValue = txtSearch.Text;
 
            // create the sql query using the search value - note: access over oledb requires square brackets round table name
            string strSql = "SELECT OrderID, ServiceID FROM [Order2Service] WHERE ServiceID LIKE '%" + strSearchValue + "%';";
 
 
            // create a new connection object
            OleDbConnection conn = new OleDbConnection();
 
            // set the connection string from the global variable
            conn.ConnectionString = strConnectionString;
 
            // create a new command object using the connection
            OleDbCommand command = new OleDbCommand();
            command = conn.CreateCommand();
            command.CommandText = strSql; // set the sql query
 
            // create the dataset
            DataSet ds = new DataSet();
 
            // set and run the query - the adaptor executes the command against the database set in the command
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter = new OleDbDataAdapter(command);
            adapter.Fill(ds);
 
            // bind and show the results in the grid
            grdResults.DataSource = ds.Tables[0];
            grdResults.DataBind();
 
        }
    }
}

Open in new window

Avatar of William Elliott
William Elliott
Flag of United States of America image

DSN not dns
Example connection strings can be found here: -
http://www.connectionstrings.com/?carrier=access
Avatar of martgriff
martgriff

ASKER

I have been on that website and if you look at my code i have put what they recommed but it is still failing, i have never connected to an DSN before so im not sure if there is somthing else missing?
ASKER CERTIFIED SOLUTION
Avatar of William Elliott
William Elliott
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial