Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
(Check out the pics to understand by clicking the link)

I have made the following WinForm in which the user provide book details and create a book record. This book record gets added to the dataGridView with the checkBox automatically selected:

http://i.stack.imgur.com/VOOTL.png[^]

http://i.stack.imgur.com/MpYNM.png[^]

Now, when I deselect some books and click on 'Insert to database' button to insert the selected books to database, one of the deselected books always gets inserted to database along with the selected ones. And that may be any one of the deselected books.
Like here I deselect some books:

http://i.stack.imgur.com/MM4eM.png[^]

Now when I click the 'Insert to Database' button, the unselected books will be deleted from dataGridView and the selected one will b inserted to database:

http://i.stack.imgur.com/TE1gJ.png[^]

Now the books inserted to database are the selected books '123','345,'678' along with one unselected book '890'. Like this, one unselected book always get inserted to database along with the selected books.
The code which I have written is:
C#
public partial class CreateBookRecord : Form
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
            DataTable addedBooks;
            DataColumn bookId, bookName, author, noOfCopies, noOfCopiesAvailable;
            DataGridViewCheckBoxColumn check;       
            public CreateBookRecord()
            {
                InitializeComponent();                   
            }

            private void CreateBookRecord_Load(object sender, EventArgs e)
            {
                check = new DataGridViewCheckBoxColumn();
                check.TrueValue = true;
                check.FalseValue = false;
                addedBooks = new DataTable();
                bookId = new DataColumn("Book ID", typeof(string));
                bookName = new DataColumn("Book Name", typeof(string));
                author = new DataColumn("Author", typeof(string));
                noOfCopies = new DataColumn("No of Copies", typeof(int));
                noOfCopiesAvailable = new DataColumn("No of Copies Available", typeof(int));
                addedBooks.Columns.AddRange(new DataColumn[] { bookId, bookName, author, noOfCopies,noOfCopiesAvailable });
                dataGridViewAddedBooks.Columns.Add(check);
                dataGridViewAddedBooks.DataSource = addedBooks;
                dataGridViewAddedBooks.Columns["No of Copies Available"].Width = 0;
            }

            private void buttonCreateBook_Click(object sender, EventArgs e)
            {
                    try
                    {
                        addedBooks.Rows.Add(textBoxID.Text, textBoxBookName.Text, textBoxAuthorName.Text,Convert.ToInt32(textBoxNoOfCopies.Text),Convert.ToInt32(textBoxNoOfCopies.Text));                            
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

            private void dataGridViewAddedBooks_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
            {
                DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dataGridViewAddedBooks.Rows[e.RowIndex].Cells[0];
                checkcell.Value = check.TrueValue;
            }

            private void dataGridViewAddedBooks_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == 0)
                {
                    DataGridViewCheckBoxCell checkcell;
                    checkcell = (DataGridViewCheckBoxCell)dataGridViewAddedBooks.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    if (checkcell.Value == check.TrueValue)
                    {
                        dataGridViewAddedBooks.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
                        checkcell.Value = check.FalseValue;
                    }
                    else if (checkcell.Value == check.FalseValue)
                    {
                        dataGridViewAddedBooks.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
                        checkcell.Value = check.TrueValue;
                    }
                }
                else
                {
                    MessageBox.Show("You selected an invalid cell", "Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            private void buttonInsertBooks_Click(object sender, EventArgs e)
            {           
                foreach (DataGridViewRow row in dataGridViewAddedBooks.Rows)
                {
                    DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)row.Cells[0];
                    if (checkcell.Value == check.FalseValue)
                    {
                        dataGridViewAddedBooks.Rows.Remove(row);
                    }
                }
                SqlBulkCopy copy = new SqlBulkCopy(connection);
                copy.DestinationTableName = "Book";
                try
                {
                    connection.Open();
                    copy.WriteToServer(addedBooks);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    connection.Close();
                }        
            }


I would be very thankful if anyone could help me resolve this problem.
Thank you!
Posted

1 solution

Ashish ....You have a Good Programming Skills...I like your GUI for your concern project.

lets come to problem....

The part of coding stuff you gave here is somehow complicated to understand..

But...still I will try to help you...


I think you are Updating a DataGridView after 'Book Selection Process' is completed.

Here is your solution....

Try...to make sure that You are passing ONLY those Records which has Selected CheckBoxes

Use 'Debugging' mode to verify which exact values are passing on 'Insert to Database' event.


Happy Programming.......:-)
 
Share this answer
 
v2
Comments
Ashish159 15-Oct-13 12:14pm    
Thank you @MayurDighe. Yes, that's what I am trying to do. Selecting the records whose CheckBox is selected. But a single unselected record out of all unselected records gets inserted to the database along with the the selected records. And I am going nuts figuring it out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900