-1

It runs, but when I click the update button it says 'Syntax error in UPDATE statement.' in the cmd.ExecuteNonQuery();

I tried re-arranging and even checking the spelling but everything is correct, So I really don't know now what to do, I'll put my codes here. There is no error but when you run and click the button in errors

string Update = "UPDATE StudInformation SET FMonthly123=@FMonthly123,FPrem123=@FPrem123,SPrem123=@SPrem123,Midterm123=@Midterm123,Pre_Finals123=@Pre_Finals123,Finals123=@Finals123,GwaRaw123=@GwaRaw123,GwaNum123=@GwaNum123,Remarks123=@Remarks123,Status123=@Status123, WHERE StudNum123=@StudNum123,Course123=@Course123,Semester123=@Semester123,SchoolYr123=@SchoolYr123,YearLvl123=@YearLvl123,SubjectCode123=@SubjectCode123,SubDesc123=@SubDesc123,Units123=@Units123,Date_Of_Registry123=@DoR,Time_Of_Registry123=@ToR)";
          
using (cmd = new OleDbCommand(Update, con))
{
               
    cmd.Parameters.AddWithValue("@StudNum123", TxtStudNo.Text);
    cmd.Parameters.AddWithValue("@Course123", CbCourse.Text);
    cmd.Parameters.AddWithValue("@Semester123", CbSem.Text);
    cmd.Parameters.AddWithValue("@SchoolYr123", CbSchYr.Text);
    cmd.Parameters.AddWithValue("@YearLvl123", CbYrLvl.Text);
    cmd.Parameters.AddWithValue("@SubjectCode123", CbSubCode.Text);
    cmd.Parameters.AddWithValue("@SubDesc123", TxtSubDesc.Text);
    cmd.Parameters.AddWithValue("@Unit123", TxtUnit.Text);
    cmd.Parameters.AddWithValue("@DoR", lblDate.Text);
    cmd.Parameters.AddWithValue("@ToR", lblTime.Text);
    cmd.Parameters.AddWithValue("@FMonthly123", FirstmBox.Text);
    cmd.Parameters.AddWithValue("@FPrem123", FirstpBox.Text);
    cmd.Parameters.AddWithValue("@SPrem123", SecondpBox.Text);
    cmd.Parameters.AddWithValue("@Midterm123", MidtermBox.Text);
    cmd.Parameters.AddWithValue("@Pre_Finals123", PrefinBox.Text);
    cmd.Parameters.AddWithValue("@Finals123", FinalsBox.Text);
    cmd.Parameters.AddWithValue("@GwaRaw123", GwarawBox.Text);
    cmd.Parameters.AddWithValue("@GwaNum123", GwanumBox.Text);
    cmd.Parameters.AddWithValue("@Remarks123", RemarkBox.Text);
    cmd.Parameters.AddWithValue("@Status123", StatusBox.Text);
               
    con.Open();
    cmd.ExecuteNonQuery();              
    con.Close();
}
}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • You're going to need to provide more details about the error. We know nothing about the structure of your tables, and without that information we can't help. You're setting some unknown variables using text values, and assigning them to some unknown columns in your table. – Ken White May 17 '23 at 03:57
  • 4
    The parameter binding in MS Access using OleDB is **NOT** by name (`@StudNum123`) - it's by position. So you need **define and fill them** in the **same order** as they appear in the SQL statement ! Right now, you're not doing that at all ..... – marc_s May 17 '23 at 04:04
  • 3
    You should check out [Can we stop using AddWithValue() already?](http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/) and stop using `.AddWithValue()` - it can lead to unexpected and surprising results... – marc_s May 17 '23 at 04:05
  • The following may be of interest: https://stackoverflow.com/a/69638011/10024425 and [OleDbParameter](https://learn.microsoft.com/en-us/dotnet/api/system.data.oledb.oledbparameter?view=dotnet-plat-ext-7.0#remarks). – Tu deschizi eu inchid May 17 '23 at 04:07
  • There is an extra comma before the `WHERE` – Klaus Gütter May 17 '23 at 04:23
  • You also should never reuse connections; it should be in a `using` block as `cmd` is. – Ňɏssa Pøngjǣrdenlarp May 17 '23 at 04:38

1 Answers1

-1

If you are using access try to replace the ',' with 'AND' after the WHERE.

string Update = "UPDATE StudInformation SET FMonthly123=@FMonthly123 AND FPrem123=@FPrem123 AND SPrem123=@SPrem123 AND Midterm123=@Midterm123 AND Pre_Finals123=@Pre_Finals123 AND Finals123=@Finals123 AND GwaRaw123=@GwaRaw123 AND GwaNum123=@GwaNum123 AND Remarks123=@Remarks123 AND Status123=@Status123 AND  WHERE StudNum123=@StudNum123 AND Course123=@Course123 AND Semester123=@Semester123 AND SchoolYr123=@SchoolYr123 AND YearLvl123=@YearLvl123 AND SubjectCode123=@SubjectCode123 AND SubDesc123=@SubDesc123 AND Units123=@Units123 AND Date_Of_Registry123=@DoR AND Time_Of_Registry123=@ToR)";

Tip: Implement some Throw to have a better input about your error here. and share that error in the page that will give us some more details about the issue