Click here to Skip to main content
15,881,709 members
Home / Discussions / C#
   

C#

 
AnswerAnother idea Pin
RomkaFromUa25-Sep-06 6:00
RomkaFromUa25-Sep-06 6:00 
QuestionAchieving high refresh rate on C# winform application Pin
SwEr22-Sep-06 7:18
SwEr22-Sep-06 7:18 
AnswerRe: Achieving high refresh rate on C# winform application Pin
Judah Gabriel Himango22-Sep-06 8:39
sponsorJudah Gabriel Himango22-Sep-06 8:39 
AnswerRe: Achieving high refresh rate on C# winform application Pin
zaccheus22-Sep-06 9:37
zaccheus22-Sep-06 9:37 
AnswerRe: Achieving high refresh rate on C# winform application Pin
led mike22-Sep-06 9:50
led mike22-Sep-06 9:50 
QuestionRe: Achieving high refresh rate on C# winform application Pin
SwEr24-Sep-06 1:18
SwEr24-Sep-06 1:18 
AnswerRe: Achieving high refresh rate on C# winform application Pin
darkelv24-Sep-06 6:29
darkelv24-Sep-06 6:29 
QuestionProblem connecting to an MS Access Database [modified] Pin
TheBlindWatchmaker22-Sep-06 6:45
TheBlindWatchmaker22-Sep-06 6:45 
Greetings!

I'm trying to connect to a database and read certain fields from it. I'm unable to read any fields from it and would appreciate your help debugging/fixing the problem.

Here is what I have:

1. A file that acts has the database interface code:

<br />
public class AppDatabaseInterface<br />
{<br />
<br />
// Directory names<br />
private string m_cstrDefaultdbDir = "C:\\";<br />
<br />
// File names<br />
private const string m_cstrDBFileName = "DefaultDatabase.mdb";<br />
<br />
private System.Data.OleDb.OleDbConnection m_dbConn;<br />
<br />
public AppDatabaseInterface()<br />
{<br />
// Determine the App Root directory when this object is constructed<br />
System.Reflection.Assembly assm =<br />
System.Reflection.Assembly.GetAssembly(System.Type.GetType("AppDatabase.AppDatabaseInterface"));<br />
m_cstrDefaultdbDir = System.IO.Path.GetDirectoryName(assm.CodeBase.ToString());<br />
m_cstrDefaultdbDir = m_cstrDefaultdbDir.Remove(0, 6); // Removes the leading file:\ header from the path.<br />
<br />
OpenDatabase();<br />
}<br />
<br />
<br />
private void OpenDatabase()<br />
{<br />
lock (this)<br />
{<br />
// If the database connection hasn't been initialized, create it.<br />
// If it exists and is currently connected, close the connection<br />
// as we are about to establish a new connection.<br />
if (m_dbConn == null)<br />
{<br />
m_dbConn = new OleDbConnection();<br />
}<br />
else<br />
{<br />
if (m_dbConn.State != ConnectionState.Closed)<br />
{<br />
m_dbConn.Close();<br />
}<br />
}<br />
<br />
// Create the new connection string and try and attach to the new database.<br />
try<br />
{<br />
m_dbConn.ConnectionString = string.Format(<br />
@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}\{1};",<br />
m_cstrDefaultdbDir, m_cstrDBFileName);<br />
}<br />
catch<br />
{<br />
Console.WriteLine("Could not open database");<br />
}<br />
m_dbConn.Open();<br />
<br />
}<br />
}<br />


2. The "Makes" Method

Here's the "Makes" method in the AppDatabaseInterface.cs file

<br />
 public string[] Makes<br />
        {<br />
            get<br />
            {<br />
                OleDbDataReader dbr = null;<br />
                ArrayList Makes = new ArrayList();<br />
<br />
                OleDbCommand cmd = new OleDbCommand(string.Format("SELECT * FROM {0};", "Speakers"), m_dbConn);<br />
                dbr = cmd.ExecuteReader();<br />
<br />
                // Read each record one at a time ...<br />
                while (dbr.Read())<br />
                {<br />
                    // Load our arrays with the Field names<br />
                    // and values.  NOTE: Reloading the field<br />
                    // names over and over again is probably<br />
                    // redundant, but I'm not sure that the order<br />
                    // column names are returned is fixed.<br />
                    for (int it = 0; it < dbr.FieldCount; it++)<br />
                    {<br />
                        string strField = dbr.GetName(it);<br />
                        if (strField == "MAKE")<br />
                        {<br />
                            string make = (string)dbr.GetValue(it);<br />
                            bool found = false;<br />
                            foreach (object o in Makes)<br />
                            {<br />
                                if ((string)o == make)<br />
                                {<br />
                                    found = true;<br />
                                    break;<br />
                                }<br />
                            }<br />
                            if (found)<br />
                                Makes.Add(make);<br />
                            break;<br />
                        }<br />
<br />
                    }<br />
<br />
                }<br />
                string[] strMakes = new string[Makes.Count];<br />
                int i= 0;<br />
                foreach (object o in Makes)<br />
                {<br />
                    strMakes[i++] = (string)o;<br />
                }<br />
                return strMakes;<br />
            }<br />
        }<br />


3. The actual UI form

<br />
public partial class frmAppDatabase: Form<br />
{<br />
AppDatabaseInterface m_database;<br />
<br />
public frmSpeakerDatabase()<br />
{<br />
InitializeComponent();<br />
<br />
m_database = new AppDatabaseInterface();<br />
<br />
string[] strSpeakerMakes = m_database.Makes;<br />
<br />
for (int i = 0; i < strSpeakerMakes.Length; i++)<br />
{<br />
cbSpeakerMakes.Items.Add(strSpeakerMakes[i]);<br />
}<br />
}<br />


What I want this portion of the code to do is to fill in the combobox with the various items from the specified field in the database. However, I see nothing.

The Access file is in the \bin\debug directory of the project.

Could you please advice me as to what is wrong?

Thanks!


-- modified at 13:54 Friday 22nd September, 2006
AnswerRe: Problem connecting to an MS Access Database Pin
Alaric_22-Sep-06 8:40
professionalAlaric_22-Sep-06 8:40 
GeneralRe: Problem connecting to an MS Access Database Pin
TheBlindWatchmaker22-Sep-06 9:15
TheBlindWatchmaker22-Sep-06 9:15 
GeneralRe: Problem connecting to an MS Access Database Pin
Alaric_22-Sep-06 10:33
professionalAlaric_22-Sep-06 10:33 
GeneralRe: Problem connecting to an MS Access Database [modified] Pin
Alaric_22-Sep-06 11:03
professionalAlaric_22-Sep-06 11:03 
GeneralRe: Problem connecting to an MS Access Database Pin
TheBlindWatchmaker22-Sep-06 11:40
TheBlindWatchmaker22-Sep-06 11:40 
GeneralRe: Problem connecting to an MS Access Database Pin
Alaric_22-Sep-06 11:58
professionalAlaric_22-Sep-06 11:58 
QuestionWatching Data Send and Recieved for a particular internet connection Pin
logicon22-Sep-06 6:31
logicon22-Sep-06 6:31 
AnswerRe: Watching Data Send and Recieved for a particular internet connection Pin
Judah Gabriel Himango22-Sep-06 6:42
sponsorJudah Gabriel Himango22-Sep-06 6:42 
Questionhow to use List Pin
waheed awan22-Sep-06 5:46
waheed awan22-Sep-06 5:46 
AnswerRe: how to use List Pin
Judah Gabriel Himango22-Sep-06 5:51
sponsorJudah Gabriel Himango22-Sep-06 5:51 
AnswerRe: how to use List Pin
User 665822-Sep-06 5:52
User 665822-Sep-06 5:52 
Questionformat of MIB_IPFORWARDROW structure? Pin
b_girl22-Sep-06 5:32
b_girl22-Sep-06 5:32 
AnswerRe: format of MIB_IPFORWARDROW structure? Pin
Guffa22-Sep-06 6:39
Guffa22-Sep-06 6:39 
GeneralRe: format of MIB_IPFORWARDROW structure? Pin
b_girl22-Sep-06 7:19
b_girl22-Sep-06 7:19 
AnswerRe: format of MIB_IPFORWARDROW structure? Pin
Guffa22-Sep-06 9:25
Guffa22-Sep-06 9:25 
GeneralRe: format of MIB_IPFORWARDROW structure? Pin
b_girl22-Sep-06 9:40
b_girl22-Sep-06 9:40 
AnswerRe: format of MIB_IPFORWARDROW structure? Pin
Guffa22-Sep-06 9:47
Guffa22-Sep-06 9:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.