2

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 = '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 />

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

Mervin
  • 725
  • 2
  • 16
  • 37
  • Isn't this the same question as http://stackoverflow.com/questions/6128622/converting-a-string-into-date-format-in-the-code-behind ? – Jan-Peter Vos May 25 '11 at 18:54

2 Answers2

3

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.

James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
0

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();
KBoek
  • 5,794
  • 5
  • 32
  • 49