Search code examples
c#asp.netsqldatasource

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


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] &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 />

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


Solution

  • 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.