Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 117,610 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,958 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Trouble opening mdb files in C#?

 
Reply to this topicStart new topic

Trouble opening mdb files in C#?

pcnote
post 5 Jul, 2008 - 09:22 AM
Post #1


New D.I.C Head

*
Joined: 24 Feb, 2007
Posts: 30


My Contributions


This is the error that I am getting. I tried to fix this error and do not know where else to fix this. This is what it came up on IE.
You can find "Northwind.mdb" off the internet. Make sure to rename it to Nwind.mdb as the program reads it to open. I have to use the orders table and display customers name and freight prices. I also need to change the connections string.


Syntax error in FROM clause.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Syntax error in FROM clause.

Source Error:


Line 29: //Create a data reader which will read each record from the select
Line 30: //command in a forward-only, read-only mode
Line 31: OleDbDataReader rdr = cmd.ExecuteReader();
Line 32:
Line 33: //Loop for every record

Even with the updated code:

try{

catch (InvalidOperationException e) {
//error handling for an already open connection
//examine e.HResult, e.Message
}
catch (OleDbException e) {
foreach(OleDbError err in e.Errors) {
//examine e.Number, e.Message
}
}
catch (Exception e) {
//general error handling
}
if (con != null) {
//code to work with a valid databaseconnection
}

It gave me 2 errors.
blink.gif

CODE

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat="server" Language="C#">

private String _TableName;

public String TableName {
   get {
     return _TableName;
   }
   set {
     _TableName = value;
   }
}

</script>
<%
   //Create a connection to the local NorthWind database
     OleDbConnection con =
        new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" +
           "Data Source=C:\\Users\\Julius\\Desktop\\Nwind.mdb;" +
           "User ID=;Password=;");
   con.Open();   [quote]<==== The problem is here.[/quote]

   //Create a simple command that will select every record from a table
   OleDbCommand cmd = new OleDbCommand("Select * from " + _TableName, con);

   //Create a data reader which will read each record from the select
   //command in a forward-only, read-only mode
   OleDbDataReader rdr = cmd.ExecuteReader();

   //Loop for every record
   while(rdr.Read()) {

     //Loop through every column - 0 to FieldCount
     for( int fldNum = 0; fldNum < rdr.FieldCount; fldNum++ ) {

       String strFieldItem = "";

       //If the value in the column is not null, retrieve it as
       //a String value
       if(!rdr.IsDBNull(fldNum)) {
         strFieldItem = rdr.GetString(fldNum);
       }

       //Write out the value
       Response.Write(strFieldItem);
       if (fldNum < rdr.FieldCount - 1)
         Response.Write(", ");
       else
         Response.Write("<br>");
     }
   }
   //Close the data reader and the connection - no need to close
   //the command object
   rdr.Close();
   con.Close();
%>


This post has been edited by pcnote: 5 Jul, 2008 - 09:59 AM
User is offlineProfile CardPM

Go to the top of the page


tody4me
post 9 Jul, 2008 - 12:39 PM
Post #2


Only Jenny Craig makes thin clients...

Group Icon
Joined: 12 Apr, 2006
Posts: 1,056



Thanked 3 times

Dream Kudos: 100
My Contributions


check out connectionstrings.com for the proper connection string for Access. You want the version of Access that is on your computer, and the format for the string for opening a file.
Secondly, the error about the Select clause causing an error. The table name has not been selected, therefore you're actually passing "Select * from " to the SQL parser, which returns the error. Change
CODE

("Select * from " + _TableName, con);

to
CODE

(string.Format("Select * from {0}", _TableName, con);

but check that _TableName != string.empty first.

This post has been edited by tody4me: 9 Jul, 2008 - 12:40 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/7/08 11:41PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month