Solved

how to connect to a mdb via odbc with c# in a webapplication

Posted on 2004-08-29
1
1,158 Views
Last Modified: 2008-03-10
can anybody give me an example of connecting to a ms access database via odbc in c#.

i want to know how i can simply fill some textboxes of a webapp out of a database with some SELECT-queries.

thanxs
h
0
Comment
Question by:1hoschi
1 Comment
 
LVL 35

Accepted Solution

by:
YZlat earned 125 total points
Comment Utility
first create odbc for your access database. Give it a name, for example MyODBC. Then go to the root directory of your C# application and find web.config file. Open it and add the following to <configuration section, right below</system.web> section:

 <appSettings>
      <add key="dsn" value="dsn=MyODBC;" />
  </appSettings>

Then create a module, something like MyFunctions.cs file with the following code:



    Public OdbcConnection GetConnection()
{
        string strDSN= ConfigurationSettings.AppSettings("dsn");
        OdbcConnection* Conn = new OdbcConnection(strDSN);        
        Conn.Open();

        Return Conn;
}

    Public OdbcDataReader GetData(string sql )
{
        OdbcConnection* conn= GetConnection();

      OdbcCommand* cmd = new OdbcCommand(sql, conn);
      OdbcDataReader* dr = cmd->ExecuteReader();

      Return dr;
}

Then from any of your web forms' code-behind files you can declare a datareader, sql string and simply call GetData function:

string sql="Select * from Table1";
OdbcdataReader dr=GetData(sql);

While dr->Read(){
/*retrieve the data and display it*/
txtName.Text=dr["FullName"]

}
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now