Need help with this Error

  • Thread starter Thread starter news.microsoft.com
  • Start date Start date
N

news.microsoft.com

I have a SQL Server database. I am using a OleDB Provider. I am getting
the following error

An unhandled exception of type 'System.InvalidOperationException' occurred
in System.Data.dll

Additional information: Command parameter[2] '' is invalid.
Command parameter[3] '' is invalid.

Here is the code

_sql = "INSERT INTO QueryBuilderTableViews([ObjectName],[ObjectType]) VALUES
" +

" (?, ?)";

cmd.CommandText = _sql;

OleDbParameter _objectName = new OleDbParameter("@ObjectName",
OleDbType.Char, 50);

OleDbParameter _objectType = new OleDbParameter("@ObjectType",
OleDbType.Char, 1);

_objectName.Value = _name;

_objectType.Value = _type;

cmd.Parameters.Add(_objectName);

cmd.Parameters.Add(_objectType);

cmd.ExecuteNonQuery(); ////ERROR LINE



Thanks

Bill
 
Bill,
First, I have to wonder why you're not using the SqlClient provider, but I
assume you know what you're doing.
Next, I have to wonder why you're not using the OleDb New Constructor that
instantiates the Parameter and appends it to the Parameters collection in a
single line of code as shown below.

Imports System.Data.OleDb

Public Class Form1

Dim _type As String

Dim _object As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Try

Dim cmd As New OleDbCommand( _

"INSERT INTO QueryBuilderTableViews([ObjectName],[ObjectType]) VALUES (?,
?)")

cmd.Parameters.Add("@ObjectName", OleDbType.Char, 50).Value = _name

cmd.Parameters.AddWithValue("@objectType", _type)

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

End Class

Without understanding why you're using CHAR instead of VarChar or NVarChar
and what the schema of the database looks like, again, it's hard to help
you.

Lastly, I also doubt your name is "news.microsoft.com" unless your father
has a special relationship with the other Bill. ;)
--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
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.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
Back
Top