I have problem with asp.net editing result of select from database. Everything is okay so far, i can dispay result of my select. But if i try edit one row i get InvalidOperationException. Cannot convert Datum_doručení from System.String to System.DateTime. If i remove DateTime row from asp.net file it work again fine..
EDIT:
just end up with debugging, in sql my field "Datum_doručení" have value "2015-04-30 00:00:00.0000000", same as result of select in asp.net. But if i try edit it, value of "Datum_doručeni" is "1.1.0001 0:00:00" and it will overflow because range. Why this is happening ?
EDIT2: I fix it with kind of hack solution. Using just string. But still i don't know why datetime does not work.
public DateTime Datum_doručení { get; set; } changed to public string Datum_D { get; set; } ---- s.Datum_doručení = reader.GetDateTime(4); changed to s.Datum_D = reader.GetDateTime(4).ToString("yyyy-MM-dd HH:mm:ss.fff");
and
command.Parameters.Add("@Datum_doručení", System.Data.SqlDbType.DateTime).Value = zásilka.Datum_doručení; to command.Parameters.Add("@Datum_doručení", System.Data.SqlDbType.DateTime2).Value = DateTime.Parse(zásilka.Datum_D);
My class:
public class Zásilka
{
public int zásilka_id { get; set; }
public decimal objednavka_id { get; set; }
public decimal zakaznik_id { get; set; }
public decimal zaměstnanec_id { get; set; }
public DateTime Datum_doručení { get; set; }
public string Stav { get; set; }
public decimal Váha { get; set; }
}
ClassTable:
public class ZásilkaTable
{
private string selectAllDelivery = "select * from zásilka";
private string editDelivery = "UPDATE zásilka SET objednavka_id=@objednavka_id, zakaznik_id=@zakaznik_id, zaměstnanec_id=@zaměstnanec_id, Datum_doručení=@Datum_doručení, Stav=@Stav, Váha=@Váha WHERE zásilka_id=@zásilka_id";
public Collection<ORM.Zásilka> SelectAllDelivery()
{
Collection<ORM.Zásilka> delivery = null;
Database db = new Database();
SqlDataReader reader = null;
try
{
db.Connect();
SqlCommand command = db.CreateCommand(selectAllDelivery);
reader = db.Select(command);
delivery = new Collection<Zásilka>();
while (reader.Read())
{
Zásilka s = new Zásilka();
s.zásilka_id = reader.GetInt32(0);
s.objednavka_id = reader.GetDecimal(1);
s.zakaznik_id = reader.GetDecimal(2);
s.zaměstnanec_id = reader.GetDecimal(3);
s.Datum_doručení = reader.GetDateTime(4);
s.Stav = reader.GetString(5);
s.Váha = reader.GetDecimal(6);
delivery.Add(s);
}
}
catch (Exception e)
{
throw;
}
finally
{
reader.Close();
db.Close();
}
return delivery;
}
public string Update(Zásilka zásilka)
{
Database db = new Database();
try
{
SqlCommand command = db.CreateCommand(editDelivery);
command.Parameters.Add("@zásilka_id", System.Data.SqlDbType.Int).Value = zásilka.zásilka_id;
command.Parameters.Add("@objednavka_id", System.Data.SqlDbType.Decimal).Value = zásilka.objednavka_id;
command.Parameters.Add("@zakaznik_id", System.Data.SqlDbType.Decimal).Value = zásilka.zakaznik_id;
command.Parameters.Add("@zaměstnanec_id", System.Data.SqlDbType.Decimal).Value = zásilka.zaměstnanec_id;
command.Parameters.Add("@Datum_doručení", System.Data.SqlDbType.DateTime).Value = zásilka.Datum_doručení;
command.Parameters.Add("@Stav", System.Data.SqlDbType.VarChar).Value = zásilka.Stav;
command.Parameters.Add("@Váha", System.Data.SqlDbType.Decimal).Value = zásilka.Váha;
db.Connect();
command.ExecuteNonQuery();
}
catch (Exception e)
{
return e.Message;
}
finally
{
db.Close();
}
return "OK";
}
}
ASP.NET:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="Zásilka.aspx.cs" Inherits="dais.Zásilka" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<center>
<h1>Seznam zásilek</h1>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSourceadAS" DataKeyNames="zásilka_id">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="zásilka_id" HeaderText="zásilka_id" SortExpression="zásilka_id" />
<asp:BoundField DataField="objednavka_id" HeaderText="objednavka_id" SortExpression="objednavka_id" />
<asp:BoundField DataField="zakaznik_id" HeaderText="zakaznik_id" SortExpression="zakaznik_id" />
<asp:BoundField DataField="zaměstnanec_id" HeaderText="zaměstnanec_id" SortExpression="zaměstnanec_id" />
<asp:BoundField DataField="Datum_doručení" HeaderText="Datum_doručení" SortExpression="Datum_doručení" ConvertEmptyStringToNull="False" />
<asp:BoundField DataField="Stav" HeaderText="Stav" SortExpression="Stav" />
<asp:BoundField DataField="Váha" HeaderText="Váha" SortExpression="Váha" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSourceadAS" runat="server" DataObjectTypeName="dais.ORM.Zásilka" SelectMethod="SelectAllDelivery" TypeName="dais.ORM.ZásilkaTable" UpdateMethod="Update"></asp:ObjectDataSource>
</center>
</asp:Content>
And in sql database:
CREATE TABLE [zásilka]
(
[zásilka_id] INT NOT NULL,
[objednavka_id] Decimal(10,0) NOT NULL,
[zakaznik_id] Decimal(10,0) NOT NULL,
[zaměstnanec_id] Decimal(10,0) NOT NULL,
[Datum_doručení] Datetime NOT NULL,
[Stav] Varchar(2) NOT NULL,
[Váha] Decimal(10,0) NOT NULL
)
go
Exact error
Podrobnosti o výjimce: System.InvalidOperationException: Nelze převést hodnotu parametru Datum_doručení z System.String na System.DateTime.
Zdrojová chyba:
Při provádění aktuálního webového požadavku byla vyvolána neošetřená výjimka. Informace týkající se původu a umístění výjimky lze zjistit pomocí níže uvedeného trasování zásobníku výjimek.
Trasování zásobníku:
[InvalidOperationException: Nelze převést hodnotu parametru Datum_doručení z System.String na System.DateTime.]
System.Web.UI.WebControls.ObjectDataSourceView.ConvertType(Object value, Type type, String paramName, ParsingCulture parsingCulture) +416
System.Web.UI.WebControls.ObjectDataSourceView.BuildObjectValue(Object value, Type destinationType, String paramName, ParsingCulture parsingCulture) +166
System.Web.UI.WebControls.ObjectDataSourceView.BuildDataObject(Type dataObjectType, IDictionary inputParameters) +210
System.Web.UI.WebControls.ObjectDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +1276
System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +87
System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +1210
System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +738
System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9664586
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724