Form to SQL Helper Class?
Hi does anyone have a helper class for converting Request.Form values to a suitable SQL value or system value? I'm posting a a function in a seperate thread I find useful in the interest of code sharing.
Right now I have a simple information class and I am using reflection to get all it's properties. Then I am looping thru the form keys, and setting any matches. I am getting an error because drop downs are listed in the form keys array even if no value was selected. So I want some kind of function that accepts (object, system.type) and makes sure the object value in an acceptable value for the given type. i.e. "" goes to "0" for an integer, "on" goes to "true" for a checkbox boolean.
VB Code:
Public Sub LoadInfoFromRequest(ByRef myInfo As MyInfoBase)
Dim f As System.Reflection.FieldInfo
Dim x As Integer
Dim o As Object
Dim n, t As String
Dim FormKeys() As String
'Use reflection to get the fields
Dim myFieldInfo() As System.Reflection.FieldInfo
myFieldInfo = myInfo.GetType().GetFields()
'Load the object from the datarow.
FormKeys = HttpContext.Current.Request.Form.AllKeys
For Each f In myFieldInfo
x = FormKeys.IndexOf(FormKeys, f.Name)
If x >= 0 Then
n = f.Name
t = f.GetType().ToString
o = HttpContext.Current.Request.Form(f.Name)
'I need some function here like ConvertObjectValue( object, system.type)
f.SetValue(myInfo, o)
End If
Next
End Sub
Thanks,
Jamie
Re: Form to SQL Helper Class?
I'm not sure I understand what you mean. Why are you handling the request like this?