Click here to Skip to main content
15,909,645 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to run sql query from program , the query when used in stored procedure, it works fine , but when using from program it gives above error.I cant use stored procedures as per clients requirement. my code is bellow,

C#
DateTime frmdate = Convert.ToDateTime(dtpfrom.Text);
                    DateTime todate = Convert.ToDateTime(dtpto.Text);
                    string datetimefrm = frmdate.ToString("yyyy-MM-dd 00:00:00.000");
                    string datetimeto = todate.ToString("yyyy-MM-dd 00:00:00.000");

   string ap = "Approved"+'%';
   string st = "Fully Processed"+'%';
                        
 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 '"+ap+"'" +
        "AND [approved_date] BETWEEN '" + datetimefrm + "' AND '" + datetimeto + "'" +
        "AND [processing_status] NOT LIKE '"+st+"'" +
        "AND [from_store_name] ='" + txtfromstore.SelectedItem + "'"+
        "AND [to_store_name] = '" + txttostore.SelectedItem + "'";
                          
                            cmd = new SqlCommand();
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = query;
                            cmd.Connection = con;
                            table = new DataTable();
                            adapter = new SqlDataAdapter(cmd);
                            adapter.Fill(table);
Posted
Updated 21-Jan-16 0:39am
v3

Spaces.
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 '"+ap+"'" +

Generates a string like:
SELECT DISTINCT ...processing_status as 'Processing Status'FROM vw_DSInfo_indent_detailsWHERE [indent_status] like ...
You get away with it for the Status'FROM becuaset the quote terminates the prvious data. But
vw_DSInfo_indent_detailsWHERE
is taken as a single word.
And please, don't do it like that.
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. You get away with it here because you define the strings yourself, but it's a dangerous precedent and may well leave you wide open to SQL Injection if the code is ever changed. Get into the habit of using parameterised queries at all times - it's a lot easier to read, and a lot safer!
 
Share this answer
 
Comments
Member 11543226 21-Jan-16 6:57am    
ok will search for parameterised queries, thanks
Use the debugger to examine the contents of the query variable and you'll see it has things like

processing_status as 'Processing Status'FROM vw_DSInfo_indent_detailsWHERE [indent_status] like 'xyz'AND [approved_date] BETWEEN


You're not adding the appropriate spaces.

FROM vw_DSInfo_indent_details" +
" WHERE [indent_status] like '"+ap+"'" +
" AND [approved_date] BETWEEN '" + datetimefrm + "' AND '" + datetimeto + "'" +


You should also use paramterised queries as your code will fail if the data you search for contains an apostrophe, and worse it leaves you open to SQL injection attacks. Google for how to use paramterised queries with ado.net for examples.
 
Share this answer
 
Comments
Member 11543226 21-Jan-16 7:21am    
thanks
It looks single quotes are extra.
Simply just replace your variable with following code
C#
string ap = "Approved%";
string st = "Fully Processed%";
 
Share this answer
 

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