Saturday, March 31, 2012

String was not recognized as a valid DateTime

I am getting the following error:

Server Error in '/Web' Application.

String was not recognized as a valid DateTime.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.FormatException: String was not recognized as a valid DateTime.

Source Error:

Line 103: decimal Reorderlevel = decimal.Parse(row["P_ReorderLevel"].ToString());Line 104: string Warehouseno = row["P_WareHouseNo"].ToString();Line 105: DateTime Modifieddate = DateTime.Parse(row["P_ModifiedDate"].ToString());Line 106: string Memo = row["P_Memo"].ToString();Line 107: string Desc = row["P_Desc"].ToString();


Source File:C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs Line:105

Stack Trace:

[FormatException: String was not recognized as a valid DateTime.] System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) +2271362 System.DateTime.Parse(String s) +22 WareHouse.DataLayer.DataObjects.SqlServer.SqlServerProductDao.GetProduct(Int32 ProductID) in C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs:105 WareHouse.BusinessLayer.Facade.ProductFacade.GetProduct(Int32 ProductID) in C:\IList_WareHouse_Solution\Facade\ProductFacade.cs:104[TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +0 System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) +72 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +358 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +29 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +17 System.Web.UI.WebControls.ObjectDataSourceView.InvokeMethod(ObjectDataSourceMethod method, Boolean disposeInstance, Object& instance) +676 System.Web.UI.WebControls.ObjectDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +2660 System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +84 System.Web.UI.WebControls.DataBoundControl.PerformSelect() +154 System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +99 System.Web.UI.WebControls.DetailsView.DataBind() +23 System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +91 System.Web.UI.WebControls.DetailsView.EnsureDataBound() +196 System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls() +101 System.Web.UI.Control.EnsureChildControls() +134 System.Web.UI.Control.PreRenderRecursiveInternal() +109 System.Web.UI.Control.PreRenderRecursiveInternal() +233 System.Web.UI.Control.PreRenderRecursiveInternal() +233 System.Web.UI.Control.PreRenderRecursiveInternal() +233 System.Web.UI.Control.PreRenderRecursiveInternal() +233 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4436

whenever I run

//get single product

publicProduct GetProduct(int ProductID)

{

StringBuilder sql =newStringBuilder();

sql.Append("usp_SelectProduct");

SqlParameter[] p =newSqlParameter[1];

p[0] =newSqlParameter("@dotnet.itags.org.P_Productid",SqlDbType.Int);

p[0].Value = ProductID;

DataRow row =Db.GetDataRow(sql.ToString(), p);

int productid =int.Parse(row["P_ProductID"].ToString());

string Productname = row["P_ProductName"].ToString();

decimal Qtyperunit =decimal.Parse(row["P_QtyPerUnit"].ToString());

decimal Unitprice =decimal.Parse(row["P_UnitPrice"].ToString());

decimal Unitsinstock =decimal.Parse(row["P_UnitsInStock"].ToString());

decimal Unitsonorder =decimal.Parse(row["P_UnitsOnOrder"].ToString());

decimal Reorderlevel =decimal.Parse(row["P_ReorderLevel"].ToString());

string Warehouseno = row["P_WareHouseNo"].ToString();

DateTime Modifieddate =DateTime.Parse(row["P_ModifiedDate"].ToString());

string Memo = row["P_Memo"].ToString();

string Desc = row["P_Desc"].ToString();

Product product;

return product =newProduct(productid, Productname, Qtyperunit, Unitprice, Unitsinstock, Unitsonorder, Reorderlevel, Warehouseno, Modifieddate, Memo, Desc);

}

Is there a problem in the way that I am converting my DateTime to string?

thanks

Nick

what is the data type of this:P_ModifiedDate
if it is a datetime, then you dont need to convert it to a string at all

try:

DateTime Modifieddate =Convert.ToDateTime(row["P_ModifiedDate"])


As the other poster said, if p_ModifiedDate is of type DateTime you don't need to user DateTime.Parse, neither convert it to string.

If it's in a string, then you don't need the .ToString method, however, the date has to be in the format appropriate to your thread's culture.

eg, if it's en-US it should be mm/dd/yyyy.


Ok, so DateTime is I guess a string type? I tried the

DateTime Modifieddate =Convert.ToDateTime(row["P_ModifiedDate"]);

and it generated another error saying:

Server Error in '/Web' Application.

Object cannot be cast from DBNull to other types.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 103: decimal Reorderlevel = decimal.Parse(row["P_ReorderLevel"].ToString());Line 104: string Warehouseno = row["P_WareHouseNo"].ToString();Line 105: DateTime Modifieddate = Convert.ToDateTime(row["P_ModifiedDate"]);Line 106: string Memo = row["P_Memo"].ToString();Line 107: string Desc = row["P_Desc"].ToString();


Source File:C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs Line:105

this is what my product.cs class looks like:

namespace WareHouse.BusinessLayer.BusinessObjects

{

[Serializable]

publicclassProduct

{

/**** FIELD PRIVATE ****************************************/

privateint _ProductID;

privatestring _ProductName;

privatedecimal _QtyPerUnit;

privatedecimal _UnitPrice;

privatedecimal _UnitsInStock;

privatedecimal _UnitsOnOrder;

privatedecimal _ReOrderLevel;

privatestring _WareHouseNo;

privateDateTime _ModifiedDate;

privatestring _Memo;

privatestring _Desc;

// overload with 10 arguemtns for SqlServerProductDao

public Product(int P_ProductID,string P_ProductName,decimal P_QtyPerUnit,decimal P_UnitPrice,decimal P_UnitsInStock,decimal P_UnitsOnOrder,decimal P_ReorderLevel,string P_WareHouseNo,DateTime P_ModifiedDate,string P_Memo,string P_Desc)

{

this._ProductID = P_ProductID;

this._ProductName = P_ProductName;

this._QtyPerUnit = P_QtyPerUnit;

this._UnitPrice = P_UnitPrice;

this._UnitsInStock = P_UnitsInStock;

this._UnitsOnOrder = P_UnitsOnOrder;

this._ReOrderLevel = P_ReorderLevel;

this._WareHouseNo = P_WareHouseNo;

this._ModifiedDate = P_ModifiedDate;

this._Memo = P_Memo;

this._Desc = P_Desc;

}

...

....

publicDateTime ModifiedDate

{

get

{

return _ModifiedDate;

}

set

{

_ModifiedDate =value;

}

}


Ok, so DateTime is I guess a string type? I tried the

DateTime Modifieddate =Convert.ToDateTime(row["P_ModifiedDate"]);

and it generated another error saying:

Server Error in '/Web' Application.

Object cannot be cast from DBNull to other types.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.InvalidCastException: Object cannot be cast from DBNull to other types.

Source Error:

Line 103: decimal Reorderlevel = decimal.Parse(row["P_ReorderLevel"].ToString());Line 104: string Warehouseno = row["P_WareHouseNo"].ToString();Line 105: DateTime Modifieddate = Convert.ToDateTime(row["P_ModifiedDate"]);Line 106: string Memo = row["P_Memo"].ToString();Line 107: string Desc = row["P_Desc"].ToString();


Source File:C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs Line:105

this is what my product.cs class looks like:

namespace WareHouse.BusinessLayer.BusinessObjects

{

[Serializable]

publicclassProduct

{

/**** FIELD PRIVATE ****************************************/

privateint _ProductID;

privatestring _ProductName;

privatedecimal _QtyPerUnit;

privatedecimal _UnitPrice;

privatedecimal _UnitsInStock;

privatedecimal _UnitsOnOrder;

privatedecimal _ReOrderLevel;

privatestring _WareHouseNo;

privateDateTime _ModifiedDate;

privatestring _Memo;

privatestring _Desc;

// overload with 10 arguemtns for SqlServerProductDao

public Product(int P_ProductID,string P_ProductName,decimal P_QtyPerUnit,decimal P_UnitPrice,decimal P_UnitsInStock,decimal P_UnitsOnOrder,decimal P_ReorderLevel,string P_WareHouseNo,DateTime P_ModifiedDate,string P_Memo,string P_Desc)

{

this._ProductID = P_ProductID;

this._ProductName = P_ProductName;

this._QtyPerUnit = P_QtyPerUnit;

this._UnitPrice = P_UnitPrice;

this._UnitsInStock = P_UnitsInStock;

this._UnitsOnOrder = P_UnitsOnOrder;

this._ReOrderLevel = P_ReorderLevel;

this._WareHouseNo = P_WareHouseNo;

this._ModifiedDate = P_ModifiedDate;

this._Memo = P_Memo;

this._Desc = P_Desc;

}

...

....

publicDateTime ModifiedDate

{

get

{

return _ModifiedDate;

}

set

{

_ModifiedDate =value;

}

}


The date you fetch from DB might be null, Try this:

DateTime Modifieddate;object Modifieddate_obj = row["P_ModifiedDate"];if(Modifieddate!= System.DBNull.Value){ Modifieddate = Convert.ToDateTime();}


I tried the following:

DateTime Modifieddate;object Modifieddate_obj = row["P_ModifiedDate"];if(Modifieddate!= System.DBNull.Value){ Modifieddate = Convert.ToDateTime();}

and got errors saying:

Error 11 Operator '!=' cannot be applied to operands of type 'System.DateTime' and 'System.DBNull' C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs 107 17 DataObjects
Error 12 No overload for method 'ToDateTime' takes '0' arguments C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs 109 32 DataObjects

Next I tried:

object Modifieddate_obj = row["P_ModifiedDate"];
if (Modifieddate_obj != System.DBNull.Value)
{
Modifieddate = Convert.ToDateTime();
}

and I still got errors saying:

Error 11 No overload for method 'ToDateTime' takes '0' arguments C:\IList_WareHouse_Solution\DataObjects\SqlServer\SqlServerProductDao.cs 109 32 DataObjects

thanks

nick


Make sure you are getting some value in P_ModifiedDate and it is in the correct date format. If so, then you can convert it to your needs. And also Convert.ToDateTime takes a parameter, it should be used like this : Modifieddate = Convert.ToDateTime(Modifieddate_obj);

Thanks

0 comments:

Post a Comment