I have a hard time accessing data from SQL and MS Acess database using c#. Please help. My XP computer has MS Access 10 (XP) installed with no logon password and it also has MS SQL 7.0 client (personal) edition installed. The following is the c# code to get the MS Access table. The table has one record.
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
string source = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"User ID=Admin;Password=;" +
"Database=MyAccessFile.mdb";
string select = "SELECT RecordID FROM MyTable";
OleDbConnection cnn = new OleDbConnection(source);
cnn.Open();
OleDbCommand cmd = new OleDbCommand(select, cnn);
OleDbDataReader rdr = cmd.ExecuteReader();
MessageBox.Show("The key field data is " + rdr.GetString(0),"Data Reader"
rdr.Close();
cnn.Close();
When running, a message showed up: "An unhandled exception of type (System.Data.OleDb.OleDbException) occurred in System.Data.dll."
The line OleDbCommand cmd = new OleDbCommand(select, cnn); is then highlighted. Then another message box showed: There is no source code available for the current location.
Then I looked into the debug output window it showed:
Unhandled Exception: System.Data.OleDb.OleDbException: Could not find installable ISAM.
Do you know what I did wrong or what I should do to fix the problem? How do I install ISAM? Thru MSAcces or Visual Studio Development Environment?
Then I changed the code and tried to get data from the sql server. I used the default user id of sa. The table also has one record. C# code:
string source = "server=(local);" +
"uid=sa;" +
"database=MyFileSQL";
string select = "SELECT RecordID FROM MyTable";
SqlConnection cnn = new SqlConnection(source);
cnn.Open();
SqlCommand cmd = new SqlCommand(select, cnn);
SqlDataReader rdr = cmd.ExecuteReader();
object o = rdr["RecordID"];
MessageBox.Show("The key field data is " + o.ToString(),"Data Reader"
rdr.Close();
cnn.Close();
When running, a message showed up: "An unhandled exception of type System.NotSupportedException occurred in System.Data.dll. Additional information: Invalid attempt to read when no data is there."
Then another message box showed: There is no source code available for the current location
object o = rdr["RecordID"]; is highlighted.
Can anybody explain to me what the problems are? I thought I did a very basic code to get data from the database files. It simply did not work. Thanks in advance for your reply.
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
string source = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"User ID=Admin;Password=;" +
"Database=MyAccessFile.mdb";
string select = "SELECT RecordID FROM MyTable";
OleDbConnection cnn = new OleDbConnection(source);
cnn.Open();
OleDbCommand cmd = new OleDbCommand(select, cnn);
OleDbDataReader rdr = cmd.ExecuteReader();
MessageBox.Show("The key field data is " + rdr.GetString(0),"Data Reader"
rdr.Close();
cnn.Close();
When running, a message showed up: "An unhandled exception of type (System.Data.OleDb.OleDbException) occurred in System.Data.dll."
The line OleDbCommand cmd = new OleDbCommand(select, cnn); is then highlighted. Then another message box showed: There is no source code available for the current location.
Then I looked into the debug output window it showed:
Unhandled Exception: System.Data.OleDb.OleDbException: Could not find installable ISAM.
Do you know what I did wrong or what I should do to fix the problem? How do I install ISAM? Thru MSAcces or Visual Studio Development Environment?
Then I changed the code and tried to get data from the sql server. I used the default user id of sa. The table also has one record. C# code:
string source = "server=(local);" +
"uid=sa;" +
"database=MyFileSQL";
string select = "SELECT RecordID FROM MyTable";
SqlConnection cnn = new SqlConnection(source);
cnn.Open();
SqlCommand cmd = new SqlCommand(select, cnn);
SqlDataReader rdr = cmd.ExecuteReader();
object o = rdr["RecordID"];
MessageBox.Show("The key field data is " + o.ToString(),"Data Reader"
rdr.Close();
cnn.Close();
When running, a message showed up: "An unhandled exception of type System.NotSupportedException occurred in System.Data.dll. Additional information: Invalid attempt to read when no data is there."
Then another message box showed: There is no source code available for the current location
object o = rdr["RecordID"]; is highlighted.
Can anybody explain to me what the problems are? I thought I did a very basic code to get data from the database files. It simply did not work. Thanks in advance for your reply.