SQL Boolean property and ASP.NET
I开发者_JAVA技巧 have a column in my SQL table, that I wanted to make a boolean property, but there wasn't one so I made it an int and used 1 for yes and 0 for no. Is there a way that I can add a dropdown list to a listview in visual studio containing the values Yes and No?
Essentially so I can Bind the column to the dropdown, rather than when updating the record the user can select Yes or No rather than enter 1 or 0?
Essentially so I can Bind the column to the dropdown, rather than when updating the record the user can select Yes or No rather than enter 1 or 0?
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="Yes" Value="1" />
<asp:ListItem Text="No" Value="0" />
</asp:DropDownList>
First, there is a column type called "bit" which is exactly for this purpose.
Second, I would bind to a list of user friendly names for the drop down. Or, you could use a checkbox? All you need to do is translate that to a bool and pass it to sql.
You could create an enumeration:
enum BooleanValue
{
No = 0,
Yes = 1
}
And then to use it:
BooleanValue myBool = (BooleanValue)myIntegerValue;
myBool.ToString(); // returns "No"
For reference, the bit
SQL datatype corresponds to bool
in .Net.
Make the Text of the Item List as Yes & the Item Value 1 same goes for the second text of the Item List as No & the Item Value 0
then use the SeletedValue Property
精彩评论