-1
我目前正在做一个Web应用程序,我需要过滤一个日期范围的图表,这个范围选择了两个日历,将日期传递给两个不同的文本框。我尝试过两种不同的方式:使用filterparameters和“between @ date1 and @ date2”查询。从字符串转换日期和/或时间时转换失败。
的事情是我得到的是说了一个问题:从字符串转换日期和/或时间时
转换失败。 只有当我选择第二个日期范围时,才会出现此错误,它不会影响我从日历中选择日期的顺序。我也检查了我的日期时间转换。
我已经debbuged已经和我没有看到我的代码中的错误:
protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
txtDate2.Text = Calendar2.SelectedDate.ToShortDateString();
Calendar2.Visible = false;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtDate1.Text = Calendar1.SelectedDate.ToShortDateString();
Calendar1.Visible = false;
}
这是我在代码和客户端查询:
protected void btnShow_Click(object sender, EventArgs e)
{
DateTime date1 = Convert.ToDateTime(txtDate1.Text);
DateTime date2 = Convert.ToDateTime(txtDate2.Text);
using (SqlConnection conn = new SqlConnection(CONNECTION_STRING))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
cmd.Connection = conn;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "select Family, sum (TimeMins) as sumfield from DTCres where DateCres between @Date1 and @Date2 group by Family order by sumfield desc ";
cmd.Parameters.AddWithValue("@Date1", date1);
cmd.Parameters.AddWithValue("@Date2", date2);
conn.Open();
cmd.ExecuteNonQuery();
//SqlDataReader dr = cmd.ExecuteReader();
//if (dr.HasRows == !true)
//{
// //dateLabel.Visible = true;
//}
//if (dr.HasRows == true)
//{
// //dateLabel.Visible = false;
//}
conn.Close();
}
catch (Exception nessie)
{
string doc = nessie.Message;
//dateLabel.Visible = true;
}
}
}
}
<asp:SqlDataSource ID="SqlDS" runat="server" ConnectionString="<%$ ConnectionStrings:DTCrestronConnectionString %>"
SelectCommand="select Family, sum (TimeMins) as sumfield from DTCres where DateCres between @Date1 and @Date2 group by Family order by sumfield desc"
FilterExpression="Family='{0}'">
<SelectParameters>
<asp:ControlParameter Name="Date1" ControlID="txtDate1" PropertyName="Text" Type="String"/>
<asp:ControlParameter Name="Date2" ControlID="txtDate2" PropertyName="Text" Type="String"/>
</SelectParameters>
<FilterParameters>
<asp:ControlParameter ConvertEmptyStringToNull="false" Name="Family" ControlID="cmbChartFam1" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
</td>
请,任何帮助将如此好,并提前感谢
什么行具体是抛出错误?这听起来像是一个DateTime转换,它可以解决这个问题,你试图转换的字符串具体是什么? –
你应该输入这个答案,以便人们知道你找到了答案,找到了你的答案。请参阅:https://stackoverflow.com/help/self-answer –