Hi, I'm working on making a database that holds basic information on dvds. I've got a strange problem, though it doesn't happen all the time. When I run my code I'm getting the following message in the error list:

A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Sometimes this still lets me search successfully and sometimes it comes up saying no records were found. Any help would be much appreciated. Thanks in advance!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace MediaDB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void moviesBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.moviesBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.dvdDataSet);
        }

        private void search_Click(object sender, EventArgs e)
        {
            string idQuery = "SELECT * FROM movies where ID = \'" + id.Text.ToString() + " \'";
            oleDbDataAdapter1.SelectCommand.CommandText = idQuery;

            string titleQuery = "SELECT * FROM movies where Title LIKE '" + '%' + title.Text.ToString() + '%' + "' ";
            oleDbDataAdapter1.SelectCommand.CommandText = titleQuery;

            string genreQuery = "SELECT * FROM movies where Genre = \'" + genre.Text.ToString() + " \'";
            oleDbDataAdapter1.SelectCommand.CommandText = genreQuery;

            //string releaseDateQuery = "SELECT * FROM movies where Release_Date = \'" + releaseDate.Text.ToString() + " \'";
            //oleDbDataAdapter1.SelectCommand.CommandText = releaseDateQuery;

            //string ratingQuery = "SELECT * FROM movies where rating = \'" + rating.Text.ToString() + " \'";
            //oleDbDataAdapter1.SelectCommand.CommandText = ratingQuery;

            //string runTimeQuery = "SELECT * FROM movies where Run_Time = \'" + runTime.Text.ToString() + " \'";
            //oleDbDataAdapter1.SelectCommand.CommandText = runTimeQuery;

            //string ASINQuery = "SELECT * FROM movies where ASIN = \'" + ASIN.Text.ToString() + " \'";
            //oleDbDataAdapter1.SelectCommand.CommandText = ASINQuery;

            dvdDataSet.Clear();
            dvdDataSet.EnforceConstraints = false;

            int numRows = oleDbDataAdapter1.Fill(dvdDataSet,"movies");

            if (numRows > 0)
            {
                DataTable dt = dvdDataSet.Tables["movies"];
                id.Text = dt.Rows[0][0].ToString();
                title.Text = dt.Rows[0][1].ToString();
                genre.Text = dt.Rows[0][2].ToString();
                releaseDate.Text = dt.Rows[0][3].ToString();
                rating.Text = dt.Rows[0][4].ToString();
                runTime.Text = dt.Rows[0][5].ToString();
                ASIN.Text = dt.Rows[0][6].ToString();
            }
            else 
            {
                MessageBox.Show("Movie not found");
            }
        }
        private void insert_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dvdDataSet.movies' table. You can move, or remove it, as needed.
            this.moviesTableAdapter.Fill(this.dvdDataSet.movies);
        }


    }
}

Why are you replacing the Query string over and over? each time you assign the command text it replaces the previous query, so i would imagine (based on the code you posted) the search will work when you have typed in a genre, but will fail if you are trying to search by Title as you are ending up looking for the given title in teh databses genre field.

I think you might want to use some sort of selection (switch/if..else) to decide which query string to use.

This article has been dead for over six months. Start a new discussion instead.