473,387 Members | 1,486 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,387 software developers and data experts.

Problem with AddWithValue

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.mytestConnecti onString))
{

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();

}

Nov 12 '06 #1
6 11107
Slimshim,

If I see it well, than you only need to set the value.
The AddWithValue is to add a parameteritem to the parametercollection with a
value.
(and than use an indexer to tell which parameter from the collection).

http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

Now you are in my idea trying to do that 4 times.

Cor

"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:68**********************************@microsof t.com...
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.mytestConnecti onString))
{

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();

}

Nov 12 '06 #2
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 ...

"Cor Ligthert [MVP]" wrote:
Slimshim,

If I see it well, than you only need to set the value.
The AddWithValue is to add a parameteritem to the parametercollection with a
value.
(and than use an indexer to tell which parameter from the collection).

http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

Now you are in my idea trying to do that 4 times.

Cor

"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:68**********************************@microsof t.com...
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.mytestConnecti onString))
{

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();

}


Nov 12 '06 #3
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").Val ue = tphonenum;
this will do the trick.

"SLIMSHIM" wrote:
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 ...

"Cor Ligthert [MVP]" wrote:
Slimshim,

If I see it well, than you only need to set the value.
The AddWithValue is to add a parameteritem to the parametercollection with a
value.
(and than use an indexer to tell which parameter from the collection).

http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

Now you are in my idea trying to do that 4 times.

Cor

"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:68**********************************@microsof t.com...
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.mytestConnecti onString))
{
>
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();
>
}
>
Nov 12 '06 #4
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);

--
Dave Sexton

"SLIMSHIM" <SL******@discussions.microsoft.comwrote in message
news:46**********************************@microsof t.com...
>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").Val ue = tphonenum;
this will do the trick.

"SLIMSHIM" wrote:
>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 ...

"Cor Ligthert [MVP]" wrote:
Slimshim,

If I see it well, than you only need to set the value.
The AddWithValue is to add a parameteritem to the parametercollection
with a
value.
(and than use an indexer to tell which parameter from the collection).

http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

Now you are in my idea trying to do that 4 times.

Cor

"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:68**********************************@microsof t.com...
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.mytestConnecti onString))
{

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();

}


Nov 12 '06 #5
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").Val ue = tphonenum;
"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:46**********************************@microsof t.com...
>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").Val ue = tphonenum;
this will do the trick.

"SLIMSHIM" wrote:
>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 ...

"Cor Ligthert [MVP]" wrote:
Slimshim,

If I see it well, than you only need to set the value.
The AddWithValue is to add a parameteritem to the parametercollection
with a
value.
(and than use an indexer to tell which parameter from the collection).

http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

Now you are in my idea trying to do that 4 times.

Cor

"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:68**********************************@microsof t.com...
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.mytestConnecti onString))
{

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();

}


Nov 13 '06 #6
Rad
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.

On Mon, 13 Nov 2006 05:42:30 +0100, "Cor Ligthert [MVP]"
<no************@planet.nlwrote:
>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").Va lue = tphonenum;
"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:46**********************************@microso ft.com...
>>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").Va lue = tphonenum;
this will do the trick.

"SLIMSHIM" wrote:
>>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 ...

"Cor Ligthert [MVP]" wrote:

Slimshim,

If I see it well, than you only need to set the value.
The AddWithValue is to add a parameteritem to the parametercollection
with a
value.
(and than use an indexer to tell which parameter from the collection).

http://msdn2.microsoft.com/en-us/library/6h86wyk1.aspx

Now you are in my idea trying to do that 4 times.

Cor

"SLIMSHIM" <SL******@discussions.microsoft.comschreef in bericht
news:68**********************************@microso ft.com...
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.mytestConnect ionString))
{

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();

}

Nov 13 '06 #7

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

Similar topics

2
by: Jim McGivney | last post by:
Using VS'05 I have a label (LabelFileNum) on an aspx page. I want to set the text of the lable to the last name, which is a field from a table called Ante_pedigree from an Access Database. I...
8
by: Bill Gower | last post by:
I want to store an SQL string into a table and then be able to run it later. Here is the insert command "insert into ProjectSQL(ProjectID, SQLID, SQLString) values( " & locProjectID & ", " &...
1
by: rtwPhoenix | last post by:
The problem. I have created an exam using the control wizard. Most answers are text answers I created the SQL db. Now when I try to read the data it always gives me cast error. I am new at this...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 I've got 2 Calendars on a webpage I'm developing. These Calendards are named "calFrom" and "calTo".. The user will choose from and to date and then perform some operation...
14
by: rashmidutt | last post by:
hello sir i am making project on vb.net language..and project is on hospital management..its major project..and too many fields are present in its data base..i was connecting data base in forms but...
3
by: raghulvarma | last post by:
I am not able to insert values into msacess what could be the error in the below shown statement? string con = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Database2.mdb");...
6
by: raghulvarma | last post by:
I have got a grid view in that I have two label and then a checkbox. In presentation layer ie .cs i have the coding like foreach(GridViewRow row in GridView1.Rows) { if(row.RowType ==...
2
by: sirdavethebrave | last post by:
Hi guys - I have written a form, and a stored procedure to update the said form. It really is as simple as that. A user can go into the form, update some fields and hit the update button to...
15
dbrewerton
by: dbrewerton | last post by:
Ok everyone, when I do this update clause using code behind it gives me a stupid error that says: Line: 6 Char: 62099 Error: Sys.WebForms.PageRequestManagerServerErrorException: Format of the...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.