i'm currently getting the following error when inserting a date into a database, but it works fine when amending the date via a update:
Conversion failed when converting date and/or time from character string
The code I have (Very crappy i know, learning on the fly...):
update - working
SqlCommand update = new SqlCommand("Update bookings set Guests = '" +
drptxtGuests.Text + "'," + "CheckInDate = '" +
BasicDatePicker1.SelectedDate + "'," + "CheckOutDate = '" +
BasicDatePicker2.SelectedDate + "'," + "RoomType = '" +
drptxtRoomType.Text + "'," + "Price = '" + txtBookingPrice.Text +
"' where BookingNumber = '" + txtBookingNumber.Text + "'" , con);
update.ExecuteNonQuery();
con.Close();
insert - error
SqlCommand insert = new SqlCommand("Insert into bookings(BookingNumber, MemberID, " +
"CheckInDate, CheckOutDate, Guests, RoomType, Price) values('" +
txtBookingNumber.Text + "','" + Session["id"] + "','" +
BasicDatePicker1.SelectedDate + "','" + BasicDatePicker2.SelectedDate +
"','" + drptxtGuests.Text + "','" + drptxtRoomType.Text + "','" +
txtBookingPrice.Text + "')", con);
insert.ExecuteNonQuery();
con.Close();
i wouldn't be surprised if it was something very basic, obvious and stupid but please bear with me. :)
edit: Im also certain that the problem is with the datepickers, i just dont understand why it works for updating and not inserting?
I'm trying to remake my system and my older system is 100% working, but when I changed something (I added a lot of columns in ms access database) I did the correct format in inserting data of each textboxes but it still says "error in insert into statement".
This is my code. Please take your time reading the query for that is only the error I got. I double checked the spelling or capitalization on each field from database as well as here.
try
{
connection.Open(); //open connection
OleDbCommand command = new OleDbCommand(); // command object , we can execute to validate our database
command.Connection = connection; // make a connection for the command
command.CommandText = " insert into StudentsRecord([StudentID],Name,Section,Semester,MathPrelim,MathMidterm,MathFinals,MathAverage,MathFinalGrade,EnglishPrelim,EnglishMidterm,EnglishFinals,EnglishAverage,EnglishFinalGrade,SciencePrelim,ScienceMidterm,ScienceFinals,ScienceAverage,ScienceFinalGrade,StatisticsPrelim,StatisticsMidterm,StatisticsFinals,StatisticsAverage,StatisticsFinalGrade,ReadandWritePrelim,ReadandWriteMidterm,ReadandWriteFinals,ReadandWriteAverage,ReadandWriteFinalGrade) values ('" + txtStudentID.Text + "' , '" + txtName.Text + "' , '" + txtSection.Text + "' , '" + cmbSemester.SelectedItem + "', '" + txtMathp.Text + "' , '" + txtMathm.Text + "' , '" + txtMathf.Text + "' , '" + txtMatha.Text + "' , '" + txtMathFG.Text + "' , '" + txtEnglishp.Text + "' , '" + txtEnglishm.Text + "', '" + txtEnglishf.Text + "','" + txtEnglisha.Text + "','" + txtEnglishFG.Text + "','" + txtMathFG.Text + "','" + txtSciencep.Text + "','" +txtSciencem.Text+ "','" + txtSciencef.Text + "','" + txtSciencea.Text + "','" + txtScienceFG.Text + "','" + txtStatisticsp.Text + "','" + txtStatisticsm.Text + "','" + txtStatisticsf.Text + "','" + txtStatisticsa.Text + "','" + txtStatisticsFG.Text + "','" + txtReadandWritep.Text + "','" + txtReadandWritem.Text + "','" + txtReadandWritef.Text + "','" + txtReadandWritea.Text + "','" + txtReadandWriteFG.Text + "')";
/* this is a string or a query used to execute. asterisk is used
to give you all column data from your database ,declaration of query */
command.ExecuteNonQuery(); // this is used to inserting data , updating or deleting data , this command will execute the above query
MessageBox.Show(" Saved! ");
}
catch (Exception a)
{
MessageBox.Show(" Error " + a.Message);
}
connection.Close();
I see in the first part you have 29 fields, but the inserted fields are 30...
In addition, you should use parameterized queries to avoid sql injection.
You need to debug codes and paste your CommandText to SSMS(SQL Sever Management Studio) to figure out errors.
Other suggestions:
command.CommandText = string.format("insert into StudentsRecord(..) VALUES(#...)); sql params to avoid sql injection
using(SqlConnection conn = ...)
Already solved the problem just now by putting [ ] on each field. which will be something like this:
insert into StudentsRecord(
[StudentID],[Name],[Section],[Semester],
[MathPrelim],[MathMidterm],[MathFinals]
) values ('"++"'..) ... etc.
Thanks everyone who tried to help me, have a good day!
Mabuhay!
Hi! Sorry to bother but im getting error in pasring this code.
SqlDataAdapter sda5 = new SqlDataAdapter("insert into [bodega].[dbo].[Stocks] ([Itemlookupcode],[Quantity],[Status],[Description],[Amount],[Total]) values ('" + ilc1.Text + "','" + qty1.Text + "','IN','" + description.Text + "','" + amount.Text + "','" + int.Parse(amount.Text) * qty1.Text + "')", con);
error
Error 1 Operator '*' cannot be applied to operands of type 'int' and 'string' G:\Data\Payroll\Program\BodegaItems\BodegaItems\Form1.cs 26 268 BodegaItems
I even try
'" + Convert.ToInt32(amount.Text) * qty1.Text + "'
Thank you!
The bug is telling you that it can't multiply an integer int.Parse(amount.Text) with the text value of qty1.Text.
You need to parse qty1.Text into an int: int.Parse(qty1.Text).
I am trying to save some data to access DB but the date is stored in incorrect format
dbCommand.CommandText = "insert into Clients(Name,Gender,PhoneNumber,ReciveServiceDate)
values ('" + name_txtBox.Text + "','" + gender_comBox.Text + "',"
+ long.Parse(phone_txtBox.Text) + ","
+ (recive_dateTimePicker.Value).ToShortDateString() + ");";
Listen to Jon's advice.
However, if you insist, you can do it like this:
+ (recive_dateTimePicker.Value).ToString("#yyyy'/'MM'/'dd#") + ");";
string query = "insert into TraineeDetail values('" + trainee.TyNo + "','" +
trainee.PersonalNumber.ToString() + "','" + trainee.TraineeName.ToString() + "','" +
trainee.Rank.ToString() + "','" + trainee.Division.ToString() + "','" + trainee.ENMATEL + "','" +
trainee.ENMATMECH + "','" + trainee.ENMATGSC + "','" + trainee.MAXM.ToString() + "','" +
trainee.SUBBR.ToString() + "') order by MAXM desc ";
i m getting error- missing semicolon (;) at the end of the sql statement
any solution
INSERT statements are used for just that. Inserting data.
You appear to be using it combined with an ORDER BY. What do you intend to order?
ORDER BY is generally used when you are SELECT'ing data. As in, "I want this data, but order it in this way before you show it to me".
Remove the ORDER BY and your query will work.
Please also investigate SQL injection and SqlParameters. As it is, your code is very insecure.