How to add a record to a table?

  • Thread starter Thread starter Binary Poet
  • Start date Start date
B

Binary Poet

Is there a simple way to simply add a record to a table without needing to
do a query, fill a dataset, add the row, etc....?
I do not want to use the "Insert into tablename......" as shown below...

SQLText="Insert into tablename (.......) value(........)" ect......
OLEConn.Open()
OLECommand.CommandText = SQLText
OLECommand.Connection = OLEConn
OLEDR = OLECommand.ExecuteReader
OLECommand.Dispose()
 
You don't need to do a Fill. You would have to create a table with the
appropriate schema, and then either define an Insert command, or use the
commandbuilder, and then call Update on the data adapter.

If you ask me, if this is just for one insert, it will be easier to just run
an insert statement. If you need a more robust, generic way of doing it, you
will need to use the method I described above.
 
I'm with Marina. I would certainly use a Command object and build a
Parameters collection to pass in the values. This will automatically deal
with a lot of issues you have not encountered (yet).

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Thanks for the reply...

I am trying to build a generic method for just one table.. The problem is I
am storing text for pages that were processed from an OCR engine and I get
alot
of ' and " in the text..... which just create havoc tyring to do inserts....

Any other ideas?
 
Well, one way is to replace every single quote in a parameter with 2. That
should eliminate syntax errors, not to mention hacking attempts.

A more robust way though, is when you define your insert command to use
parameter placeholders, and then add parameters with appropriate values.
Taking care of any special characters will all be done for you.
 
Marina,

If I could bother you to show me a sample or a link to what you are
descibing would help more than I can describe.
I was doing a Replace, but read somewhere that this does not help with
performance much

Private Function ReplaceQuotes(ByVal InputString As String) As String

Dim ResultString As String

ResultString = Replace(InputString, "'", "''")

Return ResultString

End Function
 
Again, Marina is right on. Don't bother with the imbedded quotes yourself.
Let ADO.NET do it for you with a Parameter. This also eliminates (or
reduces) the chance of a SQL injection attack.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Can someone point me in the right direction here?

This is what I am trying to do based on your sugestions and what I could
find on the net. It is not working and throughing an error


Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ", @OCRTEXT)", OLEConn)

Dim p1 As OleDbParameter = cmd.Parameters.Add("@OCRTEXT",
OleDbType.LongVarChar)

p1.Value = OCRText

OLECommand.Connection = OLEConn

OLEDR = OLECommand.ExecuteReader

OLECommand.Dispose()


The table I am trying to insert into is 3 columns
DocID as Int
PageNumber as int
OCRTextData as varchar
 
When you say 'throwing an error' in a newsgroup, you must say what the error
and the error message are. Otherwise, that information is not very helpful
to the people trying to help you.

To run insert/update/delete statements, use ExecuteNonQuery. ExecuteReader
is meant for queries that return result sets, i.e. SELECT queries.

Also, as long as you are going for parameters, you might want to use
parameters for all your columns, including DocID and PageNum.
 
Marina,

Good point, sorry about that...
I will add the others as parameters as soon as I can get the basic's
working.....


Here is the updated code that I am trying to execute:

Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ",@OCRTEXT)", OLEConn)
With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With
cmd.parameters("@OCRTEXT").value = OCRText
Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The error I get is:
"Exception has been thrown by the target of an invocation"

The stack trace is:

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

at Microsoft.VisualBasic.CompilerServices.LateBinding.FastCall(Object o,
MethodBase method, ParameterInfo[] Parameters, Object[] args, Type objType,
IReflect objIReflect)

at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack, Boolean IgnoreReturn)

at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)

at aXs.OCR.SaveOCRDataForPage(Int64 DocID, Int32 PageNum, String OCRText) in
C:\aXsInfo\Source\aXsNet3.0\aXs\aXsNet.vb:line 251



Many thanks again!

Steve
 
Are you calling this code using reflection? If so, try calling
GetBaseException on the exception to see the real exception.

If that is the exception you are getting in the catch block, then I'm not
sure I know the cause. Sorry...

Binary Poet said:
Marina,

Good point, sorry about that...
I will add the others as parameters as soon as I can get the basic's
working.....


Here is the updated code that I am trying to execute:

Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ",@OCRTEXT)", OLEConn)
With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With
cmd.parameters("@OCRTEXT").value = OCRText
Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The error I get is:
"Exception has been thrown by the target of an invocation"

The stack trace is:

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

at Microsoft.VisualBasic.CompilerServices.LateBinding.FastCall(Object o,
MethodBase method, ParameterInfo[] Parameters, Object[] args, Type objType,
IReflect objIReflect)

at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack, Boolean IgnoreReturn)

at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)

at aXs.OCR.SaveOCRDataForPage(Int64 DocID, Int32 PageNum, String OCRText) in
C:\aXsInfo\Source\aXsNet3.0\aXs\aXsNet.vb:line 251



Many thanks again!

Steve





Marina said:
When you say 'throwing an error' in a newsgroup, you must say what the
error
and the error message are. Otherwise, that information is not very helpful
to the people trying to help you.

To run insert/update/delete statements, use ExecuteNonQuery. ExecuteReader
is meant for queries that return result sets, i.e. SELECT queries.

Also, as long as you are going for parameters, you might want to use
parameters for all your columns, including DocID and PageNum.

with
the
 
Reflection?
I understand the basic's there but not doing anything special....

Here is the original code that seems to work, but based on yours and "Bill"
recomendation was trying to change it to use parms to avoid some of the text
cleanup issues involved

Dim SendString As String
SendString = "Insert into DocumentFullText values(" & DocID & "," & PageNum
& ",'" & ReplaceQuotes(OCRText) & "')"
OLECommand.CommandText = SendString
OLECommand.Connection = OLEConn
OLEDR = OLECommand.ExecuteReader
OLECommand.Dispose()


Private Function ReplaceQuotes(ByVal InputString As String) As String

Dim CleanedString As String
CleanedString = Replace(InputString, "'", "''")
Return CleanedString

End Function





Marina said:
Are you calling this code using reflection? If so, try calling
GetBaseException on the exception to see the real exception.

If that is the exception you are getting in the catch block, then I'm not
sure I know the cause. Sorry...

Binary Poet said:
Marina,

Good point, sorry about that...
I will add the others as parameters as soon as I can get the basic's
working.....


Here is the updated code that I am trying to execute:

Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ",@OCRTEXT)", OLEConn)
With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With
cmd.parameters("@OCRTEXT").value = OCRText
Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The error I get is:
"Exception has been thrown by the target of an invocation"

The stack trace is:

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

at Microsoft.VisualBasic.CompilerServices.LateBinding.FastCall(Object o,
MethodBase method, ParameterInfo[] Parameters, Object[] args, Type objType,
IReflect objIReflect)

at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack, Boolean IgnoreReturn)

at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)

at aXs.OCR.SaveOCRDataForPage(Int64 DocID, Int32 PageNum, String OCRText) in
C:\aXsInfo\Source\aXsNet3.0\aXs\aXsNet.vb:line 251



Many thanks again!

Steve





Marina said:
When you say 'throwing an error' in a newsgroup, you must say what the
error
and the error message are. Otherwise, that information is not very helpful
to the people trying to help you.

To run insert/update/delete statements, use ExecuteNonQuery. ExecuteReader
is meant for queries that return result sets, i.e. SELECT queries.

Also, as long as you are going for parameters, you might want to use
parameters for all your columns, including DocID and PageNum.

Can someone point me in the right direction here?

This is what I am trying to do based on your sugestions and what I could
find on the net. It is not working and throughing an error


Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText
values(" &
DocID & "," & PageNum & ", @OCRTEXT)", OLEConn)

Dim p1 As OleDbParameter = cmd.Parameters.Add("@OCRTEXT",
OleDbType.LongVarChar)

p1.Value = OCRText

OLECommand.Connection = OLEConn

OLEDR = OLECommand.ExecuteReader

OLECommand.Dispose()


The table I am trying to insert into is 3 columns
DocID as Int
PageNumber as int
OCRTextData as varchar




Again, Marina is right on. Don't bother with the imbedded quotes
yourself.
Let ADO.NET do it for you with a Parameter. This also eliminates (or
reduces) the chance of a SQL injection attack.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

Well, one way is to replace every single quote in a parameter with 2.
That
should eliminate syntax errors, not to mention hacking attempts.

A more robust way though, is when you define your insert command to
use
parameter placeholders, and then add parameters with appropriate
values.
Taking care of any special characters will all be done for you.

Thanks for the reply...

I am trying to build a generic method for just one table.. The
problem
is
I
am storing text for pages that were processed from an OCR engine and
I
get
alot
of ' and " in the text..... which just create havoc tyring to do
inserts....

Any other ideas?



You don't need to do a Fill. You would have to create a table with
the
appropriate schema, and then either define an Insert command, or
use
the
commandbuilder, and then call Update on the data adapter.

If you ask me, if this is just for one insert, it will be easier to
just
run
an insert statement. If you need a more robust, generic way of
doing
it,
you
will need to use the method I described above.

Is there a simple way to simply add a record to a table without
needing
to
do a query, fill a dataset, add the row, etc....?
I do not want to use the "Insert into tablename......" as
shown
below...

SQLText="Insert into tablename (.......) value(........)"
ect......
OLEConn.Open()
OLECommand.CommandText = SQLText
OLECommand.Connection = OLEConn
OLEDR = OLECommand.ExecuteReader
OLECommand.Dispose()
 
Here is the result from the GetBaseExecption

System.Data.OleDb.OleDbException: Must declare the variable '@OCRTEXT'.

at System.Data.OleDb.OleDbDataReader.NextResults(IMultipleResults
imultipleResults, OleDbConnection connection, OleDbCommand command)

at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior
behavior, String method)

at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()




Marina said:
Are you calling this code using reflection? If so, try calling
GetBaseException on the exception to see the real exception.

If that is the exception you are getting in the catch block, then I'm not
sure I know the cause. Sorry...

Binary Poet said:
Marina,

Good point, sorry about that...
I will add the others as parameters as soon as I can get the basic's
working.....


Here is the updated code that I am trying to execute:

Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ",@OCRTEXT)", OLEConn)
With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With
cmd.parameters("@OCRTEXT").value = OCRText
Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The error I get is:
"Exception has been thrown by the target of an invocation"

The stack trace is:

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

at Microsoft.VisualBasic.CompilerServices.LateBinding.FastCall(Object o,
MethodBase method, ParameterInfo[] Parameters, Object[] args, Type objType,
IReflect objIReflect)

at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack, Boolean IgnoreReturn)

at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)

at aXs.OCR.SaveOCRDataForPage(Int64 DocID, Int32 PageNum, String OCRText) in
C:\aXsInfo\Source\aXsNet3.0\aXs\aXsNet.vb:line 251



Many thanks again!

Steve





Marina said:
When you say 'throwing an error' in a newsgroup, you must say what the
error
and the error message are. Otherwise, that information is not very helpful
to the people trying to help you.

To run insert/update/delete statements, use ExecuteNonQuery. ExecuteReader
is meant for queries that return result sets, i.e. SELECT queries.

Also, as long as you are going for parameters, you might want to use
parameters for all your columns, including DocID and PageNum.

Can someone point me in the right direction here?

This is what I am trying to do based on your sugestions and what I could
find on the net. It is not working and throughing an error


Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText
values(" &
DocID & "," & PageNum & ", @OCRTEXT)", OLEConn)

Dim p1 As OleDbParameter = cmd.Parameters.Add("@OCRTEXT",
OleDbType.LongVarChar)

p1.Value = OCRText

OLECommand.Connection = OLEConn

OLEDR = OLECommand.ExecuteReader

OLECommand.Dispose()


The table I am trying to insert into is 3 columns
DocID as Int
PageNumber as int
OCRTextData as varchar




Again, Marina is right on. Don't bother with the imbedded quotes
yourself.
Let ADO.NET do it for you with a Parameter. This also eliminates (or
reduces) the chance of a SQL injection attack.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

Well, one way is to replace every single quote in a parameter with 2.
That
should eliminate syntax errors, not to mention hacking attempts.

A more robust way though, is when you define your insert command to
use
parameter placeholders, and then add parameters with appropriate
values.
Taking care of any special characters will all be done for you.

Thanks for the reply...

I am trying to build a generic method for just one table.. The
problem
is
I
am storing text for pages that were processed from an OCR engine and
I
get
alot
of ' and " in the text..... which just create havoc tyring to do
inserts....

Any other ideas?



You don't need to do a Fill. You would have to create a table with
the
appropriate schema, and then either define an Insert command, or
use
the
commandbuilder, and then call Update on the data adapter.

If you ask me, if this is just for one insert, it will be easier to
just
run
an insert statement. If you need a more robust, generic way of
doing
it,
you
will need to use the method I described above.

Is there a simple way to simply add a record to a table without
needing
to
do a query, fill a dataset, add the row, etc....?
I do not want to use the "Insert into tablename......" as
shown
below...

SQLText="Insert into tablename (.......) value(........)"
ect......
OLEConn.Open()
OLECommand.CommandText = SQLText
OLECommand.Connection = OLEConn
OLEDR = OLECommand.ExecuteReader
OLECommand.Dispose()
 
Well go figure....
I solved the problem by changing the @OCRTEXT out to a ? in the Insert line
and it works fine...

Here is the new code working like a champ......


Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ",?)", OLEConn)

With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With

cmd.parameters("@OCRTEXT").value = OCRText

Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
Debug.WriteLine(ex.GetBaseException.ToString
End Try


Many thanks to you and "Bill"

Steve


Marina said:
Are you calling this code using reflection? If so, try calling
GetBaseException on the exception to see the real exception.

If that is the exception you are getting in the catch block, then I'm not
sure I know the cause. Sorry...

Binary Poet said:
Marina,

Good point, sorry about that...
I will add the others as parameters as soon as I can get the basic's
working.....


Here is the updated code that I am trying to execute:

Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values(" &
DocID & "," & PageNum & ",@OCRTEXT)", OLEConn)
With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With
cmd.parameters("@OCRTEXT").value = OCRText
Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The error I get is:
"Exception has been thrown by the target of an invocation"

The stack trace is:

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

at Microsoft.VisualBasic.CompilerServices.LateBinding.FastCall(Object o,
MethodBase method, ParameterInfo[] Parameters, Object[] args, Type objType,
IReflect objIReflect)

at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack, Boolean IgnoreReturn)

at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)

at aXs.OCR.SaveOCRDataForPage(Int64 DocID, Int32 PageNum, String OCRText) in
C:\aXsInfo\Source\aXsNet3.0\aXs\aXsNet.vb:line 251



Many thanks again!

Steve





Marina said:
When you say 'throwing an error' in a newsgroup, you must say what the
error
and the error message are. Otherwise, that information is not very helpful
to the people trying to help you.

To run insert/update/delete statements, use ExecuteNonQuery. ExecuteReader
is meant for queries that return result sets, i.e. SELECT queries.

Also, as long as you are going for parameters, you might want to use
parameters for all your columns, including DocID and PageNum.

Can someone point me in the right direction here?

This is what I am trying to do based on your sugestions and what I could
find on the net. It is not working and throughing an error


Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText
values(" &
DocID & "," & PageNum & ", @OCRTEXT)", OLEConn)

Dim p1 As OleDbParameter = cmd.Parameters.Add("@OCRTEXT",
OleDbType.LongVarChar)

p1.Value = OCRText

OLECommand.Connection = OLEConn

OLEDR = OLECommand.ExecuteReader

OLECommand.Dispose()


The table I am trying to insert into is 3 columns
DocID as Int
PageNumber as int
OCRTextData as varchar




Again, Marina is right on. Don't bother with the imbedded quotes
yourself.
Let ADO.NET do it for you with a Parameter. This also eliminates (or
reduces) the chance of a SQL injection attack.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

Well, one way is to replace every single quote in a parameter with 2.
That
should eliminate syntax errors, not to mention hacking attempts.

A more robust way though, is when you define your insert command to
use
parameter placeholders, and then add parameters with appropriate
values.
Taking care of any special characters will all be done for you.

Thanks for the reply...

I am trying to build a generic method for just one table.. The
problem
is
I
am storing text for pages that were processed from an OCR engine and
I
get
alot
of ' and " in the text..... which just create havoc tyring to do
inserts....

Any other ideas?



You don't need to do a Fill. You would have to create a table with
the
appropriate schema, and then either define an Insert command, or
use
the
commandbuilder, and then call Update on the data adapter.

If you ask me, if this is just for one insert, it will be easier to
just
run
an insert statement. If you need a more robust, generic way of
doing
it,
you
will need to use the method I described above.

Is there a simple way to simply add a record to a table without
needing
to
do a query, fill a dataset, add the row, etc....?
I do not want to use the "Insert into tablename......" as
shown
below...

SQLText="Insert into tablename (.......) value(........)"
ect......
OLEConn.Open()
OLECommand.CommandText = SQLText
OLECommand.Connection = OLEConn
OLEDR = OLECommand.ExecuteReader
OLECommand.Dispose()
 
Okay, since you're using OLE DB, you have to mark parameters with the ?
character (as you have discovered). I really think you need to read one of
the books out there on ADO.NET. I've written one of about 25 that are
available. These details are covered in depth in most of them.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Binary Poet said:
Here is the result from the GetBaseExecption

System.Data.OleDb.OleDbException: Must declare the variable '@OCRTEXT'.

at System.Data.OleDb.OleDbDataReader.NextResults(IMultipleResults
imultipleResults, OleDbConnection connection, OleDbCommand command)

at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior
behavior, String method)

at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()




Marina said:
Are you calling this code using reflection? If so, try calling
GetBaseException on the exception to see the real exception.

If that is the exception you are getting in the catch block, then I'm not
sure I know the cause. Sorry...

Binary Poet said:
Marina,

Good point, sorry about that...
I will add the others as parameters as soon as I can get the basic's
working.....


Here is the updated code that I am trying to execute:

Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText values("
&
DocID & "," & PageNum & ",@OCRTEXT)", OLEConn)
With cmd.parameters
.add(New OleDbParameter("@OCRTEXT", OleDb.OleDbType.VarChar))
End With
cmd.parameters("@OCRTEXT").value = OCRText
Try
cmd.executeNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

The error I get is:
"Exception has been thrown by the target of an invocation"

The stack trace is:

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo
culture, Boolean verifyAccess)

at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)

at Microsoft.VisualBasic.CompilerServices.LateBinding.FastCall(Object o,
MethodBase method, ParameterInfo[] Parameters, Object[] args, Type objType,
IReflect objIReflect)

at
Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object
o, Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack, Boolean IgnoreReturn)

at Microsoft.VisualBasic.CompilerServices.LateBinding.LateCall(Object o,
Type objType, String name, Object[] args, String[] paramnames, Boolean[]
CopyBack)

at aXs.OCR.SaveOCRDataForPage(Int64 DocID, Int32 PageNum, String
OCRText) in
C:\aXsInfo\Source\aXsNet3.0\aXs\aXsNet.vb:line 251



Many thanks again!

Steve





When you say 'throwing an error' in a newsgroup, you must say what the
error
and the error message are. Otherwise, that information is not very helpful
to the people trying to help you.

To run insert/update/delete statements, use ExecuteNonQuery. ExecuteReader
is meant for queries that return result sets, i.e. SELECT queries.

Also, as long as you are going for parameters, you might want to use
parameters for all your columns, including DocID and PageNum.

Can someone point me in the right direction here?

This is what I am trying to do based on your sugestions and what I could
find on the net. It is not working and throughing an error


Dim cmd = New OleDb.OleDbCommand("Insert into DocumentFullText
values(" &
DocID & "," & PageNum & ", @OCRTEXT)", OLEConn)

Dim p1 As OleDbParameter = cmd.Parameters.Add("@OCRTEXT",
OleDbType.LongVarChar)

p1.Value = OCRText

OLECommand.Connection = OLEConn

OLEDR = OLECommand.ExecuteReader

OLECommand.Dispose()


The table I am trying to insert into is 3 columns
DocID as Int
PageNumber as int
OCRTextData as varchar




message
Again, Marina is right on. Don't bother with the imbedded quotes
yourself.
Let ADO.NET do it for you with a Parameter. This also eliminates
(or
reduces) the chance of a SQL injection attack.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

Well, one way is to replace every single quote in a parameter with 2.
That
should eliminate syntax errors, not to mention hacking attempts.

A more robust way though, is when you define your insert command
to
use
parameter placeholders, and then add parameters with appropriate
values.
Taking care of any special characters will all be done for you.

Thanks for the reply...

I am trying to build a generic method for just one table.. The
problem
is
I
am storing text for pages that were processed from an OCR engine and
I
get
alot
of ' and " in the text..... which just create havoc tyring to do
inserts....

Any other ideas?



You don't need to do a Fill. You would have to create a table with
the
appropriate schema, and then either define an Insert command,
or
use
the
commandbuilder, and then call Update on the data adapter.

If you ask me, if this is just for one insert, it will be
easier to
just
run
an insert statement. If you need a more robust, generic way of
doing
it,
you
will need to use the method I described above.

Is there a simple way to simply add a record to a table
without
needing
to
do a query, fill a dataset, add the row, etc....?
I do not want to use the "Insert into tablename......" as
shown
below...

SQLText="Insert into tablename (.......) value(........)"
ect......
OLEConn.Open()
OLECommand.CommandText = SQLText
OLECommand.Connection = OLEConn
OLEDR = OLECommand.ExecuteReader
OLECommand.Dispose()
 
Back
Top