Connecting Tech Pros Worldwide Help | Site Map

PostgreSQL object datatype

Newbie
 
Join Date: Jun 2008
Posts: 2
#1: Jul 1 '08
Hi,

I'm having a problem, I'm using PostgreSQL V8.3.3-1, Npgsql and Visual studio 2008 c#.

I'm passed a dictionary of key, value (key been a string, value been an object). The code works fine with key and value both been strings but postgresql doesn't seem to like it when the value is an object. How can I correct my code to work when passed an object? I realise that rowsaffected is currently only storing the first value, that's fine. I just want to be able to run a query using an object datatype for now.

current error is: "InvalidCastException is unhandled by user code"

sql is a query
Expand|Select|Wrap|Line Numbers
  1. "SELECT username FROM users WHERE age = :Value1"
Expand|Select|Wrap|Line Numbers
  1.         public static string GetSql(string sql, Dictionary<string, object> d)
  2.         {
  3.             string connection = System.Configuration.ConfigurationManager.ConnectionStrings["POSTGRESQL_LOCAL"].ConnectionString;
  4.  
  5.             using (NpgsqlConnection conn = new NpgsqlConnection(connection))
  6.             {
  7.                 conn.Open();
  8.                 using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
  9.                 {
  10.                     int i = 0;
  11.  
  12.                     foreach (KeyValuePair<string, object> kvp in d)
  13.                     {
  14.                         command.Parameters.Add(new NpgsqlParameter(kvp.Key, DbType.Object));
  15.                         command.Parameters[i].Value = kvp.Value;
  16.                         i++;
  17.                     }
  18.                     string rowsaffected = "error: No data";
  19.                     using (NpgsqlDataReader dr = command.ExecuteReader())
  20.                     {
  21.                         while (dr.Read())
  22.                         {
  23.                             rowsaffected = dr[0].ToString();
  24.                         }
  25.                     }
  26.  
  27.                     conn.Close();
  28.                     return rowsaffected;
  29.  
  30.                 }
  31.  
  32.             }
  33.  
  34.         }
I hope I placed this on the correct forum. Most of the code is c# but I feel the problem lies with Postgresql...

Thanks
Reply