Note that there are some explanatory texts on larger screens.

plurals
  1. POINSERT INTO command not working
    primarykey
    data
    text
    <p>Before I start, I'll let you know that I tried everything that has already been suggested on previous questions and other websites before I considered posting a question myself. As it happens, nothing seems to work and I'm just about fed up with this.</p> <p>As some background information, this is for my Computing A2 project, so I'm kind of stuck for time now - i.e. I can't be changing loads of my code ideally.</p> <p>Anyway, onto the issue... I'm using SQLCe in my code to read from various tables and write to one. So far, the code for reading from the tables works fine, so that's any connection issues out the way first. The piece of code I am struggling with is as follows:</p> <pre><code> string connectionString = Properties.Settings.Default.BookingSystemDatabaseConnectionString; using (SqlCeConnection myConnection = new SqlCeConnection(connectionString)) { myConnection.Open(); try { string commandStr = "INSERT INTO bookings(username, room, time) VALUES(@username, @room, @time)"; SqlCeCommand myCommand = new SqlCeCommand(commandStr); //Passes parameters into SQL command. myCommand.Parameters.AddWithValue("username", StaticUser.StudentUser.username); myCommand.Parameters.AddWithValue("room", roomBox.Text); myCommand.Parameters.AddWithValue("time", timeBox.Text); //Executes SQL command. Returns the number of affected rows (unecessary for my purposes; a bi-product if you will). myCommand.ExecuteNonQuery(); } catch { System.Windows.Forms.MessageBox.Show("Could not write new booking to database. This is likely because the database cannot be reached.", "Error"); Program.AccessError = true; } myConnection.Close(); } </code></pre> <p>This is just one of the many ways I have tried to combat the issue I am having. I have also explored:</p> <pre><code>myCommand.Parameters.Add(new SqlCeParameter("username", StaticUser.StudentUser.username)); </code></pre> <p>to pass the parameters...and another method which escapes me now (using ".Value = StaticUser.StudentUser.username" I think). Furthermore, I have tried using a 'using' statement for the command to save me closing the connection myself (I will probably end up using a solution that uses 'using'). Finally (albeit this isn't a chronological recollection), I tried:</p> <pre><code>SqlCeCommand myCommand = new SqlCeCommand("INSERT INTO bookings(username, room, time) VALUES(@username, @room, @time)", myConnection) </code></pre> <p>Again, of course, to no avail.</p> <p>To highlight the actual symptoms of the issue I am having: The code appears to run fine; stepping through the full method I have pasted above shows that no error is being caught (of course, the message box does not appear - I realised afterwards that stepping through was arguably an unnecessary procedure) and in the other methods I have touched on, the same thing happens. The issue, then, is that the table 'bookings' is not actually being updated.</p> <p>So, my question, why?</p> <hr> <p>I didn't do the obvious and check the Debug folder for an updated database.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload