0

what is the problem of my code...when i view it in browser and click the button insert there is an error message
saying "Syntax error in INSERT INTO Statement

Dim myConn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim sqlString, BookingDate, CheckInDate, CheckOutDate, RoomNo As String

        BookingDate = txtBDate.Text
        CheckInDate = txtCIDate.Text
        CheckOutDate = txtCODate.Text
        RoomNo = txtRNo.Text

        myConn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\OHRS.accdb")

        myConn.Open()
        sqlString = "INSERT INTO tbl_Booking (Booking Date, Check In Date, Check Out Date, Room No) VALUES ('" + BookingDate + "','" + CheckInDate + "','" + CheckOutDate + "','" + RoomNo + "')"
        cmd = New OleDbCommand(sqlString, myConn)
        cmd.ExecuteNonQuery()
        myConn.Close()

i hope u can help me...

Edited by nosajbiboy.talan

Attachments Untitled.png 76.86 KB
5
Contributors
6
Replies
8
Views
4 Years
Discussion Span
Last Post by MohdDilshad
0

The first thing that sticks out for me is the fact that your fields have spaces in the name. Therefore, you need to wrap the field names within brackets. For example...

INSERT INTO tbl_Booking ([Booking Date], [Check In Date], [Check Out Date],

0

Maybe that:
"INSERT INTO User ([UserID], [Forename], [Surname], [DateOfBirth], [TargetWeight], [TargetCalories], [Height]) Values ('" + userid.Text.ToString() + "' , '" + fname.Text.ToString() + "' , '" + sname.Text.ToString() + "' , '" + dob.Text.ToString() + "' , '" + tarweight.Text.ToString() + "' , '" + tarcal.Text.ToString() + "' , '" + height.Text.ToString() + "')";

Edited by jangirke

0

tnx...i think it works...but when i run it there is a new error message saying
Data type mismatch in criteria expression.

0

the data you are tyring to insert needs to match the database fields' data types. For example...trying to insert a string ('ABC') into a integer field.

0

Use parameterized SQL statements.

sqlString ="INSERT INTO tbl_Booking ([Booking Date],[Check In Date],[Check Out Date],[Room No]) VALUES (@BookingDate,@InDate,@OutDate,@RoomNo)";

using(OleDbConnection cn=new OleDbConnection())
{
using(OleDbCommand cmd=new OleDbCommand())
 {
   cn.ConnectionString=@"connection_string_here";
   cmd.Connection=cn;
   cmd.CommandText=sqlString;
   cmd.Parameters.AddWithValue("@BookingDate",BookingDate);
   ...
   cn.Open();
   cmd.ExecuteNonQuery();
   cn.Close();
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.