I have created this vb.net file and it is working when i send a string like this:
?data={"id":"12345","timestamp":"2012-03-03 12:00:00","latitude":"23.41223","longitude"="54.12345"}
but i also want it to work with a format like this:
?id=12345×tamp=2012-03-03 12:00:00&latitude=23.41223&longitude=54.12345
How do i make this work in my vb.net file
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("data")) Then
Dim data As String = Request.QueryString("data")
Dim myObj As New MyObject
Dim properties() As PropertyInfo = myObj.GetType().GetProperties()
Dim values() As String = Server.UrlDecode(data).Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace(""":""", """=""").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {"="}, StringSplitOptions.RemoveEmptyEntries)
For Each prop As PropertyInfo In properties
If prop.Name.ToLower = keyValue(0).ToLower.Replace("""", "") Then
prop.SetValue(myObj, keyValue(1).Replace("""", ""), Nothing)
End If
Next
Next
lblText1.Text = String.Format("ID: {0}", myObj.ID)
lblText2.Text = String.Format("Longitude: {0}", myObj.Longitude)
lblText3.Text = String.Format("Latitude: {0}", myObj.Latitude)
lblText4.Text = String.Format("Timestamp: {0}", myObj.Timestamp)
Cmd.Parameters.Clear()
Cmd.Parameters.AddWithValue("@ID", myObj.ID)
Cmd.Parameters.AddWithValue("@Longitude", myObj.Longitude)
Cmd.Parameters.AddWithValue("@Latitude", myObj.Latitude)
Cmd.Parameters.AddWithValue("@Timestamp", myObj.Timestamp)
Con.ConnectionString = "Data Source=servert\sql;Initial Catalog=table;Integrated Security=True"
Cmd.Connection = Con
Con.Open()
Cmd.CommandText = "IF EXISTS (SELECT 1 FROM Locatie WHERE id = @ID) " & Environment.NewLine & _
" BEGIN UPDATE Locatie SET Longitude = @Longitude, Latitude = @Latitude, Timestamp = @Timestamp WHERE id=@ID END " & Environment.NewLine & _
"ELSE " & Environment.NewLine & _
" BEGIN INSERT INTO Locatie VALUES (@ID, @Longitude, @Latitude, @Timestamp) END "
Reader = Cmd.ExecuteReader
Reader.Close()
Con.Close()
Con.Dispose()
End If
End Sub
Public Class MyObject
Private _ID As String
Private _Longitude As String
Private _Latitude As String
Private _Timestamp As String
Public Property ID As String
Get
Return _ID
End Get
Set(value As String)
_ID = value
End Set
End Property
Public Property Longitude As String
Get
Return _Longitude
End Get
Set(value As String)
_Longitude = value
End Set
End Property
Public Property Latitude As String
Get
Return _Latitude
End Get
Set(value As String)
_Latitude = value
End Set
End Property
Public Property Timestamp As String
Get
Return _Timestamp
End Get
Set(value As String)
_Timestamp = value
End Set
End Property
End Class
</script>
Where probably the key must be around here, so that there is also a second option where the second string is split set the values as id, longitude, latitude and timestamp:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(Request.QueryString("data")) Then
Dim data As String = Request.QueryString("data")
Dim myObj As New MyObject
Dim properties() As PropertyInfo = myObj.GetType().GetProperties()
Dim values() As String = Server.UrlDecode(data).Replace("[", "").Replace("]", "").Replace("{", "").Replace("}", "").Replace(""":""", """=""").Split(New Char() {","}, StringSplitOptions.RemoveEmptyEntries)
For Each value As String In values
Dim keyValue() As String = value.Split(New Char() {"="}, StringSplitOptions.RemoveEmptyEntries)
For Each prop As PropertyInfo In properties
If prop.Name.ToLower = keyValue(0).ToLower.Replace("""", "") Then
prop.SetValue(myObj, keyValue(1).Replace("""", ""), Nothing)
End If
Next
Next
Do a check on the query string. If the string starts with data, then allocate values to the variables using the first method, else allocate values to variables by getting the value of the query string.
Example:
Edit
Example of the implementation within the provided Page_Load subroutine.