Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
note it is windows appplication.

Save Button Code as follows;

C#
for(int i =0;i<dgv_session.rowcount;i++)>
{
    if (Convert.ToBoolean(DGv_Session.Rows[i].Cells[1].Value) == true)
    {
        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.ToString(DGv_Session[1, i].Value == "true") + ",
" + Convert.ToString(DGv_Session[2, i].Value == "true") + ",
" + Convert.ToString(DGv_Session[3, i].Value == "true") + ",
" + Convert.ToString(DGv_Session[4, i].Value == "true") + ")";
        
        GFun.Error = "";
        GFun.InsertAccessData(sql);
        if (GFun.Error.ToString() != "")
        {
            MessageBox.Show(GFun.Error.ToString(), "Error");
            return;
        }
        GFun.OleDbCon.Close();
        MessageBox.Show("Record Inserted Successfully");
    }
}



Run Mode Screen As follows;

Course Code (combobox)

Days sess1 sess2 sess3 sess4
1 checked checked unchecked unchecked

in the above for the course day 1 sess1 and sess2 is checked and save in the database.


But In database records as follows;

Days sess1 sess2 sess3 sess4
1 unchecked unchecked unchecked unchecked

why the sess1 and sess2 checked checkbox are not checked in the database.

what is the problem in my above checkbox checked true code?

Please help me.

regards,
Narasiman P.


note it is windows appplication.
Posted
Updated 12-Mar-13 0:22am
v2

Hi,

may the value you are comparing with "true" is always giving false. so, try like below.
C#
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() + ")";


hope it works.
 
Share this answer
 
Comments
Jegan Thiyagesan 12-Mar-13 10:07am    
Hi, how is your code any different from the original code written by the author of the question?

Why converting an object into Boolean, checking if it is "true" then converting to string?

What would happen if the values are false? Wouldn't the query become just series of commas?

What if the value is null, will the compiler convert a null to Boolean? Will that return "true"?
What would happen when with "null.ToString()"?
Hi,
The DataGridView Value property will return the object that is contains so if have assigned a Boolean type object to a cell, it will return that Boolean object when asked, you wouldn't need to convert it back to Boolean again.

I assume that you not used proper typed objects for the data binding i.e. where a "True/False" property typed as bool not string. However, if you have used correct types it is good. If not, it is more likely in your case, you would be using everything as string; then you must also check that the string object you are parsing around is not null.

So, in this case if the value is not null parse the string of that object, if it is null then set as empty:
C#
for(int i =0;i<dgv_session.rowcount;i++)>
{
    if (DGv_Session.Rows[i].Cells[1].Value != null)
    {
        if (Convert.ToBoolean(DGv_Session.Rows[i].Cells[1].Value))
        {
            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 != null) ? DGv_Session.Rows[i].Cells[0].Value.ToString() : "" + "',"
                + (DGv_Session[1, i].Value != null) ? DGv_Session[1, i].Value.ToString() : "" + ","
                + (DGv_Session[2, i].Value != null) ? DGv_Session[2, i].Value.ToString() : ""  + ","
                + (DGv_Session[3, i].Value != null) ? DGv_Session[3, i].Value.ToString() : ""  + ","
                + (DGv_Session[4, i].Value != null) ? DGv_Session[4, i].Value.ToString() : "" + ")";

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


I hope this helps.

Regards
Jegan
 
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