Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I recently got very different error. that I wrote a query which when execute from mysql workbench it executes successfully and showing result as required but when same query executing from c# code it is giving error?
whats wrong with my query please tell me where is the problem ?

here is my code and query statement,

C#
string query = "SELECT DISTINCT indent_no as 'Indent No'," +
                " approved_date as 'Approved Date'," +
                " from_store_name as 'From Store'," +
                " to_store_name as 'To Store'," +
                " indent_status as 'Indent Status'," +
                " processing_status as 'Processing Status'" +
                " FROM vw_DSInfo_indent_details" +
                " WHERE indent_status like 'Approved'" +
                " AND processing_status Not like 'Fully Processed'" +
                " AND from_store_name = @fmstore" +
                " AND to_store_name = @tostore" +
                " AND approved_date between @from and @to;"; 
                         
cmd = new MySqlCommand();
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
cmd.Parameters.Add(new MySqlParameter("from", datetimefrm));
cmd.Parameters.Add(new MySqlParameter("to", datetimeto));
cmd.Parameters.Add(new MySqlParameter("fmstore", txtfromstore.SelectedItem));
cmd.Parameters.Add(new MySqlParameter("tostore", txttostore.SelectedItem));
table = new DataTable();
adapter = new MySqlDataAdapter(cmd);
adapter.Fill(table);
int c = table.Rows.Count;
}


edited::
error at bold line if i execute without this line then shows result but with this line not gives any result.
Posted
Updated 22-Jan-16 20:33pm
v2
Comments
Sarath kumar.N 23-Jan-16 0:52am    
You got very different error! What is the error message?
Member 11543226 23-Jan-16 2:10am    
it not gives any error now but not showing result
PIEBALDconsult 23-Jan-16 1:25am    
I may just be tired, but doesn't the MySQL provider use backticks (`) rather than apostrophes?

I also recommend using a verbatim string when writing long SQL statements, to ensure that nothing breaks when copying to/from some other tool.
For example:

string query = @"
SELECT DISTINCT indent_no as `Indent No`
, approved_date as `Approved Date`
, from_store_name as `From Store`
, to_store_name as `To Store`
, indent_status as `Indent Status`
, processing_status as `Processing Status`
FROM vw_DSInfo_indent_details
WHERE indent_status like `Approved`
AND processing_status Not like `Fully Processed`
AND from_store_name = @fmstore
AND to_store_name = @tostore
AND approved_date between @from and @to;
" ;
(Putting the commas at the beginnings of lines makes it easier for me to see them.)
Garth J Lancaster 25-Jan-16 0:43am    
(I've only ever used single quotes with MySQL on Windows) but I do like your example, ie Verbatim string, SQL keywords in CAPS, putting the commas at the front of the lines - this last one narks my 'internal style guide', but makes sense :-)

1 solution

try it this way (note my mods to your cmd.Parameters.Add(...) lines)

C#
cmd.Parameters.Add(new MySqlParameter("@from", datetimefrm));
cmd.Parameters.Add(new MySqlParameter("@to", datetimeto));
cmd.Parameters.Add(new MySqlParameter("@fmstore", txtfromstore.SelectedItem));
cmd.Parameters.Add(new MySqlParameter("@tostore", txttostore.SelectedItem));
 
Share this answer
 
Comments
Member 11543226 23-Jan-16 2:10am    
i did this before but not getting result. error is at between clause, if i removed between clause it shows result but with this condition it doesn't shows result.
PIEBALDconsult 23-Jan-16 10:38am    
Then lok at the values of @from and @to to ensure that they are what you expect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900