When I click Edit Button the CheckBox gets the value from database and gets ticked.
But when I click Update Button, Value ' True ' gets updated to database. But when i try to update the same data the following error message appears near CheckBox:
When casting from a number, the value must be number less than infinity.
Make sure the source type is convertible to the destination type.
I want to update 1 or 2 as value for the field ACTIVE. ie the CheckBox value should be 1 or 2.
Basically, a bit value is either 0 (True) or 1 (False). It would be ideal if the datatype for the field that stores the CheckBox values in the database to be of type bit. A condition for a checkbox is true if it is checked, and false if it is not. So, it
only makes sense to store this type of a result in a database as such.
In the code, we use the byte datatype to evaluate the CheckBox and return the appropirate value to that is compatible with the SQL bit datatype
VB.NET:
Dim result As byte
result = Convert.ToByte(chkID.Checked)
C#:
byte result;
result = Convert.ToByte(chkID.Checked);
You can then use the result from the byte conversions in your code to store a true/false value in your database.
Bindu
Member
425 Points
83 Posts
How to Update CheckBox checked value to sql server database?
Dec 15, 2005 04:55 PM|LINK
My Code is as follows:
<
asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" Height="50px" Style="position: static" Width="125px"> <Fields> <asp:TemplateField HeaderText="Active" SortExpression="Active"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Active") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("Active") %>' Style="position: static" /> </EditItemTemplate> <InsertItemTemplate> <asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# Bind("Active") %>' Style="position: static" /> </InsertItemTemplate> </asp:TemplateField> <asp:CommandField ShowInsertButton="True" /> <asp:CommandField ShowEditButton="True" /> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NetConnectionString %>" SelectCommand="SELECT [Id], [Active] FROM [HN_RoomMaster]" InsertCommand="INSERT INTO [HN_RoomMaster] ([Active]) VALUES (@Active)" UpdateCommand="UPDATE [HN_RoomMaster] SET [Active] = @Active WHERE [Id] = @original_Id"> <DeleteParameters> <asp:Parameter Name="original_Id" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="Active" Type="String" /> <asp:Parameter Name="original_Id" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="Active" Type="String" /> </InsertParameters> </asp:SqlDataSource>When I click Edit Button the CheckBox gets the value from database and gets ticked. But when I click Update Button, Value ' True ' gets updated to database. But when i try to update the same data the following error message appears near CheckBox:
When casting from a number, the value must be number less than infinity.
Make sure the source type is convertible to the destination type.
I want to update 1 or 2 as value for the field ACTIVE. ie the CheckBox value should be 1 or 2.
Darmark
Contributor
3843 Points
709 Posts
Re: How to Update CheckBox checked value to sql server database?
Dec 15, 2005 05:17 PM|LINK
It should be zero(0) and one (1).
0 = false
1 = true
Mark as Answer, if this reply answers your post.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How to Update CheckBox checked value to sql server database?
Dec 15, 2005 05:52 PM|LINK
I would store the value in the database as type bit and then use the byte value type in your code to pass if the CheckBox is checked.
i.e.
byte Checked = Convert.ToByte(chk.Checked)
Bindu
Member
425 Points
83 Posts
Re: How to Update CheckBox checked value to sql server database?
Dec 16, 2005 06:20 AM|LINK
SinkableHail
Participant
1550 Points
306 Posts
Re: How to Update CheckBox checked value to sql server database?
Dec 16, 2005 03:01 PM|LINK
Try this:
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("Active") %>' Style="position: static" />
I am guessing that active is a boolean field. If so when Active = 0 the check box will be unchecked and when Active = 1 the check box will be checked.
StrongTypes
All-Star
30801 Points
6013 Posts
ASPInsiders
Re: How to Update CheckBox checked value to sql server database?
Dec 16, 2005 03:11 PM|LINK
Basically, a bit value is either 0 (True) or 1 (False). It would be ideal if the datatype for the field that stores the CheckBox values in the database to be of type bit. A condition for a checkbox is true if it is checked, and false if it is not. So, it only makes sense to store this type of a result in a database as such.
In the code, we use the byte datatype to evaluate the CheckBox and return the appropirate value to that is compatible with the SQL bit datatype
VB.NET:
Dim result As byte
result = Convert.ToByte(chkID.Checked)
C#:
byte result;
result = Convert.ToByte(chkID.Checked);
You can then use the result from the byte conversions in your code to store a true/false value in your database.
spganesh
Member
2 Points
1 Post
Re: How to Update CheckBox checked value to sql server database?
Feb 11, 2010 08:44 AM|LINK
thanks, similar to how to get the result of same yes or no for the same condition