Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Design as follows;

Course Code Combobox

Datagridview

In Datagridview as follows;
Days   1          2           3        4

1     check box check box check box check box
2     check box check box check box check box
3     check box check box check box check box
4     check box check box check box check box


In the rum mode, I select the course and in the datagridiview select the checkbox and save.
Then I again choose another course and in the datagridveiw select the checkbox and save.

My save code as follows;

C#
for (int i = 0; i < DGv_Session.RowCount; i++)
            {
                try
                {
                    if (DGv_Session.Rows[i].Cells[1].Value != null && DGv_Session.Rows[i].Cells[2].Value != null && DGv_Session.Rows[i].Cells[3].Value != null && DGv_Session.Rows[i].Cells[4].Value != null)
                    {
                   sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";
sql = sql + " values('" + cb_Course_Code.Text + "', 
'" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
" + (Convert.ToBoolean(DGv_Session[1, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[2, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[3, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[4, i].Value) == true).ToString() + ")";
                                        
                                        GFun.Error = "";
                                        GFun.InsertAccessData(sql);
                                        if (GFun.Error.ToString() != "")
                                        {
                                            MessageBox.Show(GFun.Error.ToString(), "Error");
                                            return;
                                        }
                                          GFun.OleDbCon.Close();

    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error");
                    return;
                }
            }
                     MessageBox.Show("Record Inserted Successfully","Record Inserted",MessageBoxButtons.OK,MessageBoxIcon.Information);

When I save in the database, suppose I save the two course in the database.
Only one course save in the database.
The second course not saved in the database.

What is the problem in my above code?

Please help me.

Regards,
Narasiman P.

Note it is windows applciation.
Posted
v2
Comments
frostcox 15-Mar-13 4:48am    
Hey I don't know much about Access but it looks to me that you are closing your connection to the database after the first insert in the loop.

1 solution

C#
for (int i = 0; i < DGv_Session.RowCount; i++)
            {
                try
                {
                    if (DGv_Session.Rows[i].Cells[1].Value != null && DGv_Session.Rows[i].Cells[2].Value != null && DGv_Session.Rows[i].Cells[3].Value != null && DGv_Session.Rows[i].Cells[4].Value != null)
                    {
                   sql = "insert into Tb_Session_Structure ([Cmn_Minor_code],[Days],[Session1],[Session2],[Session3],[Session4])";
sql = sql + " values('" + cb_Course_Code.Text + "',
'" + DGv_Session.Rows[i].Cells[0].Value.ToString() + "',
" + (Convert.ToBoolean(DGv_Session[1, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[2, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[3, i].Value) == true).ToString() + ",
" + (Convert.ToBoolean(DGv_Session[4, i].Value) == true).ToString() + ")";

                                        GFun.Error = "";
                                        GFun.InsertAccessData(sql);
                                        if (GFun.Error.ToString() != "")
                                        {
                                            MessageBox.Show(GFun.Error.ToString(), "Error");
                                            return;
                                        }
                                         // GFun.OleDbCon.Close();

    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error");
                    return;
                }
                finally{GFun.OleDbCon.Close();}
            }
                     MessageBox.Show("Record Inserted Successfully","Record Inserted",MessageBoxButtons.OK,MessageBoxIcon.Information);
 
Share this answer
 

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