Help with prepare statement during an Insert

F

fmarchioni

Hi all,
I'm trying to do an insert using MySQL OleDb Provider.
I'd like to use the prepare() method of OleDbCommand class
to precompile the statement.
The problem is that when I issue prepare() I get an error
"OleDbCommand.Prepare requires all parameters to have an explicitly set
type"

I have checked the parameters and it seems to me that I didn't forget
anything....anybody can help me ?
Thanks a lot
here's my code:

System.Data.OleDb.OleDbConnection con;
con=new System.Data.OleDb.OleDbConnection("");
con.ConnectionString="Provider=MySqlProv.3.0;Data
Source=fad;Password=admin;User ID=admin";

try
{
con.Open();
OleDbCommand cmd = new OleDbCommand(null,con);

cmd.CommandText = "INSERT into ANAGRAFICA (ANAG_RS," +
"ANAG_INDIRIZZO,"+
"ANAG_CF," +
"ANAG_CAP," +
"ANAG_EMAIL," +
"ANAG_RIFERIMENTO," +
"ANAG_PHONE1," +
"ANAG_PHONE2)" +
" values ( " +
"@ANAG_RS," +
"@ANAG_INDIRIZZO," +
"@ANAG_CF," +
"@ANAG_CAP," +
"@ANAG_EMAIL," +
"@ANAG_RIFERIMENTO,"+
"@ANAG_PHONE1,"+
"@ANAG_PHONE2)";


cmd.Parameters.Add("@ANAG_RS" ,this.textRS.Text);
cmd.Parameters.Add ("@ANAG_INDIRIZZO" ,this.textIndirizzo.Text);

cmd.Parameters.Add ("@ANAG_CF" ,this.textCF.Text);
cmd.Parameters.Add ("@ANAG_CAP" , this.textCap.Text);
cmd.Parameters.Add ("@ANAG_EMAIL" , this.textMail.Text);
cmd.Parameters.Add ("@ANAG_RIFERIMENTO", this.textRiferimento.Text);

cmd.Parameters.Add ("@ANAG_PHONE1", this.textTelefono1.Text);
cmd.Parameters.Add ("@ANAG_PHONE2", this.textTelefono2.Text);
cmd.Prepare ( );
cmd.ExecuteNonQuery ( );
 
O

Ollie Riches

you haven't set the parameter types when adding the parameters into the
collection, just as the exception says.

e.g.

myParamCollection.Add("CustomerID", OleDbType.VarChar)

HTH

Ollie Riches
 
N

Nick Malik [Microsoft]

Hi all,
I'm trying to do an insert using MySQL OleDb Provider.
I'd like to use the prepare() method of OleDbCommand class
to precompile the statement.
The problem is that when I issue prepare() I get an error
"OleDbCommand.Prepare requires all parameters to have an explicitly set
type"

I have checked the parameters and it seems to me that I didn't forget
anything....anybody can help me ?
Thanks a lot
here's my code:
cmd.Parameters.Add("@ANAG_RS" ,this.textRS.Text);
cmd.Parameters.Add ("@ANAG_INDIRIZZO" ,this.textIndirizzo.Text);

You didn't provide a single OleDb data type on your cmd.Parameters.Add call.
See
http://msdn.microsoft.com/library/e...edboledbparametercollectionclassaddtopic6.asp


I'd expect these lines to read:
cmd.Parameters.Add("@ANAG_RS" ,OleDbType.VarChar, this.textRS.Text.Length,
this.textRS.Text);
cmd.Parameters.Add ("@ANAG_INDIRIZZO"
,OleDbType.VarChar,this.textIndirizzo.Text.Length, this.textIndirizzo.Text);

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 

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

Top