mamad
mamad

Reputation: 1

SQL Insert Into Syntax Error using C# 2013

string Connstr = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Shahrivar92\Documents\Safa_co.accdb");
OleDbConnection oleconn = new OleDbConnection(Connstr);
OleDbCommand olecmd = new OleDbCommand();
olecmd.Connection = oleconn;         
olecmd.CommandText = string.Format("insert into info-ghrardad(gh-note,gh-tarikh,gh-mablagh,gh-shomareh,gh-modat,today-date)values(@gh-note,@gh-tarikh,@gh-mablagh,@gh-shomareh,@gh-modat,@today-date)");

oleconn.Open();
olecmd.Parameters.AddWithValue("@gh-note", Gh_name.Text);
olecmd.Parameters.AddWithValue("@gh-tarikh", Gh_date.Text);
olecmd.Parameters.AddWithValue("@gh-mablagh", Gh_mablagh.Text);
olecmd.Parameters.AddWithValue("@gh-shomareh", Gh_number.Text);
olecmd.Parameters.AddWithValue("@gh-modat", Gh_modat.Text);
olecmd.Parameters.AddWithValue("@today-date", TodayDate.Text);
olecmd.ExecuteNonQuery();          
oleconn.Close();
System.Data.OleDb.OleDbException was unhandled
  HResult=-2147217900
  Message=Syntax error in INSERT INTO statement.

Upvotes: 0

Views: 166

Answers (1)

DRapp
DRapp

Reputation: 48179

Aside from marc_s comment about named parameters and using "?" as place-holder, the table an column names having "-" as the name is IMO a really bad naming convention. It is like you are telling the system to subtract two values.

That said, it may be that you need to both change the insert values as "?" place holders and add them in same sequence (which you already have), but also may need to wrap your column names within brackets, tick marks, whatever, such as

insert into [info-ghrardad] (
   [gh-note], [gh-tarikh], [gh-mablagh], [gh-shomareh], [gh-modat], [today-date] )
   values( ?, ?, ?, ?, ?, ?)");

then your parameters...

Additionally your date field might be part of the crash if you are putting in as just text instead of an actual date or date/time data type value if that is what the actual structure has. Similarly if amounts should be numeric and you are grabbing simple text, make sure it is in proper expected format for the columns.

Upvotes: 2

Related Questions