2017-07-03 136 views
-3

我在添加語句BETWEEN以在兩個日期之間進行選擇並顯示數據後發生此問題。在此之前,它只適用於我只選擇一個日期。當我想選擇日期以外的其他值時會發生此問題。它顯示字符串不被識別爲有效的日期時間。但如果我只選擇日期或日期的值,它不會出現任何錯誤。爲什麼會發生?它只適用於我有一個日期選擇,但錯誤,當我在日期之間添加聲明。這是我的代碼到目前爲止。在選擇日期asp.net c中添加BETWEEN語句後,字符串未被識別爲有效日期時間#

if (!string.IsNullOrEmpty(DropDownListSearchJO.SelectedValue) || (!string.IsNullOrEmpty(DropDownListSearchLine.SelectedValue)) || (!string.IsNullOrEmpty(TextBoxSearchRec.Text)) || (!string.IsNullOrEmpty(TextBoxSearchRec2.Text)) || (!string.IsNullOrEmpty(DropDownListProcess.SelectedValue))) 
     { 

      try 
      { 
       con.Open(); 

       //select data from CutPanelCard 
       query = "select a.req_id, a.prod_line, a.jo_no, a.buyer, a.request_date,CONVERT(VARCHAR(10),a.need_by_date ,101) as need_by_date, a.qty, a.username, b.process_id from CutPanelCard a LEFT JOIN CutPanelConfirmation b on b.req_id = a.req_id "; 

       //check if dropdownJO not empty so query the data 
       if (!string.IsNullOrEmpty(DropDownListSearchJO.SelectedValue)) 
       { 
        query += "where [email protected]_no and status='F' "; 
        cmd = new SqlCommand(query, con); 
        cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
        checking = true; 

       } 

       //check if dropdownLine not empty so query the data 
       if (!string.IsNullOrEmpty(DropDownListSearchLine.SelectedValue)) 
       { 
        if (checking == true) 
        { 
         query += "and [email protected]_line and status='F' "; 
         cmd = new SqlCommand(query, con); 
         cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
         cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
         cmd.Parameters.AddWithValue("@from", Convert.ToDateTime(TextBoxSearchRec.Text)); 
         cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text)); 
        } 
        else 
        { 
         query += "where p[email protected]_line and status='F' "; 
         cmd = new SqlCommand(query, con); 
         cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
         cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
         checking = true; 
        } 
       } 

       //check if dropdownProcess not empty so query the data 
       if (!string.IsNullOrEmpty(DropDownListProcess.SelectedValue)) 
       { 
         if (checking == true) 
         { 
          query += "and a.req_id not in (select req_id from cutpanelconfirmation where process_id = @process_id) and status='F' "; 
          cmd = new SqlCommand(query, con); 
          cmd.Parameters.AddWithValue("@processs_id", DropDownListProcess.SelectedValue); 
          cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
          cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
         } 
         else 
         { 
          query += "where a.req_id not in (select req_id from cutpanelconfirmation where process_id = @process_id) and status='F' "; 
          cmd = new SqlCommand(query, con); 
          cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue); 
          cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
          cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
          checking = true; 
         } 
       } 

       //check if textboxRec not empty so query the data 
       if ((!string.IsNullOrEmpty(TextBoxSearchRec.Text)) || (!string.IsNullOrEmpty(TextBoxSearchRec2.Text))) 
       { 
        if (checking == true) 
        { 
         query += "and need_by_date BETWEEN @from and @to and status='F' "; 
         cmd = new SqlCommand(query, con); 
         cmd.Parameters.AddWithValue("@from", Convert.ToDateTime(TextBoxSearchRec.Text)); 
         cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text)); 
         cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
         cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
         cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue); 
        } 
        else 
        { 
         query += "where need_by_date BETWEEN @from and @to and status='F' "; 
         cmd = new SqlCommand(query, con); 
         cmd.Parameters.AddWithValue("@need_by_date", Convert.ToDateTime(TextBoxSearchRec.Text)); 
         cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text)); 
         cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
         cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
         cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue); 
         checking = true; 
        } 
       } 


       //show the query based on ascending req_id and last_update 
       query += "order by a.req_id, b.last_update DESC "; 
       cmd = new SqlCommand(query, con); 
       cmd.Parameters.AddWithValue("@from", Convert.ToDateTime(TextBoxSearchRec.Text)); 
       cmd.Parameters.AddWithValue("@to", Convert.ToDateTime(TextBoxSearchRec2.Text)); 
       cmd.Parameters.AddWithValue("@jo_no", DropDownListSearchJO.SelectedValue); 
       cmd.Parameters.AddWithValue("@prod_line", DropDownListSearchLine.SelectedValue); 
       cmd.Parameters.AddWithValue("@process_id", DropDownListProcess.SelectedValue); 

       SqlDataAdapter da = new SqlDataAdapter(cmd); 

       //remove duplicated req_id before bind into gridview 
       DataTable dtRemoveDuplicate = new DataTable(); 
       da.Fill(dtRemoveDuplicate); 
       dtRemoveDuplicate = DeleteDuplicateFromDataTable(dtRemoveDuplicate, "req_id"); 
       GridView1.DataSource = dtRemoveDuplicate; 
       GridView1.DataBind(); 

       con.Close(); 
      } 
+1

調試並查看實際值。 –

+0

當運行時選擇除日期之外的其他選項時,它顯示錯誤字符串未被識別爲有效的日期時間 – NFH

+0

yes是結果,但您應該調試並查看錯誤的原因和位置,並且沒有人可以幫助您進行調試。 –

回答

-2

試試這個。

query += "and a.need_by_date BETWEEN @from and @to and status='F' "; 
query += "where a.need_by_date BETWEEN @from and @to and status='F' "; 
+0

仍然存在相同的錯誤:( – NFH

+0

使用datePicker控件,而不是文本框 –

+0

例如對不起,因爲我直到新的這個c#和visual studio – NFH

相關問題