473,378 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

OleDbCommand Parameters Collection

Hey guys I’m pretty new to this parameterized commands thing and I don’t
know why this code isn’t working. I have a Connection object set up as
Conn, and a Rec object which is holding my values. This method is inside
a "Class Library" being called from a "Windows Form" if that means anything.

string strCmd = "INSERT INTO Table (Value,Value,Value,Value,Value)
VALUES ('?','?','?','?','?')";
OleDbCommand cmd = new OleDbCommand(strCmd,Conn);
cmd.Parameters.Add(Rec.Value1);
cmd.Parameters.Add(Rec.Value2);
cmd.Parameters.Add(Rec.Value3);
cmd.Parameters.Add(Rec..Value4);
cmd.Parameters.Add(Rec.Value5);

I keep getting this error:

“An unhandled exception of type 'System.InvalidCastException' occurred
in system.data.dll

Additional information: The OleDbParameterCollection only accepts
non-null OleDbParameter type objects, not Int32 objects.”

Any help or input would be appreciated

-Adam
Nov 17 '05 #1
2 13142

Adam wrote:

Change this:
string strCmd = "INSERT INTO Table (Value,Value,Value,Value,Value)
VALUES ('?','?','?','?','?')";
To this:

string strCmd
= "INSERT INTO Table (Value1, Value2, Value3, Value4, Value5 )"
+ "VALUES ( ?, ?, ?, ?, ? ) //note no single quotes.
;

Then change this: cmd.Parameters.Add(Rec.Value1);
cmd.Parameters.Add(Rec.Value2);
cmd.Parameters.Add(Rec.Value3);
cmd.Parameters.Add(Rec..Value4);
cmd.Parameters.Add(Rec.Value5);


To this:
cmd.Parameters.Add( "@Value1", OleDbType.VarChar, 10 ).Value =
Rec.Value1;
cmd.Parameters.Add( "@Value2", OleDbType.Integer ).Value = Rev.Value2;
..
..
And so on.

Note that you have to specify the data type of each parameter, I have
just used VarChar and Integer as examples.

Then it should work fine.

Nov 17 '05 #2
Thx for the quick reply man
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: | last post by:
Hi, I was performing SQL UPDATE queries and I notice that they SUCCEED on the ExecuteNonQuery() call with NO exceptions raised BUT they fail at the Database. They say they succeed in the code...
0
by: TF | last post by:
Hi, Scenario: I have few TextBoxes on a WinForm, bound to a DataSet (using DataBindings collection). Initially only schema is filled in DataSet (using FillSchema() method), then a new row is...
1
by: Jason Steeves | last post by:
-Dim cmd As OleDbCommand -cmd = New OleDbCommand("select * from qmsstats where firstname='jason'", OleDbConnection1) -OleDbDataAdapter2.SelectCommand = cmd -DataGrid1.Visible = True...
0
by: jerim | last post by:
Hi I want to run an Updatequery against my Access DB, but it writes the false Value into the Table. Code: string oleConnectionString = "Provider=........"; string update = "UPDATE...
0
by: RG | last post by:
OleDbCommand.Prepare error: Size of parameter (VB.NET 2003) I need to do a SQL INSERT statement into an Access table, and I’d like to use the exact technique described in the Help file example...
4
by: Agnes | last post by:
Dim authConnection As New OleDbConnection("Provider = VfpOleDB.1;Data Source = d:\cgl\vfpmaster\ttsdata\tts.dbc") authConnection.Open() Dim authCommand As String = "select loginid from coinfo...
3
by: Jak | last post by:
I think I have set all the required parameters of the oledbcommand already, but the procedure still refuse to proceed, it sayes that there must be one or more parameters not set. The following...
5
by: =?Utf-8?B?bXVzb3NkZXY=?= | last post by:
Hi, I'm trying to fill my Repeater control with the contents of a query performed on an OleDB connected database. I keep getting the error... "No value given for one or more required...
0
by: =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?= | last post by:
sippyuconn wrote: The purpose of reusing a command is to set it up with parameters and everything and then call the Prepare method. Then each time you use the command you just assign values to...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.