SQL update via vb .net

K

Kenneth H. Young

When I update an SQL record with the IP address received by my tcp listener
the SQL record has a sting of square symbols after the IP address. I
believe the squares represent the remaining blank bytes in the Buffer. How
can I trim these squares from my string? I have tried clientV = clientV.Trim
() and clientV = clientV.Trim(Nothing) to no avail.

I receive the IP address as such:
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)

SyncLock CurThread

clientV = (System.Text.Encoding.ASCII.GetString(Buffer))

I Then up date SQL:

Dim updateCMD As String = "UPDATE ManagedLists SET mlName = @clValue WHERE
mlID = 849;"

Dim sqlUpdate As SqlCommand = New SqlCommand(updateCMD, Me.SqlConnection1)

sqlUpdate.Parameters.Add(New SqlParameter("@clValue", SqlDbType.VarChar))

sqlUpdate.Parameters("@clValue").Value = clientV

Me.SqlConnection1.Open()

sqlUpdate.ExecuteNonQuery()

Me.SqlConnection1.Close()



Thanks for the Help!
 
P

Peter Huang [MSFT]

Hi

1. What did you get in the code line below.
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)

2.Normally, if we send 5(e.g.) bytes in sender, the receiver will get 5
bytes too. So if the squares is not what you had send, it should be
something wrong with the TCP/IP app.
Another way is that, if the Buffer is not empty, and the Bytes did not
equal to the Buffer.Length, then in this case, we can just stripe the Bytes
count bytes from the Buffer and do ascii decoding.

3. Please make sure you are doing ascii encoding in the sender

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
K

Kenneth H. Young

I am sending the IP address of the client as follows:

Dim Client As TcpClient
Dim Buffer() As Byte
Dim InBuff(100) As Byte
Dim heserver As IPHostEntry = Dns.Resolve(Temp)
Dim curAdd As IPAddress
For Each curAdd In heserver.AddressList
TempI = curAdd.ToString()
Next
'1. Send IP address to server.
Buffer = System.Text.Encoding.ASCII.GetBytes(TempI.ToString)
Client.GetStream().Write(Buffer, 0, Buffer.Length)


Server Side code:

Dim CurSocket As Socket
Dim Buffer(100) As Byte
Dim Bytes As Integer
While Not StopListener
If CurSocket.Available > 0 Then
' 1. Receive IP Address from client:
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)
SyncLock CurThread
clientV2 = (System.Text.Encoding.ASCII.GetString(Buffer))
End SyncLock
Exit While
End If

I then take take clientV2 and update a SQL record and that is when the
square symbols show up.
NOTE: If I write clientV2's value to the config.xml file I find ""
repeated over and over - the number of bytes in the IP address. I found that
"" is apparently an illegal NULL XML value.


Thanks for all the help!
 
P

Peter Huang [MSFT]

Hi Kenneth,

From your code snippet it seems that the code is OK.
Based on my understanding, the problem should be of no business with the
SQL, because the strange data is gotten before you call the SQL statement.
The problem is that you send A but you get AXXXXXX.
If I misunderstand, please let me know.

So far please check the suggestion I posted in my last post.
1. Debug.WriteLine(TempI.ToString) before sending,
Buffer = System.Text.Encoding.ASCII.GetBytes(TempI.ToString)
Client.GetStream().Write(Buffer, 0, Buffer.Length)
2. Note the Buffer.Length, Buffer
3. In the receive side,
Confirm the Buffer is emtpy,
Bytes = CurSocket.Receive(Buffer, Buffer.Length, 0)
4. note the Bytes, Buffer.Length, and compared it with the data NOTED in 2.
5. compared Buffer with the Buffer in 2.

If you still have any concern, please send me a simple reproduce sample
together with the reproduce steps and the test data you are using.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top