2017-08-23 8 views
0

DateFrom、DateToおよびUserNameという3つのクエリ文字列変数を渡したいとします。その変数を呼び出すと、エラーが表示されます。エラー:条件が想定されるコンテキストで指定された非ブール型の式

'An expression of non-boolean type specified in a context where a condition is expected, near 'admin'.'".

問題を解決するにはどうすればよいですか?ここに私のコードです:

protected void Page_Load(object sender, EventArgs e) 
{ 
     strDate = Convert.ToDateTime(Request.QueryString["DateFrom"]); 
     endDate = Convert.ToDateTime(Request.QueryString["DateTo"]); 
     UserName = Convert.ToSingle(Request.QueryString["UsName"]); 
     string UserName = Request.QueryString["UsrName"]; 
     string sql; 
     sql = ("SELECT * FROM tblReport WHERE Date between'" + strDate + "'and'" + endDate + "'and'" + UserName + "'"); 
     SqlDataAdapter sda = new SqlDataAdapter(sql, con); 
     DataTable dt = new DataTable(); 
     DataSet dst = new DataSet(); 
     sda.Fill(dst, "tblReport"); 
     crypt.Load(@"D:\My Project\Asp.Net\ITApplication\ITApplication\CrystalReport.rpt"); 
     crypt.SetDataSource(dst); 
     CrystalReportViewer1.ReportSource = crypt; 
} 
+0

が間違ったクエリ文字列を実行しているようです。 SELECT * FROM tblReport WHERE where '+ strDate + "'と+ endDate +" 'とUserName =' "+ UserName +" '"'(パラメータ化されたクエリを使用します)。 –

答えて

0

あなたのSQL文字列はうまくフォーマットされていません。

youtは近くのPutスペースand事業者とあなたのbetween()を使用します。

sql = ("SELECT * FROM tblReport WHERE [Date] between ('" + strDate + "' and '" + endDate + "') and UserName='" + UserName + "'"); 

サイドノート:SQL文字列を使用して

を次のように値を連結して非常に悪い考えです。それはSQLインジェクションにあなたを暴露し、全体的に悪い習慣です。 Command.Parametersを使用することを検討してください:であなたは何かが欠けている

SqlCommand Command = new SqlCommand("SELECT * FROM tblReport WHERE [Date] between (@strDate and @endDate) and [email protected]"); 
Command.Parameters.Add(new SqlParameter("strDate", strDate)); 
Command.Parameters.Add(new SqlParameter("endDate", endDate)); 
Command.Parameters.Add(new SqlParameter("UserName", UserName)); 
0

をあなたの句は...このようにする必要があります:

WHERE Date BETWEEN '<FromDate>' AND '<ToDate>' 
AND UserName = '<UserName>' 
+0

ありがとうございます。 –

関連する問題