ExecuteNonQuery requires a parameter

  • Thread starter Thread starter Matt
  • Start date Start date
M

Matt

Hi all,

I'm trying to use the following code in an application:

// retrieve the connection string
string strDbcx = Globals.strDbcx;
// create the connection
OleDbConnection dbcx = new OleDbConnection(strDbcx);
// open the connection
dbcx.Open();

string sqlComplete;

DateTime confirmedDate = DateTime.Now;

sqlComplete = "UPDATE installs SET installConfirmed =
1,installDateConfirmed = confirmedDate WHERE installId = " +
lsbSelectInstallToClose.SelectedValue.ToString();

OleDbCommand cmdCompleteInstall = new
OleDbCommand(sqlComplete, dbcx);
cmdCompleteInstall.ExecuteNonQuery();

/*MessageBox.Show(sqlComplete);

OleDbCommand cmdCompleteInstall = new
OleDbCommand(sqlComplete, dbcx);
cmdCompleteInstall.ExecuteNonQuery();*/
dbcx.Close();

however I get the following error:

System.Data.OleDb.OleDbException was unhandled
Message="No value given for one or more required parameters."

The connection string works across the rest of my application, so why
won't it work here?!!

Thanks in advance,

Matt
 
Matt said:
Hi all,

I'm trying to use the following code in an application:

// retrieve the connection string
string strDbcx = Globals.strDbcx;
// create the connection
OleDbConnection dbcx = new OleDbConnection(strDbcx);
// open the connection
dbcx.Open();

string sqlComplete;

DateTime confirmedDate = DateTime.Now;

sqlComplete = "UPDATE installs SET installConfirmed =
1,installDateConfirmed = confirmedDate WHERE installId = " +
lsbSelectInstallToClose.SelectedValue.ToString();

OleDbCommand cmdCompleteInstall = new
OleDbCommand(sqlComplete, dbcx);
cmdCompleteInstall.ExecuteNonQuery();

/*MessageBox.Show(sqlComplete);

OleDbCommand cmdCompleteInstall = new
OleDbCommand(sqlComplete, dbcx);
cmdCompleteInstall.ExecuteNonQuery();*/
dbcx.Close();

however I get the following error:

System.Data.OleDb.OleDbException was unhandled
Message="No value given for one or more required parameters."

The connection string works across the rest of my application, so why
won't it work here?!!

It's not a problem with the connection string, it's a problem with the
query string - you've misspelled one of your column names.
 
Larry said:
It's not a problem with the connection string, it's a problem with the
query string - you've misspelled one of your column names.

Larry,

Thanks for the prompt reply, however I've checked my column names on
both the database and the sql statement and they are the same.

Is there anything else that requires looking at? Is it something really
silly like you can only update one field at a time and I need to run
the update query for each field? I'm going to try that anyway and see
what happens.

Cheers,

Matt
 
If installDateConfirmed is a required parameter, perhaps this:

DateTime confirmedDate = DateTime.Now;

sqlComplete = "UPDATE installs SET installConfirmed = 1,installDateConfirmed
= " + confirmedDate.ToString() + " WHERE installId = " +
lsbSelectInstallToClose.SelectedValue.ToString();
 
Small addition:

if "installDateConfirmed" column in the database is a DateTime type, not a
text type, you need to use single quote mark in the SQL statement:

sqlComplete = "UPDATE installs SET installConfirmed = 1,installDateConfirmed
= '" + confirmedDate.ToString() + "' WHERE installId = " +
lsbSelectInstallToClose.SelectedValue.ToString();
 
Back
Top