关于c#:错误:无法将类型’System.DateTime’隐式转换为’System.Data.Common.DbParameter’

Error :Cannot implicitly convert type 'System.DateTime' to 'System.Data.Common.DbParameter'

我在代码中发现了错误,我不确定下一步该怎么做

前端(ASP.NET)中的代码是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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" />
                   
                </SelectParameters>
            </asp:SqlDataSource>



 

<br /><br />
Start Date:
      </asp:TextBox>

      &nbsp;&nbsp;End Date:
      </asp:TextBox>
      &nbsp;&nbsp;&nbsp;&nbsp;<br />
<br /><br />

当后端的代码是

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

1
    }

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

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

谢谢


你错了,应该是:

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

注意代码中缺少的.value。您试图将一个DateTime分配给一个DbParameter


尝试

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

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