C# Syntax Error - INSERT INTO statement

  • Thread starter Thread starter Brandelis
  • Start date Start date
B

Brandelis

Guest
Im using a Access Database and when I try to insert data from my form into the database I get the Syntax Error with the INSERT INTO statement, ive tried numerous things like adding brackets around the field names but nothing seems to work. Hopefully someone can see something I have overlooked.


/// <summary>
/// sql to insert a patients details
/// </summary>
public static readonly string SqlInsertPatient = "Insert Into" +
" Patients(MRN, Given_Names, Surname, Address, Town, Postcode, DOB, Tests, Gender, Doctor_Provider, Medication, Medicare, Copy_To, Request_Date, Visit)" +
" Values(@MRN, @Given_Names, @Surname, @Address, @Town, @Postcode, @DOB, @Tests, @Gender, @Doctor_Provider, @Medication, @Medicare, @Copy_To, @Request_Date, @Visit)";




/// <summary>
/// Method to add new patient
/// </summary>
/// <param name="patient">patient model</param>
/// <returns>true or false</returns>
public bool AddPatient(PatientsModel Patient)
{
using (OleDbCommand oleDbCommand = new OleDbCommand())
{
// Set the command object properties
oleDbCommand.Connection = new OleDbConnection(this.ConnectionString);
oleDbCommand.CommandType = CommandType.Text;
oleDbCommand.CommandText = Scripts.SqlInsertPatient;

// Add the input parameters to the parameter collection
oleDbCommand.Parameters.AddWithValue("@MRN", Patient.MRN);
oleDbCommand.Parameters.AddWithValue("@Given_Names", Patient.GivenNames);
oleDbCommand.Parameters.AddWithValue("@Surname", Patient.Surname);
oleDbCommand.Parameters.AddWithValue("@Address", Patient.Address);
oleDbCommand.Parameters.AddWithValue("@Town", Patient.Town);
oleDbCommand.Parameters.AddWithValue("@Postcode", Patient.Postcode);
oleDbCommand.Parameters.AddWithValue("@DOB", Patient.DOB.ToShortDateString());
oleDbCommand.Parameters.AddWithValue("@Tests", Patient.Tests);
oleDbCommand.Parameters.AddWithValue("@Gender", (int)Patient.Gender);
oleDbCommand.Parameters.AddWithValue("@Doctor_Provider", Patient.DoctorProvider);
oleDbCommand.Parameters.AddWithValue("@Medication", Patient.Medication);
oleDbCommand.Parameters.AddWithValue("@Medicare", Patient.Medicare);
oleDbCommand.Parameters.AddWithValue("@Copy_To", Patient.CopyTo);
oleDbCommand.Parameters.AddWithValue("@Request_Date", Patient.RequestDate.ToShortDateString());
oleDbCommand.Parameters.AddWithValue("@Visit", Patient.Visit);


// Open the connection, execute the query and close the connection
oleDbCommand.Connection.Open();
var rowsAffected = oleDbCommand.ExecuteNonQuery();
oleDbCommand.Connection.Close();

return rowsAffected > 0;
}
}


//
// btnSave
//
this.btnSave.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnSave.Location = new System.Drawing.Point(623, 114);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(120, 43);
this.btnSave.TabIndex = 28;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.Register_Click);






Thanks, P.S Sorry if this is the from forum I couldnt find any other suitable one.

Continue reading...
 
Back
Top