delete multiple rows in datagridview and access table

Go To StackoverFlow.com

0

I have a table Movies in access, connected with OleDBConnection, and Datagridview1 component. Have such code:

if (this.dataGridView1.SelectedRows.Count > 0)
        {
            string queryString = "SELECT movieID, Title, MovieYear, Country,Located, Description, Poster, Actors, FilmDirector, Type FROM Movie,movieType WHERE movietype.typeID = Movie.typeID";
            foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
            {
                 int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
                try
                {
                    string movieIDString = dataGridView1[0, currentRow].Value.ToString();
                    movieIDInt = int.Parse(movieIDString);                

                    string queryDeleteString = "DELETE FROM Movie where movieID = " + movieIDInt + ";";
                    OleDbCommand sqlDelete = new OleDbCommand();
                    sqlDelete.CommandText = queryDeleteString;
                    sqlDelete.Connection = database;
                    sqlDelete.ExecuteNonQuery();
                    loadDataGrid(queryString);
                 }
                catch (Exception ex) { }


            }

        }

It must delete selected multiple rows, but it don't :(

Where is my mistake, can you help me, please?

2012-04-05 19:21
by RIO
In this case use a debugger and debug your code. Check out if it is calling the correct methods. My guess is loadDataGrid should be after the foreach statement - JonH 2012-04-05 19:39


1

I think, loadDataGrid(queryString) should be outside of the for loop. it's just a guess not sure though.

if (this.dataGridView1.SelectedRows.Count > 0)
    {
        string queryString = "SELECT movieID, Title, MovieYear, Country,Located, Description, Poster, Actors, FilmDirector, Type FROM Movie,movieType WHERE movietype.typeID = Movie.typeID";
        foreach (DataGridViewRow dgvrCurrent in dataGridView1.SelectedRows)
        {
             int currentRow = int.Parse(dataGridView1.CurrentCell.RowIndex.ToString());
            try
            {
                string movieIDString = dataGridView1[0, currentRow].Value.ToString();
                movieIDInt = int.Parse(movieIDString);                

                string queryDeleteString = "DELETE FROM Movie where movieID = " + movieIDInt + ";";
                OleDbCommand sqlDelete = new OleDbCommand();
                sqlDelete.CommandText = queryDeleteString;
                sqlDelete.Connection = database;
                sqlDelete.ExecuteNonQuery();                    
             }
            catch (Exception ex) { }
        }

        loadDataGrid(queryString);
    }
2012-04-05 19:35
by sarwar026
Ads