Failed to convert parameter value from String to a DateTime
Posted: Wed Jan 18, 2012 9:23 am
Hi, im creating part of a recruitment agency website in (Visual Studio) ASP.Net 3.5 for an assignment that utilises a local web service to display job records. Im trying to get all jobs posted within the last 7 days.
Everything was working until I try to query the SqlDataBase (.mdf) by passing a date dd/mm/yyyy as a string. I receive the error "Failed to convert parameter value from a String to a DateTime" when I try to run. The db field type is set to 'date', and I have tried querying with a DateTime, instead of a string... but no joy! and dont think thats possible anyway.
I am double confused as when I run my query in the 'Query Builder' I need to use mm/dd/yyyy, but when I use 'Preview Data' option I have to use dd/mm/yyyy format...!?
Any help will be gratefully received...
Code behind (.cs)
protected void searchDaysButton_Click(object sender, EventArgs e)
{
if (daysRadioButtonList.SelectedIndex == 0)
{
string dateAdded = DateTime.Today.Date.Subtract(TimeSpan.FromDays(7)).ToString("dd/mm/yyyy");
//keywordTextBox.Text = dateAdded;
s.searchJobsByDateAdded(dateAdded);
}
}
Web Service method
[WebMethod] // this ges jobs by date added@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public DataSet1.JobsTableDataTable searchJobsByDateAdded(string date2)
{
DataSet1TableAdapters.JobsTableTableAdapter ta = new DataSet1TableAdapters.JobsTableTableAdapter();
return ta.GetDataByDateAdded2(date2);
}
Query in Job table (works in query builder!)
SELECT JobId, AgencyId, Title, Salary_Rate, Benefits, Keywords, JobType, Location, Start_Date, Job_Skills_1, Job_Skills_2, Job_Skills_3, Job_Skills_4, Other_Experience,
Description, Job_Ref, Date_Posted, Expiry_Date
FROM JobsTable
WHERE (Date_Posted >= @date2)
Sorry about all the //notes!
Everything was working until I try to query the SqlDataBase (.mdf) by passing a date dd/mm/yyyy as a string. I receive the error "Failed to convert parameter value from a String to a DateTime" when I try to run. The db field type is set to 'date', and I have tried querying with a DateTime, instead of a string... but no joy! and dont think thats possible anyway.
I am double confused as when I run my query in the 'Query Builder' I need to use mm/dd/yyyy, but when I use 'Preview Data' option I have to use dd/mm/yyyy format...!?
Any help will be gratefully received...
Code behind (.cs)
protected void searchDaysButton_Click(object sender, EventArgs e)
{
if (daysRadioButtonList.SelectedIndex == 0)
{
string dateAdded = DateTime.Today.Date.Subtract(TimeSpan.FromDays(7)).ToString("dd/mm/yyyy");
//keywordTextBox.Text = dateAdded;
s.searchJobsByDateAdded(dateAdded);
}
}
Web Service method
[WebMethod] // this ges jobs by date added@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
public DataSet1.JobsTableDataTable searchJobsByDateAdded(string date2)
{
DataSet1TableAdapters.JobsTableTableAdapter ta = new DataSet1TableAdapters.JobsTableTableAdapter();
return ta.GetDataByDateAdded2(date2);
}
Query in Job table (works in query builder!)
SELECT JobId, AgencyId, Title, Salary_Rate, Benefits, Keywords, JobType, Location, Start_Date, Job_Skills_1, Job_Skills_2, Job_Skills_3, Job_Skills_4, Other_Experience,
Description, Job_Ref, Date_Posted, Expiry_Date
FROM JobsTable
WHERE (Date_Posted >= @date2)
Sorry about all the //notes!