I'm using VC++ (Visual C++) in my system. I'm doing to add student's information in a Access Database which I used SQL Command (INSERT INTO).
this is my codes to add student's information in access database
OleDb::OleDbConnection^ con = gcnew OleDb::OleDbConnection();
OleDb::OleDbCommand^ command = gcnew OleDb::OleDbCommand();
con->ConnectionString::set(conStr);
con->Open();
String^ cmdTxt = "
insert into students(studno, fname, lname,
mname, gender, status, birthday, course, shift, section, homeadd,
provadd, contactnum, nameguard, addguard, contguard, sec1, sub1,
room1, unit1, guro1, sec2, sub2, room2, unit2, guro2, sec3, sub3,
room3, unit3, guro3, sec4, sub4, room4, unit4, guro4, sec5, sub5,
room5, unit5, guro5, sec6, sub6, room6, unit6, guro6, sec7, sub7,
room7, unit7, guro7, sec8, sub8, room8, unit8, guro8, sec9, sub9,
room9, unit9, guro9, sec10, sub10, room10, unit10, guro10)
values('" + studno + "', '" + fname + "', '" + lname + "', '" + mname + "',
'" + sex + "', '" + status + "', '" + birthday + "', '" + course + "',
'" + shift + "', '" + section + "', '" + homeAdd + "', '" + provAdd + "',
'" + contactnum + "', '" + nameGuardian + "', '" + addGuardian + "',
'" + numGuardian + "', '" + se1 + "', '" + sub1 + "', '" + room1 + "',
'" + unit1 + "', '" + guro1 + "', '" + se2 + "', '" + sub2 + "',
'" + room2 + "', '" + unit2 + "', '" + guro2 + "', '" + se3 + "',
'" + sub3 + "', '" + room3 + "', '" + unit3 + "', '" + guro3 + "',
'" + se4 + "', '" + sub4 + "','" + room4 + "', '" + unit4 + "',
'" + guro4 + "', '" + se5 + "', '" + sub5 + "', '" + room5 + "',
'" + unit5 + "', '" + guro5 + "', '" + se6 + "', '" + sub6 + "',
'" + room6 + "', '" + unit6 + "', '" + guro6 + "', '" + se7 + "',
'" + sub7 + "', '" + room7 + "', '" + unit7 + "', '" + guro7 + "',
'" + se8 + "', '" + sub8 + "', '" + room8 + "', '" + unit8 + "',
'" + guro8 + "', '" + se9 + "', '" + sub9 + "', '" + room9 + "',
'" + unit9 + "', '" + guro9 + "', '" + se10 + "', '" + sub10 + "',
'" + room1 + "', '" + unit10 + "', '" + guro10 + "')";
command->Connection::set(con);
command->CommandText::set(cmdTxt);
command->ExecuteNonQuery();
con->Close();
No errors found when i compiled and run it, the problems is when i add student's information shows an error message
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement.
i checked many times in INSERT INTO, so i changed it
command->CommandText::set("insert into students("
+ "studno,"
+ "fname,"
+ "lname,"
+ "mname,"
+ "gender,"
+ "status,"
+ "birthday,"
+ "course,"
+ "shift)"
+ "values("
+ "'" + studno + "', '"
+ fname + "', '"
+ lname + "', '"
+ mname + "', '"
+ sex + "', '"
+ status + "', '"
+ birthday + "', '"
+ course + "', '"
+ shift + "')");
This code is works 100%, so I add fields,
command->CommandText::set("insert into students("
+ "studno,"
+ "fname,"
+ "lname,"
+ "mname,"
+ "gender,"
+ "status,"
+ "birthday,"
+ "course,"
+ "shift,"
+ "section)"
+ "values("
+ "'" + studno + "', '"
+ fname + "', '"
+ lname + "', '"
+ mname + "', '"
+ sex + "', '"
+ status + "', '"
+ birthday + "', '"
+ course + "', '"
+ shift + "', '"
+ section + "')");
so it gives me an error. -_-
so how to solve this problem
SECTION is a reserved word. If you surround that name with square brackets, Access will understand it is a field name. So change "section)" to "[section])"
So I have a query with the following sort order:
String sortOrder = "CASE " + ICCGoal.COLUMNS.category
+ " WHEN '" + GoalCategory.PHYSICAL_ACTIVITY.getEncodedValue() + "' THEN 1"
+ " WHEN '" + GoalCategory.SLEEP.getEncodedValue() + "' THEN 2"
+ " WHEN '" + GoalCategory.SOCIAL.getEncodedValue() + "' THEN 3"
+ " WHEN '" + GoalCategory.MOOD.getEncodedValue() + "' THEN 4"
+ " WHEN '" + GoalCategory.ENERGY.getEncodedValue() + "' THEN 5"
+ " END";
is there anyway to order the sections of the returned values?
Currently, I have all goals of "SLEEP" type returned after the "PHYSICAL_ACTIVITY" and before "SOCIAL" goals, in whatever order they are stored.
Would it be possible to return them ordered by date created? Or just ordered in another way than default?
Just add a secondary sorting criteria:
String sortOrder = "CASE " + ICCGoal.COLUMNS.category
+ " WHEN '" + GoalCategory.PHYSICAL_ACTIVITY.getEncodedValue() + "' THEN 1"
+ " WHEN '" + GoalCategory.SLEEP.getEncodedValue() + "' THEN 2"
+ " WHEN '" + GoalCategory.SOCIAL.getEncodedValue() + "' THEN 3"
+ " WHEN '" + GoalCategory.MOOD.getEncodedValue() + "' THEN 4"
+ " WHEN '" + GoalCategory.ENERGY.getEncodedValue() + "' THEN 5"
+ " END" // Original ordering from the OP
+ ", date_created"; // Secondary term
I have the following SQL statement which is giving me this error.
THIS IS NOT MY CODE!
"Error converting data type varchar to float" But i can't find the error.
I thnk the error is coming from this code:
birthday = (monthComboBox.SelectedItem) + "-" + (dayComboBox.SelectedIndex + 1) + "-" + yearTextBox.Text;
Int32 getIDBack = 0;
string query = "insert into reservation(first_name, last_name, birth_day, gender, phone_number, email_address, number_guest, street_address, apt_suite,city, state, zip_code, room_type, room_floor, room_number, total_bill,payment_type, card_type, card_number,card_exp,card_cvc, arrival_time, leaving_time, check_in, break_fast, lunch, dinner, supply_status, cleaning, towel, s_surprise, food_bill) values('" + firstNameTextBox.Text +
"', '" + lastNameTextBox.Text + "', '" + birthday + "', '" + genderComboBox.SelectedItem + "', '" + phoneNumberTextBox.Text + "', '" + emailTextBox.Text +
"', '" + (qtGuestComboBox.SelectedIndex + 1) + "', '" + addLabel.Text + "', '" + aptTextBox.Text + "', '" + cityTextBox.Text +
"', , '" + zipComboBox.Text + "', '" + roomTypeComboBox.SelectedItem + "', '" + floorComboBox.SelectedItem +
"', '" + roomNComboBox.SelectedItem + "', '" + finalizedTotalAmount + "', '" + paymentType +
"', '" + CardType + "','" + paymentCardNumber + "','" + MM_YY_Of_Card + "','" + CVC_Of_Card + "', '" + dateTimePicker1.Text + "', '" + dateTimePicker2.Text + "','" + checkin +
"', '" + breakfast + "','" + lunch + "','" + dinner + "', '" + foodStatus + "', '" + Convert.ToInt32(cleaning) + "', '" + Convert.ToInt32(towel) + "', '" + Convert.ToInt32(surprise) + "','" + foodBill + "');";
query += "SELECT CAST(scope_identity() AS int)";
SqlConnection connection = new SqlConnection(Hotel_Manager.Properties.Settings.Default.frontend_reservationConnectionString);
SqlCommand query_table = new SqlCommand(query, connection);
try
{
connection.Open();
getIDBack = (Int32)query_table.ExecuteScalar();
string userID = Convert.ToString(getIDBack);
SendSMS(getIDBack);
MetroFramework.MetroMessageBox.Show(this, "Entry successfully inserted into database. " + "\n\n" +
"Provide this unique ID to the customer." + "\n\n" +
"USER UNIQUE ID: " + userID, "Report", MessageBoxButtons.OK, MessageBoxIcon.Question);
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
ComboBoxItemsFromDataBase();
LoadForDataGridView();
reset_frontend();
GetOccupiedRoom();
ReservedRoom();
getChecked();
}
This generally means that one (or more) of the values is a number, rather than a string. If any of the values in a + expression is numeric, then the + is interpreted as addition rather than string concatenation.
One method is to find the offending values (such as finalizedTotalAmount) and then wrap them in conversions: cast(finalizedTotalAmount as varchar(255)).
A better way is to use parameterized queries, where the values are passed in as typed parameters. This is also safer because it prevents SQL injection.