What is wrong with this custom query syntax used inside C# code? - c#

The query looks like this:
string query = "IF NOT EXISTS(
SELECT * from clienti
where CodCliente= ' " + id + " '
AND NomeCliente= ' " + emri + " '
AND RagioneSociale=' " + ragSoc + " '
AND PartitaIVA=' " + piva + " ')
Insert INTO clienti VALUES(
' " + id +
" ',' " + emri +
" ',' " + ragSoc +
" ',' " + piva + " ')
else
UPDATE clienti
SET(' " + id + " ',' " + emri + " ',' " + ragSoc + " ',' " + piva + " ')";
I just keep getting the same problem stated below:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF NOT EXISTS(SELECT * from clienti where CodCliente= ' 1 ' AND NomeCliente= ' ' at line 1

I think you're missing the THEN and END IF keywords. Check mysql manual
IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF

You are concatenating blank spaces before and after all your variables. Also, make sure all your variables are in string format.

Related

in c# how to insert different types(combobox,TimeDataPicker,..) into Access Databse

i am trying to insert (textbox, combobox , Datatimepicker)
To access Database but i got this error:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Syntax error in INSERT INTO statement.
Here is my code:
private void button1_Click(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText =
"insert into Hoteldb (Guest Full Name, Date-Birth, Room Numb, Room Type,Check In Date,Check Out Date,No-Of-Adults,No-Of-Child,Advance Payment,Total Balance ) values(' "
+ this.textBox1.Text + " ', '" + this.dbp.Text + " ' ,' " + this.textBox2.Text + " ' , '" + this.comboBox1.Text + "', '" + this.dateTimePicker1.Text + "','" + this.dateTimePicker2.Text +
"' ,'" + this.textBox5.Text + " ','" + this.textBox6.Text + " ',' " + this.textBox3.Text + " ',' " + this.textBox4.Text + " ') ";
command.ExecuteNonQuery();
MessageBox.Show("Geust Check-in");
connection.Close();
}
what is the right way that can i through it insert the data correctly

Using string SQL queries

I am getting the following error.
Message = "Incorrect syntax near the keyword 'AS'.\r\nIncorrect syntax near the keyword 'AS'."
I have the following sql string query.
private const string QueryString =
"SELECT DISTINCT "
+ " Person.[PersObjId]"
+ ",Person.[PtObjId]"
+ ",MSN.[PersObjId]"
+ ",MSN.[ExtrnId] AS '" + MSNumber + "'"
+ ",HlthProfMstrV.[RptName] AS '" + ItemName + "'"
+ ",HlthProfMstrV.[ItemStsId] AS '" + ItemId + "'"
+ "FROM [dbt1].[Person]"
+ "join"
+ " ( "
+ " SELECT "
+ " PersIdRptV.PersObjId"
+ " , PersIdRptV.ExtrnId"
+ " FROM"
+ " dbt1.PersIdRptV"
+ " join dbt1.IdIssMstrV on PersIdRptV.IdIssObjId = IdIssMstrV.IdIssObjId"
+ " WHERE"
+ " PersIdRptV.TypeId = 5"
+ " and"
+ " PersIdRptV.StpDate is NULL"
+ " ) AS MSN on dbt1.Person.PersObjId = MSN.PersObjId"
+ "left join dbt1.HlthProfMstrV on Person.PCPHlthProfObjId = HlthProfMstrV.HlthProfObjId"
+ " " + "WHERE" + " "
+ "Person.[ExpressId] =12"
+ " and" + " "
+ "Person.[StnTst] = 'Actv' "
+ " and" + " "
+ "MSN.[ExtrnId] = #MSN ";
I think this error is occuring when I am getting the first join result where I assign the result set to "MSN". I tried taking out the AS from AS MSN and I get another error saying the following:
Message = "Incorrect syntax near the keyword 'MSN'.\r\nIncorrect
syntax near the keyword 'MSN'."
I been looking at this for few hours. I am sure its something simple I am missing. Help appreciated!
You're missing a space between last column alias and From keyword:
+ ",HlthProfMstrV.[ItemStsId] AS '" + ItemId + "'"
+ " FROM [dbt1].[Person]"
Space missing before person and join
+ "FROM [dbt1].[Person]"
+ " join" --> Added space before join
+ "MSN.[ExtrnID] = '" + #MSN + "'"

How to update a specific column without disturbance in other columns in database?

("update Medicine set Name = ' " + textbox1.Text + " ', Company = ' " + textbox2.Text + " ', Type = ' " + textbox3.Text + " ', Quantity = ' " + textbox4.Text + " ' where P_id =' " + textbox5.Text + " ' ");
It does not working because whenever I update any column it makes other columns NULL.
One obvious solution is to construct the update with lists of columns.
An alternative is to use coalesce():
update Medicine
set Name = coalesce(' " + textbox1.Text + " ', Name),
Company = coalesce(' " + textbox2.Text + " ', Company),
Type = coalesce(' " + textbox3.Text + " ', Type,
Quantity = coalesce(' " + textbox4.Text + " ', Quantity)
where P_id =' " + textbox5.Text + " '
Actually, you should be using parameters rather than directly putting values in the query string. Putting parameters in directly makes the system vulnerable to SQL injection.
That said, I'm not sure how you are getting NULL values. You might really want:
update Medicine
set Name =(case when ' " + textbox1.Text + " ' = '' then '" + textbox1.Text + "' else Name end),
. . .

How to add data into mysql using c# (edit table column on combo box selection )

I want to add item in a table in a database in mysql based upon what the user has selected in the items combobox.
Eg: if a person chooses tea (this event should populate tea column in my table) and fills a quantity as 2 then I want to add 2 under the column name tea in mysql table . I'm trying to use the update statement, but it gives an error the syntax is not correct.The column to be filled in table changes if the user chooses tea or coffee that's why I have used "+this.items.Text+"
"UPDATE employee.transaction
SET department = ' " + this.department.Text + " ',
' " + this.items.Text + " ' = ' " + this.quantity.Text + " ' ,
billno = ' " + this.bill_no_txt.Text + "'
where billno = ' " + this.bill_no_txt.Text + " ' ;";
I might see several points in your query that could cause problems. So lets go through it:
SET department = ' " + this.department.Text + " ',
Please make sure you enter the right data into the database. In SQL queries you need to make sure you have no spaces if you enter a text into a text or a varchar field. What you enter above is " TEXT " and not "TEXT" (mind the spaces). The following will enter the text without a space at the beginning and the end:
SET department = '" + this.department.Text + "',
What's most likely causing the error is:
billno = ' " + this.bill_no_txt.Text + "'
I assume your billno column is defined as an int (which would be correct). But you have to make sure you insert it as such. By using ' (single brakets) you are trying to enter text into the int field, which would cause an error. I can not say it for sure since I can't see the table definitions of the table you want to update the data in.
What else could cause the problem is that the column name is wrong here:
' " + this.items.Text + " ' = ' " + this.quantity.Text + " ' ,
' " + this.items.Text + " '
will basickly enter "' Columnname '" which is wrong (with "'" AND spaces). It probabely should be "Columnname". Try it without spaces and without singlebackets:
" + this.items.Text + " = '" + this.quantity.Text + "' ,
But despite all of that I think you have an architectural problem in your database... If you have 30 different beverages you will need to have 30 columns which can be done with a n to n relationship between a three column table and a two column table. I suggest you look a bit into that first.
Heres the corrected query but make sure you understand the changes:
UPDATE employee.transaction SET department = '" + this.department.Text + "'," + this.items.Text +" = '" + this.quantity.Text + "' , billno = " + this.bill_no_txt.Text + " where billno = " + this.bill_no_txt.Text + ";

MySql Inner join of 2 table with WHERE Clauses

I am making a query to 3 Mysql tables:
Sql = "SELECT " +
" u.iUserID, " +
" u.szPlayerName, " +
" u.szPlayerSurname, " +
" u.iPreferredPos, " +
" u.iAltPos, " +
" u.charType, " +
" c.szClubPrefix, "+
" tblteam.szTeamName " +
"FROM " +
" tblteam" +
" INNER JOIN tblusers u ON (tblteam.iTeamID = u.iTeamID) " +
" INNER JOIN tblclubs c ON (u.iClubID = c.iClubID) " +
"WHERE " +
"c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ' AND " +
"u.bActive = 1 " +
"AND u.charType= 'c'" +
"ORDER BY " +
" tblteam.szTeamName DESC, " +
" u.szPlayerSurname;";
my problem is that; this query only returns the data where it match c.szClubPrefix = 'CLM' and not where it match u.iClubID2 =' 2 ' please anyone can Help me what I am doing wrong here?
Thanks for your help.
Add parenthesis with respect to your preferences
"(c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ') AND " +
"u.bActive = 1 " +
"AND u.charType= 'c'"
wrap in parenthesis
(c.szClubPrefix = 'CLM' OR u.iClubID2 ='2')
and remove the whitespace before and after 2.
Note that dealing with numbers in MySQL you don't need the '
e.g.
iClubID2 = 2
instead of
iClubID2 ='2'
your where clause looks like this:
WHERE c.szClubPrefix = 'CLM' OR u.iClubID2 =' 2 ' AND u.bActive = 1 AND u.charType= 'c'
and it equivalence:
WHERE c.szClubPrefix = 'CLM' OR ( u.iClubID2 =' 2 ' AND u.bActive = 1 AND u.charType= 'c' )
so, u.iClubID2 =' 2 ' is one of those 3 conditions must be all true.

Categories

Resources