Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-30-2008, 08:40 PM
pykie101 pykie101 is offline
Newbie
 
Join Date: Jun 2008
Posts: 2
Rep Power: 0
pykie101 is on a distinguished road
Default C# objects and Postgresql

Hi, this is my first post, I'm pretty new to all this stuff, infact I haven't really tried programming in 4 years so please excuse if this is easy because for me it's stupid hard .... anyway

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.
I feel DbType.Object might be the problem.

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

sql is a query

"SELECT username FROM users WHERE age = :Value1"

Code:
public static string GetSql(string sql, Dictionary<string, object> d)
        {
            string connection = System.Configuration.ConfigurationManager.Connecti  onStrings["POSTGRESQL_LOCAL"].ConnectionString;
            
            using (NpgsqlConnection conn = new NpgsqlConnection(connection))
            {
                conn.Open();
                using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
                {
                    int i = 0;
 
                    foreach (KeyValuePair<string, object> kvp in d)
                    {
                        command.Parameters.Add(new NpgsqlParameter(kvp.Key, DbType.Object));
                        command.Parameters[i].Value = kvp.Value;
                        i++;
                    }
                    string rowsaffected = "error: No data";
                    using (NpgsqlDataReader dr = command.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            rowsaffected = dr[0].ToString();
                        }
                    }
 
                    conn.Close();
                    return rowsaffected;
 
                }
 
            }
            
        }
I hope I placed this on the correct forum.

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-30-2008, 08:44 PM
pykie101 pykie101 is offline
Newbie
 
Join Date: Jun 2008
Posts: 2
Rep Power: 0
pykie101 is on a distinguished road
Default Re: C# objects and Postgresql

For clarity, when I say both key and value are strings I mean I change the datatype to string for the value as well.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-30-2008, 09:10 PM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 130
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 has a spectacular aura aboutgaylo565 has a spectacular aura about
Default Re: C# objects and Postgresql

I haven't used protege but I know that ms sql doesn't support object type data. You need to use something like Sql_Variant (a nondescript data type.) Most SQL is pretty similar so I'm guessing that that is your problem.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 10:17 AM.

Contest Stats

John ........ 203.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 63%

Ads