VC++ SQL INSERT INTO error - sql
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])"
Related
How to convert from type Dbnull to type Date
If IsDBNull(dgv_datapasien.Rows(i).Cells(11).Value.ToString) Then tglcetak = dgv_datapasien.Rows(i).Cells(11).Value.ToString With dgv_datapasien.Rows(iRowIndex) cmdData.CommandText = "Update tabelpasien set no_lab ='" + .Cells(1).Value.ToString + "', no_rm ='" + .Cells(2).Value.ToString + "' , nama ='" + .Cells(3).Value.ToString + "',jeniskelamin ='" + .Cells(5).Value.ToString + "' , status ='" + .Cells(6).Value.ToString + "', tglmasuk ='" & FormatTglUniversal(.Cells(8).Value) & "' , ruangasal ='" + .Cells(7).Value.ToString + "' ,dokterpengirim='" + .Cells(9).Value.ToString + "' ,analis='" + .Cells(10).Value.ToString + "',umur ='" + .Cells(4).Value.ToString + "',analyzer='" + .Cells(0).Value.ToString + "' where no_lab ='" + .Cells(1).Value.ToString + "'" ' ,tglcetak='" & FormatDateNull(.Cells(11).Value) & "' End With cmdData.ExecuteNonQuery() End If I need to update database from datagridview by clicking button Update. One of the field is about datePrint. But, sometimes, my dateprint is dbnull. so I have problem to update database because datePrint is dbnull. my code always show "Conversion from type 'DbNull' to 'Date' type is not valid" fyi: I use postgresql Anyone know how to solve this?
You should check for the datePrint cell during building the SQL query and set the respective column (in your case tglcetak) to NULL if the source value is null, otherwise set it to the date value. Assuming your source grid cell(11) contains the datePrint value: With dgv_datapasien.Rows(iRowIndex) cmdData.CommandText = "Update tabelpasien set no_lab ='" + .Cells(1).Value.ToString + "', no_rm ='" + .Cells(2).Value.ToString + "', nama ='" + .Cells(3).Value.ToString + "', jeniskelamin ='" + .Cells(5).Value.ToString + "', status ='" + .Cells(6).Value.ToString + "', tglmasuk ='" & FormatTglUniversal(.Cells(8).Value) & "', ruangasal ='" + .Cells(7).Value.ToString + "', dokterpengirim='" + .Cells(9).Value.ToString + "', analis='" + .Cells(10).Value.ToString + "', umur ='" + .Cells(4).Value.ToString + "', analyzer='" + .Cells(0).Value.ToString + "'" If IsDBNull(.Cells(11).Value) Then cmdData.CommandText &= ",tglcetak=NULL" Else cmdData.CommandText &= ",tglcetak='" & FormatTglUniversal(.Cells(11).Value) & "'" End If cmdData.CommandText &= " where no_lab ='" + .Cells(1).Value.ToString + "'" End With cmdData.ExecuteNonQuery() I don't know what your FormatDateNull function returns, so I don't use it above. And I assume your function FormatTglUniversal formats the date string to postgresql date format. I would recommend you get used to using parameterized query for security and performance since your code here is inside a loop.
Make sure Date Field in database allows null while passing the parameter to database use below format input parameter of database = (dateprint == DBNull.value ? null : (DateTime?)convert.toDateTime(dateprint))
Conversion failed when converting date or time from character string
This is my insert statement to insert data into database. This is my query: INSERT INTO JobsDB (Job_Name, Job_Desc, Job_Categ, Job_Qualif, Job_PayType, Job_Payrate, Job_PloyerName, Job_StartDate, Job_EndDate, Job_Status) VALUES ('" + tbName.Text + "' , '" + tbDescription.Text + "' , '" + ddCategory.Text + "' , '" + ddQualifications.Text + "' , '" + ddPayType.Text + "' , '" + tbRate.Text + "' , 'Kaslana' , '" + calendarStart.SelectedDate + "' , '" + calendarEnd.SelectedDate + "' , 'Active') The error occurs at the point where the code takes the selected date from the calendar control and trys to insert it into the database.
Use Convert.ToDateTime(YourSelectedDate) INSERT INTO JobsDB (Job_Name, Job_Desc, Job_Categ, Job_Qualif, Job_PayType, Job_Payrate, Job_PloyerName, Job_StartDate, Job_EndDate, Job_Status) VALUES ('" + tbName.Text + "' , '" + tbDescription.Text + "' , '" + ddCategory.Text + "' , '" + ddQualifications.Text + "' , '" + ddPayType.Text + "' , '" + tbRate.Text + "' , 'Kaslana' , '" + Convert.ToDateTime(calendarStart.SelectedDate) + "' , '" +Convert.ToDateTime(calendarEnd.SelectedDate) + "' , 'Active')
Ordering subsections of case statement
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
Error converting data type varchar to float. C# VS
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.
VB.NET Incrementing Indexes
I am having trouble incrementing the indexes of my list item properties. Here is the code. Dim i As Integer = 0 For x As Integer = 1 To list.Count / 19 database.ExecuteCommand("INSERT INTO Contacts VALUES ('" + _ list.Item(i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "', '" + _ list.Item(++i) + "')") Next The ++i does not increment at all in the parameters. Thanks
you need to use your loop variable (x) and increase the index in that manner. I'm doing this in C# but I'm sure you will understand. string sql = "INSERT INTO Contact VALUES ('"; for(int i = 1; i < list.Count ; i++) { sql += list.Item(i) + "', '"; } sql = sql.Remove(sql.Length -1); sql += ")"; Database.ExecuteCommand(sql);
VB.Net does not have an increment operator.
Add this Function Function GetIncrementValue(ByRef x as Integer) as Integer x=x+1 Return x End function Your Code will be...... Dim i As Integer = 0 For x As Integer = 1 To list.Count / 19 database.ExecuteCommand("INSERT INTO Contacts VALUES ('" + _ list.Item(i) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "', '" + _ list.Item(GetIncrementValue(i)) + "')") Next Enjoy....