从字符串转换日期和/或时间时转换失败。 [英] Conversion failed when converting date and/or time from character string.?

查看:113
本文介绍了从字符串转换日期和/或时间时转换失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





使用两个日期和一个下拉到搜索记录并显示在网格中但是收到错误



hi

am using two dates and one drop down to searh records and display in grid but am getting error

"

Conversion failed when converting date and/or time from character string."





in按钮点击





in button click

protected void searchCase_Click(object sender, EventArgs e)
  {



      //end add 18-11-2013
      DataLayer dl = new DataLayer();

      string fromDate = DateTime.Now.ToShortDateString();
      string toDate = DateTime.Now.ToShortDateString();

      ////DateTime fromDate = Convert.ToDateTime(FrmDate.Text);
      ////DateTime ToDate = Convert.ToDateTime(toDate.Text);
      ////DataTable sortedDataTable = dl.DisplayFilterCaseReportAll();

      DataTable sortedDataTable = dl.DisplayFilterCaseReport(DropDownList1.SelectedItem.Text, fromDate, toDate);
      if (sortedDataTable.Rows.Count > 0)
      {
          Panel1.Visible = true;
          GridView1.DataSource = sortedDataTable;
          GridView1.DataBind();
      }
      else
      {
          GridView1.DataSource = null;
          GridView1.DataBind();
      }

  }













public DataTable DisplayFilterCaseReport(string court, string startDate, string Endate)
    {
        string dt = DateTime.Now.ToShortDateString();
        try
        {

            string qry;
            sqlconn.Open();
            if (court=="Select")
             qry = "SELECT k_tbl.Fno as [رقم الملف], k_tbl.Dno as [رقم القضية], k_tbl.HnoEng as [المحكمة], k_tbl.TknoEng as [نوع القضية], k_tbl.SubjectEng as [موضوع الدعوى],k_tbl.MnoEng as [اسم الموكل],k_tbl.MAnoEng as [صفة الموكل],k_tbl.AccusedEngName as [اسم الخصم],S_tbl.Sdate as [تاريخ الجلسة],S_tbl.Dec as [قرار],k_tbl.lawername as [اسم المحامي ],k_tbl.amount as [المبلغ] FROM k_tbl INNER JOIN S_tbl ON k_tbl.Fno = S_tbl.FileNo WHERE  (S_tbl.Sdate <=CONVERT(datetime,'" + Endate + "',103)) AND (S_tbl.Sdate >= CONVERT(datetime,'" + startDate + "',103)) ORDER BY Sdate DESC";
            else
             qry = "SELECT k_tbl.Fno as [رقم الملف], k_tbl.Dno as [رقم القضية], k_tbl.HnoEng as [المحكمة], k_tbl.TknoEng as [نوع القضية], k_tbl.SubjectEng as [موضوع الدعوى],k_tbl.MnoEng as [اسم الموكل],k_tbl.MAnoEng as [صفة الموكل],k_tbl.AccusedEngName as [اسم الخصم],S_tbl.Sdate as [تاريخ الجلسة],S_tbl.Dec as [قرار],k_tbl.lawername as [اسم المحامي ],k_tbl.amount [المبلغ] FROM k_tbl INNER JOIN S_tbl ON k_tbl.Fno = S_tbl.FileNo WHERE k_tbl.HnoEng LIKE '" + court + "' AND   (S_tbl.Sdate <=CONVERT(datetime,'" +  Convert.ToDateTime(Convert.ToDateTime((Endate)).ToString("yyyy-MM-dd hh:MM:ss")) + "',103)) AND (S_tbl.Sdate >= CONVERT(datetime,'" +  Convert.ToDateTime(Convert.ToDateTime((startDate)).ToString("yyyy-MM-dd hh:MM:ss"))+ "',103)) ORDER BY Sdate DESC";
            DataTable ds = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(qry, sqlconn);
            da.Fill(ds);

            return ds;
        }

        catch (Exception ex)// this line am getting error
        {
            return null;
        }
        finally
        {
            sqlconn.Close();
        }
    }





任何人都可以建议我>>



谢谢



can any one suggest me>>

thank you

推荐答案

调用T-SQL CONVERT函数时的样式参数(第3个参数)无效。如果您要从datetime转换为基于字符的类型,则此情况下的值将适用。如果你删除它应该可以工作。



这就是说,你需要经过大量的工作才能简单地去掉你和日期之间的时间价值。您无需将其转换为字符串,然后返回日期。以下代码将剥离时间组件:

The style parameter (3rd parameter) in your call to the T-SQL CONVERT function is invalid. The value in this case would apply if you were converting from datetime to a character based type. It should work if you remove it.

That said, you're going through a lot of work to simply strip off the time value of you to and from dates. You don't need to convert it to a string and then back to a date. The following code will strip off the time component:
DateTime fromDate;
if (DateTime.TryParse(FrmDate.Text, out fromDate)) {
     fromDate = fromDate.Date;
}


这篇关于从字符串转换日期和/或时间时转换失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆