c# sql cannot convert varchar to int (Found a work around) - c#

im trying to insert some data into a sql server and the code worked fine untill i added the #instel at the end. when i added that i got the error "Error converting data type varchar to numeric." even if i leave the textbox empty.
i had it as a textbox but since i got the error i tried to have it as a variable but it still did not fix the issue. the code works for the REL thats to be inserted and that is also a int. also the database and textbox properties of REL and #instel are the same. i also tried to make the variable a int but it did not change anything. if anyone knows why this is and could help me out it would be appriciated.
SqlCommand slMEVO_NAW = new SqlCommand(#"insert into NAW(REL,NAAM,BTW_CODE,TAV,STR,PL,LAND,PLBOX,TAAL,COUNTRY_C,TEL,FAX,EMAIL,XMAS,TEL2,STR1,INSTELINCL,INCOTERMS,PRODDAT,INSTEL) values('" + this.Txtbx_RelNmmr.Text + "','" + this.Txtbx_Bedrijf.Text + "','" + this.Txtbx_BtwCode.Text + "','" + this.Txtbx_Tav.Text + "','" + this.Txtbx_HuisAdres.Text + "','" + this.Txtbx_PstCde.Text + "','" + this.Txtbx_Land.Text + "','" + this.Txtbx_PstBs.Text + "','" + this.Txtbx_Taal.Text + "','" + this.Txtbx_LndCode.Text + "','" + this.Txtbx_Telnmmr.Text + "','" + this.Txtbx_Fax.Text + "','" + this.Txtbx_Email.Text + "','" + this.Txtbx_Xmas.Text + "','" + this.Txtbx_telnmmr2.Text + "','" + this.Txtbx_BezAdres.Text + "','" + this.TBX_Instelkstn.Text + "','" + this.Txtbx_Incot.Text + "','" + this.TBX_PrdctDtm.Text + "','#INSTEL' );", Connectie.connMEVO_NAW);
slMEVO_NAW.Parameters.Add("#INSTEL", SqlDbType.VarChar).Value = Txtbx_InstelKsten.Text;
FIX
it seems that after i changed it to all parameters it only gives the error if the textbox is empty so i just make the program set the text to 0 before it does the query right now

The type of field in your table might be as Integer or Numeric, change to Varchar.

as i stated in the main post after a edit i found a way to work around the problem
FIX it seems that after i changed it to all parameters it only gives the error if the textbox is empty so i just make the program set the text to 0 before it does the query right now

Related

Conversion to date time from char string failed on Insert but works on Update

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?

C# windowsform - Getting Syntax Error at Insert into statement

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!

Adding two values from selected query in c#

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).

Saving DateTime value into microsoft access database

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#") + ");";

missing ";" in insert query c# access database

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.

Categories

Resources