C# Insert Command Parameter Error Message

  • Thread starter Thread starter Jonathan Presnell via .NET 247
  • Start date Start date
J

Jonathan Presnell via .NET 247

I am trying to insert some data into a sql server 2000 databaseusing c#.net. I am getting the following error message:
SQL Error 8178: Prepared Statement ..... expects @Param1, whichwas not supplied.

Here is the code snippet where all the work takes place:

private void btnSave_Click(object sender, System.EventArgs e)
{
int time = 0;

sqlInsertCommand1.CommandType = CommandType.Text;
sqlInsertCommand1.CommandText = "insert into trainingroomdatavalues(@idnum,@dateof,@timeof,@isamof,@pur)";


sqlInsertCommand1.Parameters.Add(newSqlParameter("@idnum",SqlDbType.Int,4));
sqlInsertCommand1.Parameters["@idnum"].Value = 0;
sqlInsertCommand1.Parameters.Add(newSqlParameter("@dateof",SqlDbType.SmallDateTime,4));
sqlInsertCommand1.Parameters["@dateof"].Value =monthCalendar1.SelectionStart.ToShortDateString();
sqlInsertCommand1.Parameters.Add(newSqlParameter("@timeof",SqlDbType.Int,4));
sqlInsertCommand1.Parameters["@timeof"].Value = 4;
sqlInsertCommand1.Parameters.Add(newSqlParameter("@isamof",SqlDbType.Bit,1));
sqlInsertCommand1.Parameters["@isamof"].Value = 0;
sqlInsertCommand1.Parameters.Add(newSqlParameter("@pur",SqlDbType.NVarChar,500));
sqlInsertCommand1.Parameters["@pur"].Value = "to hell withthis";



try{
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();
sqlConnection1.Close();

MessageBox.Show("The data was successfully entered into thedatabase","Success",MessageBoxButtons.OK);

}
catch (SqlException ee)
{
foreach(SqlError err in ee.Errors)
{
MessageBox.Show("SQL Error " + err.Number + " : " +err.Message);
}
}
finally
{
sqlConnection1.Close();
}

}


I have added test values for the parameters in order to figureout what was going on, but to no avail. If anyone could pointme in the right directon, I would greatly appreciate it. I havesearched this discussion board, but all the previous suggestionshave not worked! Thanks.
 
Somewhere else on that form, I bet you'll find a Parameters.Add @Param1.
Looks like maybe sqlInsertCommand1 was built by the configuration wizard and
has already added some params. You could loop through the collection and
see if it exists, I'm betting it does.

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
I am trying to insert some data into a sql server 2000 database using
c#.net. I am getting the following error message:
SQL Error 8178: Prepared Statement ..... expects @Param1, which was not
supplied.

Here is the code snippet where all the work takes place:

private void btnSave_Click(object sender, System.EventArgs e)
{
int time = 0;

sqlInsertCommand1.CommandType = CommandType.Text;
sqlInsertCommand1.CommandText = "insert into trainingroomdata
values(@idnum,@dateof,@timeof,@isamof,@pur)";


sqlInsertCommand1.Parameters.Add(new
SqlParameter("@idnum",SqlDbType.Int,4));
sqlInsertCommand1.Parameters["@idnum"].Value = 0;
sqlInsertCommand1.Parameters.Add(new
SqlParameter("@dateof",SqlDbType.SmallDateTime,4));
sqlInsertCommand1.Parameters["@dateof"].Value =
monthCalendar1.SelectionStart.ToShortDateString();
sqlInsertCommand1.Parameters.Add(new
SqlParameter("@timeof",SqlDbType.Int,4));
sqlInsertCommand1.Parameters["@timeof"].Value = 4;
sqlInsertCommand1.Parameters.Add(new
SqlParameter("@isamof",SqlDbType.Bit,1));
sqlInsertCommand1.Parameters["@isamof"].Value = 0;
sqlInsertCommand1.Parameters.Add(new
SqlParameter("@pur",SqlDbType.NVarChar,500));
sqlInsertCommand1.Parameters["@pur"].Value = "to hell with this";



try{
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();
sqlConnection1.Close();

MessageBox.Show("The data was successfully entered into the
database","Success",MessageBoxButtons.OK);

}
catch (SqlException ee)
{
foreach(SqlError err in ee.Errors)
{
MessageBox.Show("SQL Error " + err.Number + " : " + err.Message);
}
}
finally
{
sqlConnection1.Close();
}

}


I have added test values for the parameters in order to figure out what was
going on, but to no avail. If anyone could point me in the right directon,
I would greatly appreciate it. I have searched this discussion board, but
all the previous suggestions have not worked! Thanks.
 
Back
Top