tags:

views:

51

answers:

1

Hi

I am trying to insert an array into SQL with no luck. I get the string from a GPRS device that looks like this:

/WeightBridge.aspx?ReadeID=A1B5A0F5C4E4A1B5A0F5C4E4&TagID=45B6C56A90B645B6C56A90B6,A47B1256A45F0843,B49B1256A45F08FF,30 SEP 2010 21:33:59,I,&Custom=Vehicle Num

All I want to do is to split the TagID array and insert it with the rest of the string into a SQL table. The TagID array must inserted into the following colomns in the DB. TagID, TID, UserMemory, DateTime and Direction. After the insert I just give a response that the insert was successfull or failed. Thank you

My code this far:

Imports System.Data.Sql
Imports System.Data.SqlClient

Partial Class WeightBridge
    Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    insertValue()


End Sub
Private Sub insertValue()
    Dim sqlConn As New SqlConnection
    Dim strConnection As String
    Dim MyTagID As String
    Dim MyReaderID As String
    Dim MyCustom As String
    Dim MyTagArray As Array
    Dim i As Integer

    'Request TagID Array
    MyTagID = Request("TagID")
    If MyTagID.Length > 0 Then
        'Response.Write(MyTagID)
        'Split TagID Array 
        MyTagArray = Split(MyTagID, ",")
        For i = 0 To UBound(MyTagArray) - 1
        Next
    End If

    Try
    strConnection = "My Connection String"

    sqlConn = New SqlConnection(strConnection)
    Dim InsertCommand As New SqlCommand("INSERT INTO WeightBridge(ReaderID, TagID, TID, UserMemory, DateTime, Direction, Custom) VALUES ( '" & Request("ReaderID") & "', '0','0','0','0','0',  '" & Request("Custom") & "')", sqlConn)
    sqlConn.Open()

    InsertCommand.ExecuteNonQuery()

    sqlConn.Close()

    Catch ex As Exception
        Response.Write("FailedNo")
    End Try

    Response.Write("Success")

End Sub

End Class
A: 

There is a comma at the end of your TagID QueryString.

Besides, have a look at following code:

Dim allCols() As String = Request("TagID").Split(","c)
Dim tagID As String = allCols(0)
Dim tID As String = allCols(1)
Dim usermemory As String = allCols(2)
Dim dateTime As String = allCols(3)
Dim direction As String = allCols(4)
'........

You should read this article because you are widely open for sql-injection attacks.

Tim Schmelter
Thank you I will try the code and read the article and give you feedback
SilverMecer
Thank you it worked fine and I optimised my code as well
SilverMecer
Glad i could help. Remember to mark as answered.
Tim Schmelter
Hi Tim wil you please point me in right direction. I have the same code as above. The only problem is that the data will now be posted as a Pipe delimeter, I need to spilt the first array from the pipe then the second array I spilt from the commma and I need to loop trought the records and insert each pipe delimited record in iets own row and with that insert the commadelimited records in their respected colomns. I tried to do a loop but got a infinit loop that inserted 5000 records per second in the db. Will you please help me
SilverMecer
Will you update your original question with your new code and the problem you described here?!
Tim Schmelter
Hi Tim, I decided to make a new question with the new code as well. Thank you for help much appreciated.
SilverMecer
http://stackoverflow.com/questions/3932370/split-multiply-arrays-loop-trough-records-and-insert-records-in-sql-server-2008
SilverMecer
Ok, but when you update your original question, it will be "bumped" too and will not be closed ;)http://meta.stackoverflow.com/questions/7046/how-to-get-attention-for-your-old-unanswered-questions
Tim Schmelter
But because you have already marked my answer, you probably wont get much more views now. Later i have more time to have a look.
Tim Schmelter