0

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());
            }

        }
7
  • What is the SQL error? Commented Mar 13, 2013 at 19:37
  • First of all, is the loop even executing? P.S. It's generally considered bad practice to use a Double as an indexer variable in a for loop! Commented Mar 13, 2013 at 19:38
  • is @time your column name if not then why did you use there? Commented Mar 13, 2013 at 19:39
  • Time is a column and a variable and i wanted it to be true, is that possible Commented Mar 13, 2013 at 19:41
  • @JackPettinger It is just the database not updating Commented Mar 13, 2013 at 19:42

3 Answers 3

2
  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);
Sign up to request clarification or add additional context in comments.

4 Comments

Time is a column and it is boolean and i wanted it to be true, is that possible, e.g. the column 8.5, i wanted that to be true
If you want to insert true into the time column, then the SQL datatype needs to be a bit. Is this is the case then there is no need to the @time parameter, what is it for?
it is just to reference the time column, e.g.8.50, 8.00
Are you saying you have different columns for different times? ie, a column for 8.00, 8.15, 8.30 etc.
0

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)

Comments

0

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.

6 Comments

Time is a column and a variable and i wanted it to be true, is that possible, e.g. the column 8.5, i wanted that to be true
@Hamoudy Answer updated with what I think you you're asking for
i thied this, but same problem SqlCommand Command = new SqlCommand("INSERT INTO Slots (Date,RoomID,8.00) Values (@date,@room,true)", cn);
@Hamoudy The square brackets I included are vital, 8.00 requires escaping to be used as a column name. Also, I just updated the answer to reflect that you don't use true as a value in SQL Server, you use 1.
Also, what you're trying to do with those time columns isn't really in line with what relational database theory suggests. You're breaking the third normal form because your time columns aren't independent.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.