Maximum length is 128

Maximum length is 128

am 22.06.2006 16:25:38 von fasan21

Im trying to insert binary data into a varbinary field in sql 2005 from
vb.net 2003.


Converting my data to binary I have finally gotten to work but now
inserting into tha database gives me a problem.

my code for binary conversion is as follows

Dim str As String
Dim chr As Char
str = ""
Dim reader As StreamReader
reader = New StreamReader("c:\test.txt")
While Not reader.EndOfStream
chr = ChrW(reader.Read)
str = str + cls.Cal_CharToBinary(chr)
End While
TextBox1.Text = str

with cls being:
Public Class CalculateFunctions
Public Function Cal_CharToBinary(ByVal CharStr As String) As String
Dim LastValue As Integer
Dim BinaryArr() As String
Dim BinaryStr As String
Dim i As Integer
LastValue = Asc(CharStr)
i = 0
ReDim BinaryArr(i)
While LastValue <> 0
ReDim Preserve BinaryArr(i)
BinaryArr(i) = LastValue Mod 2
LastValue = LastValue \ 2
i = i + 1
End While
If UBound(BinaryArr) >= 0 Then
For i = 0 To UBound(BinaryArr)
BinaryStr = BinaryArr(i) & BinaryStr
Next
BinaryStr = String.Format("0", 8 - Len(BinaryStr)) &
BinaryStr
End If
Cal_CharToBinary = BinaryStr
End Function

This all works fine, but when i try insert the converted binary data to
the database I get the following error :
The number that starts with
'01100001011000100110001101100100011001010110011001100111011 010000110100101101010011010110110110001101101011011100110111 101110000'
is too long. Maximum length is 128.

my code for doing this is
Dim myCn As New SqlConnection()
myCn.ConnectionString = "Data Source=sms;Initial
Catalog=Pictures;Persist Security Info=True;User
ID=sa;Password=38tx11local"



Dim cmd As New SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = ("INSERT INTO ImageLibrary (ImageBLOB) VALUES
(" + str + ")")
cmd.Connection = myCn


Dim rowCount As Integer
Dim previousConnectionState As ConnectionState
previousConnectionState = myCn.State
Try
If myCn.State = ConnectionState.Closed Then
myCn.Open()
End If
rowCount = cmd.ExecuteNonQuery()

Finally
If previousConnectionState = ConnectionState.Closed Then
myCn.Close()
End If
End Try


please help if you can........

Fasan

Re: Maximum length is 128

am 22.06.2006 16:36:36 von James Jones

just wondering, does it START with that? or is that full binary number that
is given? can you chance the field length of that field in the database? in
access field lengt can be changed up to 255 chars...........

mayb to get full character count, response.write it out



instead of inserting it into database...........comment out all lines for
database..................and just

response.write str



give that a shot, open word to get a count how many characters it
is..............
if u cant do a count that way, post full binary code, and ill see what i
come up with.................

wrote in message
news:1150986338.235120.298080@i40g2000cwc.googlegroups.com.. .
> Im trying to insert binary data into a varbinary field in sql 2005 from
> vb.net 2003.
>
>
> Converting my data to binary I have finally gotten to work but now
> inserting into tha database gives me a problem.
>
> my code for binary conversion is as follows
>
> Dim str As String
> Dim chr As Char
> str = ""
> Dim reader As StreamReader
> reader = New StreamReader("c:\test.txt")
> While Not reader.EndOfStream
> chr = ChrW(reader.Read)
> str = str + cls.Cal_CharToBinary(chr)
> End While
> TextBox1.Text = str
>
> with cls being:
> Public Class CalculateFunctions
> Public Function Cal_CharToBinary(ByVal CharStr As String) As String
> Dim LastValue As Integer
> Dim BinaryArr() As String
> Dim BinaryStr As String
> Dim i As Integer
> LastValue = Asc(CharStr)
> i = 0
> ReDim BinaryArr(i)
> While LastValue <> 0
> ReDim Preserve BinaryArr(i)
> BinaryArr(i) = LastValue Mod 2
> LastValue = LastValue \ 2
> i = i + 1
> End While
> If UBound(BinaryArr) >= 0 Then
> For i = 0 To UBound(BinaryArr)
> BinaryStr = BinaryArr(i) & BinaryStr
> Next
> BinaryStr = String.Format("0", 8 - Len(BinaryStr)) &
> BinaryStr
> End If
> Cal_CharToBinary = BinaryStr
> End Function
>
> This all works fine, but when i try insert the converted binary data to
> the database I get the following error :
> The number that starts with
> '01100001011000100110001101100100011001010110011001100111011 010000110100101101010011010110110110001101101011011100110111 101110000'
> is too long. Maximum length is 128.
>
> my code for doing this is
> Dim myCn As New SqlConnection()
> myCn.ConnectionString = "Data Source=sms;Initial
> Catalog=Pictures;Persist Security Info=True;User
> ID=sa;Password=38tx11local"
>
>
>
> Dim cmd As New SqlCommand
> cmd.CommandType = CommandType.Text
> cmd.CommandText = ("INSERT INTO ImageLibrary (ImageBLOB) VALUES
> (" + str + ")")
> cmd.Connection = myCn
>
>
> Dim rowCount As Integer
> Dim previousConnectionState As ConnectionState
> previousConnectionState = myCn.State
> Try
> If myCn.State = ConnectionState.Closed Then
> myCn.Open()
> End If
> rowCount = cmd.ExecuteNonQuery()
>
> Finally
> If previousConnectionState = ConnectionState.Closed Then
> myCn.Close()
> End If
> End Try
>
>
> please help if you can........
>
> Fasan
>

Re: Maximum length is 128

am 22.06.2006 16:50:27 von reb01501

fasan21@gmail.com wrote:
> Im trying to insert binary data into a varbinary field in sql 2005
> from vb.net 2003.
>
There was no way for you to know it (except maybe by browsing through
some
of the previous questions before posting yours - always a recommended
practice), but this is a classic asp newsgroup.
ASP.Net is a different technology from classic ASP.
While you may be lucky enough to find a dotnet-savvy person here who
can
answer your question, you can eliminate the luck factor by posting your
question to a newsgroup where the dotnet-savvy people hang out. I
suggest
microsoft.public.dotnet.framework.aspnet.

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.