Saving rtf using binary and oledb

A

Adam Maltby

Hi,

I am trying to save some rtf code from an rtf field to a TEXT field on SQL2000.

The code I have been playing with (thanks to Nick for the originally supplied code!) is:
Sub DoByteConversion()

Dim pBytStuff As Byte() = StringToArray(Rtxt_readme.Rtf) 'convert rtf field to byte array

Dim pDBNConnection As OleDbConnection
Dim pDBCCommand As OleDbCommand
pDBNConnection = New OleDbConnection
pDBNConnection.ConnectionString = Globals.udl0Val 'take udk connection from Globals Class
Call pDBNConnection.Open()
If (pDBNConnection.State = ConnectionState.Open) Then
pDBCCommand = pDBNConnection.CreateCommand
pDBCCommand.CommandText = "UPDATE TestsConfig Set ReadMe = @bits Where Type = '" & TestSelected & "'"
pDBCCommand.CommandTimeout = 20
Call addParameter(pDBCCommand, "bits", pBytStuff)
If (pDBCCommand.ExecuteNonQuery() > 0) Then
'OPERATION WAS FINE!
End If
End If
End Sub
Public Function getOleDbType(ByVal iType As Type) As OleDbType
If (iType Is GetType(String)) Then
Return OleDbType.VarChar
ElseIf (iType Is GetType(Integer)) Then
Return OleDbType.Integer
ElseIf (iType Is GetType(Boolean)) Then
Return OleDbType.Boolean
ElseIf (iType Is GetType(Date)) Then
Return OleDbType.Date
ElseIf (iType Is GetType(Char)) Then
Return OleDbType.Char
ElseIf (iType Is GetType(Decimal)) Then
Return OleDbType.Decimal
ElseIf (iType Is GetType(Double)) Then
Return OleDbType.Double
ElseIf (iType Is GetType(Single)) Then
Return OleDbType.Single
ElseIf (iType Is GetType(Byte())) Then
Return OleDbType.Binary
ElseIf (iType Is GetType(Guid)) Then
Return OleDbType.Guid
End If
End Function

Public Sub addParameter(ByVal iCommand As OleDbCommand, ByVal iParameter As String, _
ByVal iValue As Object)

If (iCommand.CommandText.IndexOf(iParameter) <> -1) Then
Dim pODPParameter As OleDbParameter = _
iCommand.Parameters.Add(iParameter, getOleDbType(iValue.GetType))
pODPParameter.Value = iValue
End If
End Sub

However when I run this it I just get an unhandled exception in Data.Dll

Any ideas would be appreciated!

Cheers
Adam
 
S

Sahil Malik

What's your table structure for TestsConfig ?
What kinda database?

- Sahil Malik
http://www.dotnetjunkies.com/weblog/sahilmalik

Hi,

I am trying to save some rtf code from an rtf field to a TEXT field on SQL2000.

The code I have been playing with (thanks to Nick for the originally supplied code!) is:
Sub DoByteConversion()

Dim pBytStuff As Byte() = StringToArray(Rtxt_readme.Rtf) 'convert rtf field to byte array

Dim pDBNConnection As OleDbConnection
Dim pDBCCommand As OleDbCommand
pDBNConnection = New OleDbConnection
pDBNConnection.ConnectionString = Globals.udl0Val 'take udk connection from Globals Class
Call pDBNConnection.Open()
If (pDBNConnection.State = ConnectionState.Open) Then
pDBCCommand = pDBNConnection.CreateCommand
pDBCCommand.CommandText = "UPDATE TestsConfig Set ReadMe = @bits Where Type = '" & TestSelected & "'"
pDBCCommand.CommandTimeout = 20
Call addParameter(pDBCCommand, "bits", pBytStuff)
If (pDBCCommand.ExecuteNonQuery() > 0) Then
'OPERATION WAS FINE!
End If
End If
End Sub
Public Function getOleDbType(ByVal iType As Type) As OleDbType
If (iType Is GetType(String)) Then
Return OleDbType.VarChar
ElseIf (iType Is GetType(Integer)) Then
Return OleDbType.Integer
ElseIf (iType Is GetType(Boolean)) Then
Return OleDbType.Boolean
ElseIf (iType Is GetType(Date)) Then
Return OleDbType.Date
ElseIf (iType Is GetType(Char)) Then
Return OleDbType.Char
ElseIf (iType Is GetType(Decimal)) Then
Return OleDbType.Decimal
ElseIf (iType Is GetType(Double)) Then
Return OleDbType.Double
ElseIf (iType Is GetType(Single)) Then
Return OleDbType.Single
ElseIf (iType Is GetType(Byte())) Then
Return OleDbType.Binary
ElseIf (iType Is GetType(Guid)) Then
Return OleDbType.Guid
End If
End Function

Public Sub addParameter(ByVal iCommand As OleDbCommand, ByVal iParameter As String, _
ByVal iValue As Object)

If (iCommand.CommandText.IndexOf(iParameter) <> -1) Then
Dim pODPParameter As OleDbParameter = _
iCommand.Parameters.Add(iParameter, getOleDbType(iValue.GetType))
pODPParameter.Value = iValue
End If
End Sub

However when I run this it I just get an unhandled exception in Data.Dll

Any ideas would be appreciated!

Cheers
Adam
 
A

Adam Maltby

sql2000
Fields
Q,A,B,C,D,Correct,Expired = VarChar 100
readme (rtf) = Text

Cheers
Adam
 
S

Sushil Chordia

Some more questions:
1. In the given schema, the column name - "Type" doesn't appear; but it appears in the update statement
UPDATE TestsConfig Set ReadMe = @bits Where Type = '" & TestSelected & "'"
2. Also, note that "Explicit conversion from data type binary to text is not allowed."
3. TestSelected doesnt appear in the code snippet, i assume its initialized some where else.
4. If you have already tried the above, can you send the stack trace to know where exactly this is failing.
--
HTH,
Sushil Chordia.
This posting is provided "AS IS" with no warranties, and confers no rights.
sql2000
Fields
Q,A,B,C,D,Correct,Expired = VarChar 100
readme (rtf) = Text

Cheers
Adam
 

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

Top