0

I'm trying to finish a college project that requires a program to interact with a database.

Some of my naming is a little odd, but don't worry!

I'm trying to use a single submit button to either update or insert to the database.

Main issue is that I can't get an update to work though when I changed my code to try and fix it, I made it worse. Here is what I currently have.

private void btn_submit_Click(object sender, EventArgs e)
{
   using (SqlCeConnection con = new SqlCeConnection(@"Data Source=G:\Dropbox\HND\Visual Studio\Visual C#\TestForms\TestForms\Database1.sdf"))
   {
      con.Open();
      string taskSel = "SELECT TaskCode FROM TaskCode;";
      SqlCeCommand c1 = new SqlCeCommand(taskSel, con);
      SqlCeDataReader reader;
      reader = c1.ExecuteReader();

      if (reader.Read())
      {
         try
         {
            string taskUpdate = "UPDATE TaskCode SET TaskCode = @TaskCode, TaskDescription = @TaskDescription = WHERE TaskCode = @TaskCode;";
            SqlCeCommand c = new SqlCeCommand(taskUpdate, con);
            c.Parameters.AddWithValue("@TaskCode", cbx_taskCode.Text);
            c.Parameters.AddWithValue("@TaskDescription", txt_desc.Text);
            c.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Record has been updated");
            MainMenu.Current.Show();
            this.Close();
         }
         catch (SqlCeException exp)
         {
            MessageBox.Show(exp.ToString());
         }
      }
      else
      {
         try
         {
            string taskInsert = "INSERT INTO TaskCode VALUES (@TaskCode, @TaskDescription);";
            SqlCeCommand c = new SqlCeCommand(taskInsert, con);
            c.Parameters.AddWithValue("@TaskCode", cbx_taskCode.Text);
            c.Parameters.AddWithValue("@TaskDescription", txt_desc.Text);
            c.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Record has been added");
            MainMenu.Current.Show();
            this.Close();
         }
         catch (SqlCeException exp)
         {
            MessageBox.Show(exp.ToString());
         }
      }
   }
}

Has anyone got any ideas why I am getting an error on the c.ExecuteQuery line?

If I remove said line, it will not throw an exception, but it will not update the database.

Thanks

2
  • I do not see c.ExecuteQuery line, do you mean, c.ExecuteNonQuery()? Remove the = before the WHERE in your UPDATE statement. Commented Jun 3, 2013 at 16:02
  • WHAT error are you getting? Please post the complete and exact error message - thanks! Commented Jun 3, 2013 at 16:23

2 Answers 2

3

You have a simple syntax error in your update query just before the where statement.
There is an invalid equal sign

string taskUpdate = "UPDATE TaskCode SET TaskCode = @TaskCode, " + 
                    "TaskDescription = @TaskDescription " + 
                    "WHERE TaskCode = @TaskCode;";

Your query also could be simplified with

using (SqlCeConnection con = new SqlCeConnection(@"Data Source=G:\Dropbox\HND\Visual Studio\Visual C#\TestForms\TestForms\Database1.sdf"))
{
    con.Open();
    string taskSel = "SELECT COUNT(*) FROM TaskCode";
    string cmdText; 
    SqlCeCommand c1 = new SqlCeCommand(taskSel, con);
    int count = (int)c1.ExecuteScalar();
    if (count > 0)
    {
        // Here there is no point to update the TaskCode. You already know the value
        // Unless you have a different value, but then you need another parameter
        // the 'old' TaskCode.....
        cmdText = "UPDATE TaskCode SET " + 
                  "TaskDescription = @TaskDescription " + 
                  "WHERE TaskCode = @TaskCode;";
    }
    else
    {
        cmdText = "INSERT INTO TaskCode VALUES (@TaskCode, @TaskDescription);";
    }
    try
    {
        SqlCeCommand c = new SqlCeCommand(cmdText, con);
        c.Parameters.AddWithValue("@TaskCode", cbx_taskCode.Text);
        c.Parameters.AddWithValue("@TaskDescription", txt_desc.Text);
        c.ExecuteNonQuery();
        MessageBox.Show(count > 0 ? "Record has been updated" : "Record has been added");
        MainMenu.Current.Show();
        this.Close();
    }
    catch (SqlCeException exp)
    {
         MessageBox.Show(exp.ToString());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Not sure if it is the only problem, but you have an equal (=) sign before the WHERE keyword.

Comments

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.