Reputation: 1410
I'm trying to insert a row in a PostgreSQL table with a date column. On the UI, I got a DateTimePicker where the user selectes the proper date. So far I got this:
On the UI:
objPresupuesto.date = this.dtpFechaPres.Value.ToString("yyyy-MM-dd");
On the method who inserts the row:
NpgsqlCommand query = new NpgsqlCommand("insert into presupuesto(presupuesto, codigo, descripcion, fecha, cliente, proyecto, total) values(nextval('presupuesto_presupuesto_seq'), @codigo, @descripcion, @fecha, @cliente, @proyecto, @total);Select lastval();", conn);
...
query.Parameters.Add(new NpgsqlParameter("fecha", NpgsqlDbType.Date, 0, "fecha"));
...
query.Parameters[2].Value = obj.date.toString;//without the toString it also fails
It throws this exception:
Specified cast is not valid.
The obj.date value is 2011-04-29. tryied putting single quotes around, but It also fails.
The database column type is date.
Anyone has done this before? Any ideas?
I checked this link searching for and aswer but it doesn't helped. Thanks
Upvotes: 0
Views: 1928
Reputation: 260
npgsqlCommand.Parameters.Add("fecha", NpgsqlDbType.Date).Value = DateTime.Now;
For more readable code.
Type of obj.date
is DateTime struct?
Add to connection string parameter Convert Infinity DateTime
For more info check: http://www.npgsql.org/doc/connection-string-parameters.html
Upvotes: 1
Reputation: 11
The first thing I noticed is that when you are adding your parameter it is missing the "@" from the beginning where your command references the value with the "@".
Are you sure that the Parameters[2] is your "fecha" parameter?
Upvotes: 1