DNR
DNR

Reputation: 3736

DateTime error message: Conversion failed when converting datetime from character string

I am passing parameters to a stored proc. The parameters code block on the asp.net side is:

SqlConnection con = new SqlConnection(strConn);  
string sqlItemSearch = "usp_Item_Search";  
SqlCommand cmdItemSearch = new SqlCommand(sqlItemSearch, con);  
cmdItemSearch.CommandType = CommandType.StoredProcedure;  

cmdItemSearch.Parameters.Add(new SqlParameter("@Item_Num", SqlDbType.VarChar, 30));  
cmdItemSearch.Parameters["@Item_Num"].Value = txtItemNumber.Text.Trim();  

cmdItemSearch.Parameters.Add(new SqlParameter("@Search_Type", SqlDbType.Int));  
cmdItemSearch.Parameters["@Search_Type"].Value = ddlSearchType.SelectedItem.Value;  

cmdItemSearch.Parameters.Add(new SqlParameter("@Vendor_Num", SqlDbType.VarChar, 10));  
cmdItemSearch.Parameters["@Vendor_Num"].Value = txtVendorNumber.Text.Trim();  

cmdItemSearch.Parameters.Add(new SqlParameter("@Search_User_ID", SqlDbType.Int));  
cmdItemSearch.Parameters["@Search_User_ID"].Value = ddlSeachUser.SelectedItem.Value;  

if (!string.IsNullOrEmpty(txtStartDate.Text))  
{  
    cmdItemSearch.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.DateTime));  
    cmdItemSearch.Parameters["@StartDate"].Value = Convert.ToDateTime(txtStartDate.Text.Trim());  
}  
else  
{  
    cmdItemSearch.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.DateTime));  
    cmdItemSearch.Parameters["@StartDate"].Value = Convert.ToDateTime("01/01/1996");  
}  

if (!string.IsNullOrEmpty(txtEndDate.Text))  
{  
    cmdItemSearch.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));  
    cmdItemSearch.Parameters["@EndDate"].Value = Convert.ToDateTime(txtEndDate.Text.Trim());  
}  
else  
{  
    cmdItemSearch.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime));  
    cmdItemSearch.Parameters["@EndDate"].Value = Convert.ToDateTime(DateTime.Now);  
}  
con.Open();  

SqlDataAdapter ada = new SqlDataAdapter(cmdItemSearch);  
DataSet ds = new DataSet();  
ada.Fill(ds);  

gvSearchResults.DataSource = ds;  
gvSearchResults.DataBind();

I tried using

DateTime.ParseExact(this.Text, "dd/MM/yyyy", null);

but I get the same error. The corressponding param in SQL is DateTime. I am currently passing blank fields for @StartDate and @EndDate, so the default values are passed as parameters. The error occurs at ada.Fill(ds) line. What would be causing the error?

Upvotes: 2

Views: 3772

Answers (4)

DNR
DNR

Reputation: 3736

and the solution is.......

cmdItemSearch.Parameters["@EndDate"].Value = DateTime.Now;

not the Convert.ToDateTime(DateTime.Now);

Upvotes: 1

Cylon Cat
Cylon Cat

Reputation: 7201

The default .NET DateTime isn't a valid SQL DateTime value; that's what the error is coming from. If you're going to pass a DateTime parameter for search,but don't have a specific value to search on, you should provide something in SQL's DateTime value range that will work for all your searches.

Upvotes: 0

Matthew Abbott
Matthew Abbott

Reputation: 61589

Check the culture the database is configured to use and make sure the date/time being passed in (as configured by the CurrentCulture), is compatible.

If you don't have control over the culture the database is using, you can force it to accept a specific format by prepending SET DATEFORMAT yada yada to your script, e.g.:

SET DATEFORMAT ymd;

SELECT ... WHERE [StartDate] = @StartDate

Upvotes: 0

Related Questions