错误:无法隐式转换类型“System.DateTime”到“System.Data.Common.DbParameter”

发布于 2024-11-09 10:23:06 字数 2162 浏览 0 评论 0原文

我的代码后面出现错误,我不知道下一步该怎么做

前端(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 = '[email protected]') " 
                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["@username"].Value = HttpContext.Current.User.Identity.Name; e.Command.Parameters["@Txt_selected_start_date"] = DateTime.Parse(TxtDatepicker_start.Text);

<前><代码>}

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

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

谢谢

I am getting the error in my code behind and I am not sure what to do next

The code in the front end (asp.net) is

<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] >= @Txt_selected_start_date) AND ([Pay Period End date] <= @Txt_selected_end_date) AND (LoginName = '[email protected]') " 
                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>

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

while the code at the backend is

protected void
SqlDataSource2_Selecting(object
sender,
SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["@username"].Value
= HttpContext.Current.User.Identity.Name;
e.Command.Parameters["@Txt_selected_start_date"]
= DateTime.Parse(TxtDatepicker_start.Text);

    }

The bolded code is the line which is throwing the error .

How do I fix this,any inputs would be great.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恰似旧人归 2024-11-16 10:23:06

您设置错误,应该是:

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

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

You're setting it wrong, it should be:

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

Note the .Value that is missing in your code. You're trying to assign a DateTime to a DbParameter.

亣腦蒛氧 2024-11-16 10:23:06

尝试

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();

Try

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

Or

e.Command.Parameters["@Txt_selected_start_date"].Value = DateTime.Parse(TxtDatepicker_start.Text).ToShortDateString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文