Reputation: 117
I'm trying to insert new values into my database using visual studio and here is my code:
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = dbconnectionstring;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Data_ConfigInfo (TypeID, Name, ValidFromDate, ValidToDate, ValidFromSOP, ValidToSOP, Description)" +
"VALUES(@TypeID, @Name, @ValidFromDateTime, @ValidToDateTime, @ValidFromSOP, @ValidToSOP, @Description)";
cmd.Parameters.AddWithValue("TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("Name", Logg.Name);
if(Logg.ValidFrom.Equals(null)) {
cmd.Parameters.AddWithValue("@ValidFromDateTime", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@ValidFromDateTime", Logg.ValidFrom);
}
if (Logg.ValidTo.Equals(null))
{
cmd.Parameters.AddWithValue("@ValidToDateTime", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("@ValidToDateTime", Logg.ValidFrom);
}
cmd.Parameters.AddWithValue("@ValidFromSOP", Logg.ValidFromSOP);
cmd.Parameters.AddWithValue("@ValidToSOP", Logg.ValidToSOP);
cmd.Parameters.AddWithValue("@Description", Logg.Description);
cmd.ExecuteNonQuery();
conn.close();
conn.dispose();
But at the line
cmd.ExecuteNonQuery();
I get the error
invalid column name for ValidToDateTime and ValidFromDateTime.
Both are datetime variables. I can't seem to find the error. Any ideas?
This is how my datatable looks
Upvotes: 1
Views: 3092
Reputation: 2280
Did you forgot '@'?
cmd.Parameters.AddWithValue("TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("Name", Logg.Name);
cmd.Parameters.AddWithValue("@TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("@Name", Logg.Name);
Upvotes: 1
Reputation: 10843
Your DB table has columns ValidFromDateTime, ValidToDateTime
, but your column list in the INSERT
statement has ValidFromDate, ValidToDate
. Are you sure that it is not a typo when posting here?
Upvotes: 5