How do I add a record in my database when it is coming from a checklist box control from?
I'm having trouble on how to code this. First I need to assign a value for every item in the list (1
for the first item, 2
for the 2nd and so on). Does this control have a separate field for the value aside from text?
Second, I need to insert this into my database (MSSQL 2008) in this format:
insert into table values(ID,
selectedvaluegoeshere
)
开发者_Go百科Can someone guide me on how to achieve this.
The ideal way to do this would be to use Enumerated Values to fill your Checked Combo Box, this allows you to guarantee the association of the items in the list with their values. To convert the Enum to a human friendly format you can do something like this.
Imports System.ComponentModel
Imports System.Reflection
Public Enum ePregnancyCode
<Description("Not Specified")> _
NotSpecified
<Description("Not Pregnant")> _
NotPregnant
<Description("Pregnant")> _
Pregnant
End Enum
And then retrieve the Description
field using this code
Public Function GetDescription(ByVal EnumConstant As [Enum]) As String
Dim fi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
Dim aattr() As DescriptionAttribute = _
DirectCast( _
fi.GetCustomAttributes(GetType(DescriptionAttribute), False), _
DescriptionAttribute() _
)
Return If(aattr.Length > 0, aattr(0).Description, EnumConstant.ToString())
End Function
You would probably be better off using a lookup edit for your Combo Selection now that I think about it. Then you can just retrieve the selected item collection from the Control when you go to insert into the database.
精彩评论