Cheluis
Cheluis

Reputation: 1410

Problems with Data type in Npgsql query

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

Answers (3)

KondzioSSJ4
KondzioSSJ4

Reputation: 260

  1. try use:

npgsqlCommand.Parameters.Add("fecha", NpgsqlDbType.Date).Value = DateTime.Now;

For more readable code.

  1. Type of obj.date is DateTime struct?

  2. Add to connection string parameter Convert Infinity DateTime For more info check: http://www.npgsql.org/doc/connection-string-parameters.html

Upvotes: 1

Fil
Fil

Reputation: 1060

query.Parameters[2].Value = CDate(obj.date)

Upvotes: 0

user161
user161

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

Related Questions