'System.Data.OleDb.OleDbException' when creating a new DataSet..

B

Brian

Funny you mention that because that's why i didn't include more
information.. for some reason, i couldn't compile this try.. catch
block.

Can anyone give me the correct structure if i am missing something?
Does it have to be in a Main() method or anything? Haven't really used
try.. catch blocks much before.


try
{
// load categories
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=photoDB.MDB";
string strSQL = "SELECT * FROM categories" ;

// create Objects of ADOConnection and ADOCommand
OleDbConnection myConn = new OleDbConnection(strDSN);
OleDbDataAdapter myCmd = new OleDbDataAdapter( strSQL, myConn );
myConn.Open();

DataSet dtSet = new DataSet();
myCmd.Fill( dtSet, "categories" );
DataTable dTable = dtSet.Tables[0];

foreach( DataRow dtRow in dTable.Rows )
{
cboCategory.Items.Add( dtRow["category"].ToString());
}

myConn.Close();
}
catch(Exception e)
{
MessageBox.Show("" + e.Message);
}
 
G

Guest

Brian,

You have some errors in your code. I have provided some sample code below
to help you out. Just keep in mind that you can trap more specific errors by
adding more catch clauses with more specific Exception classes (e.g.
OleDbException).

Hope this helps
----------------
try
{
string strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;DataSource=photoDB.MDB";
string strSQL = "SELECT * FROM categories" ;
OleDbConnection conn = new OleDbConnection(strDSN);
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,conn);
DataSet ds = new DataSet();
da.Fill(ds);

comboBox1.DataSource = ds.Tables[0];
comboBox1.DisplayMember = "Category"; //name of the field that you want to
display
}
//add more specific catches here ...most specific to least specific
catch(Exception e)
{
MessageBox.Show("" + e.Message);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top