错误:无法将类型'System.DateTime'隐式转换为'System.Data.Common.DbParameter'

时间:2011-05-25 18:36:27

标签: c# asp.net sqldatasource

我的代码中出现错误,我不确定下一步该做什么

前端的代码(asp.net)是

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                ConnectionString="<%$ ConnectionStrings:LicensingConnectionString %>" 



                SelectCommand="SELECT [School Name], [School City], [School State], LoginName, [Current Sales], Commission, [Pay Period start date], [Pay Period End date] FROM commissions WHERE ([Pay Period start date] &gt;= @Txt_selected_start_date) AND ([Pay Period End date] &lt;= @Txt_selected_end_date) AND (LoginName = 'mervinj@uw.edu') " 
                OnSelecting="SqlDataSource2_Selecting"
                >
                <SelectParameters>
                    <asp:parameter  
                        Name="Txt_selected_start_date" Type="DateTime" />
                    <asp:Parameter Name="Txt_selected_end_date"  Type="DateTime" />
                </SelectParameters>
            </asp:SqlDataSource>



<asp:PlaceHolder ID="pnlwithdates" runat="server" Visible="false"> 
<div style="padding: 0 200px 0 200px">
<br /><br />
Start Date: <asp:TextBox ID="TxtDatepicker_start" runat="server" Width = 125px >
      </asp:TextBox>

      &nbsp;&nbsp;End Date: <asp:TextBox ID="TxtDatepicker_end" runat="server" Width = 125px >
      </asp:TextBox>
      &nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="Button_daterecords" runat="server"  Text="Show records"  OnClick ="SQLDisplay_Date_records" /><br />
<br /><br />

而后端的代码是

  

受保护的虚空   SqlDataSource2_Selecting(对象   发件人,   SqlDataSourceSelectingEventArgs e)           {               e.Command.Parameters [ “用户名@”]。值   = HttpContext.Current.User.Identity.Name;               的 e.Command.Parameters [ “@ Txt_selected_start_date”]   = DateTime.Parse(TxtDatepicker_start.Text);

    }

粗体代码是引发错误的行。

如何解决这个问题,任何输入都会很棒。

谢谢

2 个答案:

答案 0 :(得分:3)

你说错了,它应该是:

e.Command.Parameters["@Txt_selected_start_date"].Value = DateTime.Parse(TxtDatepicker_start.Text);

请注意代码中缺少的.Value。您正尝试将DateTime分配给DbParameter

答案 1 :(得分:0)

尝试

e.Command.Parameters["@Txt_selected_start_date"].Value = DateTime.Parse(TxtDatepicker_start.Text)

或者

e.Command.Parameters["@Txt_selected_start_date"].Value = DateTime.Parse(TxtDatepicker_start.Text).ToShortDateString();
相关问题