Hamoudy
Hamoudy

Reputation: 579

SQL insert syntax, database not updating

EDIT

i changed the code so i am using:[" + Time1 + "] instead of the parameter. This works for the first time, but when the time increases by 0.5, it stays false. The for loop is working as i tried a MessageBox.Show("" + Time1 + ""); inside the for loop.

for (double Time = time_began_5; Time < time_finished_5; Time = Time + 0.5)
        {
            string Time1 = Time.ToString("0.00");


            try
            {
                SqlConnection cn = new SqlConnection("Data Source=.\\SqlExpress;Initial Catalog=AllensCroft;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework;");

                cn.Open();
                SqlCommand Command = new SqlCommand("INSERT INTO Slots ([Date],[RoomID],[" + Time1 + "]) Values (@date,@room,1)", cn);
                Command.Parameters.AddWithValue("date", date);
                Command.Parameters.AddWithValue("room", rooms_combo.SelectedValue);


                Command.ExecuteNonQuery();


                try
                {
                    cn.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

Upvotes: 0

Views: 329

Answers (3)

Jack Pettinger
Jack Pettinger

Reputation: 2755

  1. You have a @ before your 3rd column (time).
  2. When you add a parameter you need to add the @.
  3. On your insert statement you are trying to insert true as a boolean into the time column.
SqlCommand Command = new SqlCommand("INSERT INTO Slots (Date,RoomID,time) " + "Values (@date,@room,@time)", cn);

Command.Parameters.AddWithValue("@date", date);
Command.Parameters.AddWithValue("@room", rooms_combo.SelectedValue);
Command.Parameters.AddWithValue("@time", Time);

EDIT After comments. Try this:

SqlCommand Command = new SqlCommand("INSERT INTO Slots (Date,RoomID,[" + Time1 + "]) " + "Values (@date,@room,@time)", cn);

Command.Parameters.AddWithValue("@date", date);
Command.Parameters.AddWithValue("@room", rooms_combo.SelectedValue);
Command.Parameters.AddWithValue("@time", true);

Upvotes: 2

MattW
MattW

Reputation: 4628

You can't include a variable in your column list, I presume you wanted that @Time to be in the value list about where that true is.

EDIT: To inject the time as a column name, do some C# string manipulation:

SqlCommand Command = new SqlCommand("INSERT INTO Slots (Date, RoomID, [" + time + "]) Values (@date, @room, 1)", cn);

EDIT again: true is not a T-SQL keyword, T-SQL bit columns have values 1 or 0

Now you don't want to add time as a parameter to the query.

Upvotes: 0

Chains
Chains

Reputation: 13157

You're listing a variable as a column name (@time)

EDIT
Right here:

INSERT INTO Slots (Date, RoomID, --->>> @time <<<--- DANGER WILL ROBINSON, DANGER

To fix it, you need to either change it to a column name from your table, or else get rid of it.

So something like this, for example:

INSERT INTO Slots (Date, RoomID, time)

Upvotes: 0

Related Questions