1

when i check on checkbox whose datatype is bit ,its value i.e 1 or 0 is not being store in database it is giving error

Conversion failed when converting the varchar value 'System.Web.UI.WebControls.CheckBox' to data type bit

if (CheckBoxDT.Checked) 
{ 
    cmd.CommandText = " Insert into ClientMaster(doTips) values ( 1 )";    
    cmd.Parameters.AddWithValue("doTips", bool.Parse("1")); 
    cmd.ExecuteNonQuery(); 
} 
else 
{ 
    cmd.CommandText = "insert into ClientMaster(doTips) values (0)";
    cmd.Parameters.AddWithValue("doTips", bool.Parse("0")); 
    cmd.ExecuteNonQuery(); 
} 
7
  • 3
    show us some code pls! Commented Feb 18, 2013 at 8:22
  • and your table structure(screenshot) Commented Feb 18, 2013 at 8:22
  • i have added this code to convert checkbox into bit but it is not working giving same error.............. <asp:CheckBox ID="CheckBoxDL" runat="server" Checked='<%#(Convert.ToBoolean(Eval("isActive"))) %>' style="color: #FFFFFF" /> Commented Feb 18, 2013 at 8:25
  • What's your database column name? Commented Feb 18, 2013 at 8:29
  • What is "isActive"? Is that a column name? If yes, then are you trying to pass a datatable row value to the "Checked" attribute of your checkbox? Commented Feb 18, 2013 at 8:41

3 Answers 3

3

Inserting Bit value to the database

  cmd.Parameters.Add("@check", SqlDbType.Bit).Value = Convert.ToInt16(CheckBoxDT.Checked);

retrieving selected check boxes Try this

<asp:CheckBox ID="CheckBoxDT" runat="server" Checked='<%#((bool)Eval("isActive"))%>'
                        Style="color: #FFFFFF" />

enter image description here

Your code

if (CheckBoxDT.Checked) 
{ 
cmd.CommandText = " Insert into ClientMaster(doTips) values ( 1 )";    
cmd.Parameters.AddWithValue("doTips", bool.Parse("1")); 
cmd.ExecuteNonQuery(); 
} 
else 
{ 
cmd.CommandText = "insert into ClientMaster(doTips) values (0)"; 
cmd.Parameters.AddWithValue("doTips", bool.Parse("0")); 
cmd.ExecuteNonQuery(); 
}

Replace this with

cmd.CommandText = "insert into ClientMaster(doTips) values (@doTips)"; 
cmd.Parameters.Add("@doTips", SqlDbType.Bit).Value = Convert.ToInt16(CheckBoxDT.Checked)
cmd.ExecuteNonQuery(); 
Sign up to request clarification or add additional context in comments.

4 Comments

when i write this code in .cs file it wont work either ` if (CheckBoxDT.Checked) { cmd.CommandText = " Insert into ClientMaster(doTips) values ( 1 )"; cmd.Parameters.AddWithValue("doTips", bool.Parse("1")); cmd.ExecuteNonQuery(); } else { cmd.CommandText = "insert into ClientMaster(doTips) values (0)"; cmd.Parameters.AddWithValue("doTips", bool.Parse("0")); cmd.ExecuteNonQuery(); } `
Why you are parsing to bool value when your data type is bit
well it is not giving error but in database checkbox field have this value instead of 1 or 0 , System.Web.UI.WebControls.CheckBox
Can you post the code you wrote with my code that didn't happen as I posted the working image too
1

Chances are you're passing the control, rather than the value of its state; you should be passing the state, and converting that explicitly if necessary:

CheckBox         // a web control, and no standard db type to represent it
CheckBox.Checked // a boolean value, generally translatable to db bit

Comments

0

You are most probably passing the actual control to your database and not the state of it. You need to use myCheckBox.Checked instead of just myCheckBox.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.