3 Replies - 1616 Views - Last Post: 16 March 2012 - 03:23 PM Rate Topic: -----

#1 Amenhoteph  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Error: connection property has not been initialized

Posted 15 March 2012 - 05:24 PM

The utility I am writing cycles through a database table that contains full path and filename information and checks to see if the files referenced in the database actually appear on the hard drive via File.Exists. I'm not sure what I'm doing wrong here, but when the code gets to the point where this line is executed (line 26):

Int32 num = Convert.ToInt32(new OleDbCommand(cmdCount).ExecuteScalar());

I get an error that says "connection property has not been initialized". If I comment out that section of code and continue on I get the same error here (line 32):

reader = new OleDbCommand(cmdText).ExecuteReader();

I'm sure that I'm missing something simple here, I just can't see it. If one of you could point it out I would really appreciate it.

        private void cmdVerify_Click(object sender, EventArgs e)
        {
            if (!(File.Exists(this.txtMDBPath.Text)))
            {
                MessageBox.Show("No Database Selected.");
                cmdOpenMDB.Focus();
                return;
            }

            OleDbDataReader reader = null;
            OleDbConnection connection = null;

            bool bAllFound = true;

            this.progressBar1.Visible = true;
            this.progressBar1.Minimum = 1;
            this.progressBar1.Value = 1;
            this.progressBar1.Step = 1;

            connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName);
            connection.Open();

            string cmdText = "SELECT FilePath, FileName FROM FileTable";
            string cmdCount = "SELECT COUNT(*) FROM FileTable";

            Int32 num = Convert.ToInt32(new OleDbCommand(cmdCount).ExecuteScalar());
            iFullCount = num;
            connection.Close();
            this.progressBar1.Maximum = num;

            connection.Open();
            reader = new OleDbCommand(cmdText).ExecuteReader();
            Cursor.Current = Cursors.WaitCursor;
            
            try 
            {
                while(reader.Read())
                {
                    string strRawPath = reader["FilePath"].ToString();
                    string strDriveLetter = strRawPath.Substring(1, 1);
                    int iLength = strRawPath.Length;
                    string strTrimmedPath = strRawPath.Substring(4, iLength - 4);
                    this.lblStatus.Text = strDriveLetter + @":\" + strTrimmedPath + @"\" + reader["FileName"].ToString();

                    if (File.Exists(this.lblStatus.Text))
                    {
                        
                        Application.DoEvents();
                        this.progressBar1.PerformStep();
                        
                        if (this.bStop)
                        {
                            connection.Close();
                            Application.Exit();
                        }
                        
                    }
                    else
                    {
                        {
                            this.txtReport.AppendText("Not Found: " + reader["FilePath"].ToString() + @"\" + reader["FileName"].ToString() + Environment.NewLine);
                            Application.DoEvents();
                            //this.progressBar1.PerformStep();
                            
                            if (bAllFound)
                            {
                                bAllFound = false;
                            }

                            if (this.bStop)
                            {
                                connection.Close();
                                Application.Exit();
                            }
                        }
                    }
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            finally
            {
                if(reader !=null)
                {
                    reader.Close();
                }
                
                if(connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }

                if (bAllFound)
                {
                    txtReport.Text = "All " + iFullCount.ToString() + "files located.";
                    cmdExit.Focus();
                }
                else
                {
                    cmdSaveReport.Enabled = true;
                    cmdSaveReport.Focus();
                }
             }
             cmdSaveReport.Enabled = true;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Error: connection property has not been initialized

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Error: connection property has not been initialized

Posted 16 March 2012 - 02:32 AM

Quote

connection property has not been initialized

This error would suggest to me that one of the properties you require to connect to a database is null or hasn't been assigned a value (uninitialized).

You're gonna have to set breakpoints and hover over all the properties to see what they're referencing (if anything)

This post has been edited by negligible: 16 March 2012 - 02:34 AM

Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5667
  • View blog
  • Posts: 22,509
  • Joined: 23-August 08

Re: Error: connection property has not been initialized

Posted 16 March 2012 - 04:09 AM

As negligible said, the error's right there in English. You need to learn to read and interpret your errors.

So, let's see, you

connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName);
    connection.Open();

create and open a connection to the DB

Int32 num = Convert.ToInt32(new OleDbCommand(cmdCount).ExecuteScalar());

create a command object and (attempt) to run a command.

Do you see what the missing link is here?
Was This Post Helpful? 0
  • +
  • -

#4 Amenhoteph  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 15-March 12

Re: Error: connection property has not been initialized

Posted 16 March 2012 - 03:23 PM

View Postnegligible, on 16 March 2012 - 02:32 AM, said:

Quote

connection property has not been initialized

This error would suggest to me that one of the properties you require to connect to a database is null or hasn't been assigned a value (uninitialized).


Thanks Negligible. I have been going cross-eye'ed these past few days with several other projects and I just could not get this to click. I've been coding in C# for all of a week and there is a lot to absorb.

Re-written, and functioning, code below just in case someone else has a similar issue in the future. This website was also very helpful:

http://www.akadia.co...ata_reader.html


private void cmdVerify_Click(object sender, EventArgs e)
        {
            if (!(File.Exists(this.txtMDBPath.Text)))
            {
                MessageBox.Show("No Database Selected.");
                cmdOpenMDB.Focus();
                return;
            }

            OleDbDataReader reader = null;
            OleDbConnection connection = null;
            OleDbCommand cmd = null;

            bool bAllFound = true;

            this.progressBar1.Visible = true;
            this.progressBar1.Minimum = 1;
            this.progressBar1.Value = 1;
            this.progressBar1.Step = 1;

            try
            {

                string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + openFileDialog1.FileName;
                connection = new OleDbConnection(connectionString);
                connection.Open();
                                
                //Begin Determine Number of Records in Table
                
                string sqlCount = "SELECT COUNT(*) FROM Table";

                cmd = new OleDbCommand(sqlCount);
                cmd.Connection = connection;

                int num = (Int32)cmd.ExecuteScalar();
                iFullCount = num;                                   //Needed for "All Found" report
                this.progressBar1.Maximum = num;                    //Needed for accurate processing of progres bar

                //End Determone Number of Records in Table

                //Begin Check Each Record in Table

                string sqlGetFullFilePath = "SELECT FilePath, FileName FROM Table";

                cmd = new OleDbCommand(sqlGetFullFilePath);
                cmd.Connection = connection;

                reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    string strRawPath = reader["FilePath"].ToString();
                    string strDriveLetter = strRawPath.Substring(1, 1);
                    int iLength = strRawPath.Length;
                    string strTrimmedPath = strRawPath.Substring(4, iLength - 4);
                    this.lblStatus.Text = strDriveLetter + @":\" + strTrimmedPath + @"\" + reader["FileName"].ToString();

                    if (File.Exists(this.lblStatus.Text))
                    {

                        Application.DoEvents();
                        this.progressBar1.PerformStep();

                        if (this.bStop)
                        {
                            connection.Close();
                            Application.Exit();
                        }

                    }
                    else
                    {
                        {
                            this.txtReport.AppendText("Not Found: " + reader["FilePath"].ToString() + @"\" + reader["FileName"].ToString() + Environment.NewLine);
                            Application.DoEvents();
                            this.progressBar1.PerformStep();

                            if (bAllFound)
                            {
                                bAllFound = false;
                            }

                            if (this.bStop)
                            {
                                connection.Close();
                                Application.Exit();
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                    MessageBox.Show(exception.Message);
            }
            finally
            {
                connection.Close();
            }

            //End Check Each Record in Table

            if (bAllFound)
            {
                this.txtReport.Text = "All " + iFullCount.ToString() + "files located.";
            }
            else
            {
                this.txtReport.AppendText("Process Complete.");
            }
            cmdSaveReport.Enabled = true;
        }

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1