I am working on a Database app in c# that connects to a Microsoft Access database. Now I had it working where when I would click on Save it would say the data was entered successfully, but I found out that it actually wasn't entering the data into the Database.
So with help from the fine folks here at stackoverflow I was able to find out what I needed to do to get it to work, however I am now getting an unhandled exception saying the following:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error (missing operator) in query expression '@[Guest First Name]'.
I'm curious as to where the problem is. This exception is thrown when it reaches the com.ExecuteNonQuery(); line of the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using System.ComponentModel;
namespace ParkingDatabase
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
using (OleDbConnection DBConnect = new OleDbConnection())
{
DBConnect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\Users\bkoso\documents\visual studio 2015\Projects\ParkingDatabase\ParkingDatabase\ParkingData.accdb";
using (OleDbCommand com = new OleDbCommand("INSERT INTO [Guest Info]([Guest First Name], [Guest Last Name], [Room Number], [Departure Date], [Return Date], [Vehicle Colour], [Vehicle Make], [Plate Number], [Contact First Name], [Contact Last Name], [Contact Number], [Contact Email], [Tag Number]) Values(@[Guest First Name], @[Guest Last Name], @[Room Number], @[Departure Date], @[Return Date], @[Vehicle Colour], @[Vehicle Make], @[Plate Number], @[Contact First Name], @[Contact Last Name], @[Contact Email], @[Contact Email], @[Tag Number])", DBConnect))
//the section below was recently updated
{
com.Parameters.AddWithValue("@GuestFirstName", txtBxGstFName.Text);
com.Parameters.AddWithValue("@GuestLastName", txtBxGstLName.Text);
com.Parameters.AddWithValue("@RoomNumber", txtBxRm.Text);
com.Parameters.AddWithValue("@DepartureDate", txtBxDDate.Text);
com.Parameters.AddWithValue("@ReturnDate", txtBxRDate.Text);
com.Parameters.AddWithValue("@VehicleColour", txtBxVColour.Text);
com.Parameters.AddWithValue("@VehicleMake", txtBxVMake.Text);
com.Parameters.AddWithValue("@PlateNumber", txtBxPlate.Text);
com.Parameters.AddWithValue("@ContactFirstName", txtBxContactFName.Text);
com.Parameters.AddWithValue("@ContactLastName", txtBxContactLName.Text);
com.Parameters.AddWithValue("@ContactNumber", txtBxPhone.Text);
com.Parameters.AddWithValue("@ContactEmail", txtBxEmail.Text);
com.Parameters.AddWithValue("@TagNumber", txtBxTag.Text);
DBConnect.Open();
com.ExecuteNonQuery();
DBConnect.Close();
}
if (DBConnect.State == ConnectionState.Open)
{
//com.ExecuteNonQuery();
MessageBox.Show("Guest Information Saved Successfully");
txtBxGstFName.Text = "";
txtBxGstLName.Text = "";
txtBxRm.Text = "";
txtBxDDate.Text = "";
txtBxRDate.Text = "";
txtBxVColour.Text = "";
txtBxVMake.Text = "";
txtBxPlate.Text = "";
txtBxContactFName.Text = "";
txtBxContactLName.Text = "";
txtBxPhone.Text = "";
txtBxEmail.Text = "";
txtBxTag.Text = "";
}
}
}
private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtBxGstFName.Text = "";
txtBxGstLName.Text = "";
txtBxRm.Text = "";
txtBxDDate.Text = "";
txtBxRDate.Text = "";
txtBxVColour.Text = "";
txtBxVMake.Text = "";
txtBxPlate.Text = "";
txtBxContactFName.Text = "";
txtBxContactLName.Text = "";
txtBxPhone.Text = "";
txtBxEmail.Text = "";
txtBxTag.Text = "";
}
private void btnView_Click(object sender, RoutedEventArgs e)
{
}
private void btnSame_Click(object sender, RoutedEventArgs e)
{
}
private void txtBoxGuestFirstName_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
}
}
}