Problem with AddWithValue

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I"m trying to insert into a database with no adapter .(AddWithValue) I get
an error when
executing ExcecuteNonQuery . Parameter pName has no default value. what am I
doing wrong ?

using (OleDbConnection conn = new OleDbConnection(

testacc.Properties.Settings.Default.mytestConnectionString))
{

using (OleDbCommand comm = new OleDbCommand(
"INSERT INTO phonebook (name, phonenum) VALUES
(pName, pPhonenum)", conn))
{
comm.Parameters.Add("pName",
OleDbType.VarChar).SourceColumn = "name";

comm.Parameters.Add("pPhonenum",
OleDbType.VarChar).SourceColumn = "phonenum";
comm.Parameters.AddWithValue("pName", "John Doe");
comm.Parameters.AddWithValue("pPhonenum",
"555-555-1212");
conn.Open();
comm.ExecuteNonQuery();

}
 
I appreciate your help.
I still don't understand. Do I not use the .Add to set up the fields ?
Or am I missing a third step ?

I appoligize for being confused ...
 
I figured out how it's done.
string tname = "simon";
string tphonenum = "555-555-1212";

comm.Parameters.Add("pName", OleDbType.VarChar, tname.Length,"name").Value =
tname;

comm.Parameters.Add("pPhonenum",
OleDbType.VarChar,tphonenum.Length,"phonenum").Value = tphonenum;
this will do the trick.
 
Hi,

The "Add" method adds the parameter and the "AddWithValue" method adds the
parameter along with its value, simultaneously. You don't need to use both
"Add" and "AddWithValue". Use one or the other for each parameter.

comm.Parameters.AddWithValue("pName", tname);
comm.Parameters.AddWithValue("phonenum", tphonenum);
 
Hi,

The OleDB documentation on MSDN and the notations itself areconfusing.

comm.Parameters.Add("?", OleDbType.VarChar);
comm.Parameters[0]].Value = tphonenum;

is enough,

As Dave wrote you can do it as well with the AddWithValue which replaces the
overloaded Add from 1.x in version 2.0. The caveat at that, is that you can
only do it once (because it is a collection), which is seldom what is
wanted.

Be aware that this is fonfusing because this is where SQLClient and OleDb
have different notations but on many places is in MSDN taken the SQLClient
notation at OleDb. Which does not go wrong by the way, but OleDb has AFAIK
no named parameters which are working. (You can type everything there).

I hope this gives some ideas

Cor


comm.Parameters.Add("pPhonenum",
OleDbType.VarChar,tphonenum.Length,"phonenum").Value = tphonenum;
 
Also you need to be clear which provider you are using. Some
providers, e.g. Sybase ASA will throw an exception if you use named
parameters.

Hi,

The OleDB documentation on MSDN and the notations itself areconfusing.

comm.Parameters.Add("?", OleDbType.VarChar);
comm.Parameters[0]].Value = tphonenum;

is enough,

As Dave wrote you can do it as well with the AddWithValue which replaces the
overloaded Add from 1.x in version 2.0. The caveat at that, is that you can
only do it once (because it is a collection), which is seldom what is
wanted.

Be aware that this is fonfusing because this is where SQLClient and OleDb
have different notations but on many places is in MSDN taken the SQLClient
notation at OleDb. Which does not go wrong by the way, but OleDb has AFAIK
no named parameters which are working. (You can type everything there).

I hope this gives some ideas

Cor


comm.Parameters.Add("pPhonenum",
OleDbType.VarChar,tphonenum.Length,"phonenum").Value = tphonenum;


SLIMSHIM said:
I figured out how it's done.
string tname = "simon";
string tphonenum = "555-555-1212";

comm.Parameters.Add("pName", OleDbType.VarChar, tname.Length,"name").Value
=
tname;

comm.Parameters.Add("pPhonenum",
OleDbType.VarChar,tphonenum.Length,"phonenum").Value = tphonenum;
this will do the trick.
 

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

Back
Top