Why can't I add items to the database?

C# database Question?

  • this code is written in C# and the database is microsoft access..... cb= new OleDbCommandBuilder(daSub); DataRow drow = ds.Tables[uname].NewRow(); for (int ctr = 0; ctr < Subj_enroll.Items.Count; ctr++) { String Subj = Subj_enroll.Items[ctr].Text.ToString(); String Section = Subj_enroll.Items[ctr].SubItems[1].Text.... String Units = Subj_enroll.Items[ctr].SubItems[2].Text.... String Day = Subj_enroll.Items[ctr].SubItems[3].Text.... String Time = Subj_enroll.Items[ctr].SubItems[4].Text.... drow["Subject"]=Subj; drow["Units"] = Section; drow["Section"] = Units; drow["Day"] = Day; drow["Time"] = Time; ds.Tables[uname].Rows.Add(drow); daSub.Update(ds, uname); ds.Tables[uname].AcceptChanges(); } MessageBox.Show("Saved!", "Database MSG"); } ..............From listview I put it to string and then to datarow and then insert it to the database.... ...............I get the error "Syntax Error in insert into statement"......... .......hate this error!........ P.S. I'm have checked the variables and they're not null or blank.... 7 minutes ago - 4 days left to answer. Additional Details Tried this too also didn't work: sql = "INSERT INTO " + uname + " (Subject, Units, Section, Day, Time) VALUES (" + Subj + "," + Section + "," + Units + "," + Day + "," + Time + ")"; comm = new OleDbCommand(sql, conn); comm.ExecuteNonQuery();

  • Answer:

    your values are all strings so you need to enclose them in single quotes: sql = "INSERT INTO " + uname + " (Subject, Units, Section, Day, Time) VALUES ('" + Subj + "','" + Section + "','" + Units + "','" + Day + "','" + Time + "')"; copy/paste that - hope it works..should work

Healthmaker E at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

your values are all strings so you need to enclose them in single quotes: sql = "INSERT INTO " + uname + " (Subject, Units, Section, Day, Time) VALUES ('" + Subj + "','" + Section + "','" + Units + "','" + Day + "','" + Time + "')"; copy/paste that - hope it works..should work

Anthony

I'm no C# coder, actually I'm a VB coder. But for what I knew, "Syntax Error in insert into statement" means there is an error in your insert statement. What I can suggest is to debug the program, get the insert query, then try to run the query directly to the access database. Or, you can simply put the query here.

4n7JuK

I am not use but this C# tutorial may help you:

Sovandara

I'm no C# coder, actually I'm a VB coder. But for what I knew, "Syntax Error in insert into statement" means there is an error in your insert statement. What I can suggest is to debug the program, get the insert query, then try to run the query directly to the access database. Or, you can simply put the query here.

4n7JuK

I am not use but this C# tutorial may help you:

Sovandara

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.