tags:

views:

44

answers:

1

Hello, this is a weird one, when I run the following code all rows are returned from the db. Imagaine what would happen if this was an update or delete.

    Dim cmd As New NpgsqlCommand

    cmd.Connection = conn
    cmd.CommandText = "select * FROM ac_profiles WHERE profileid = @profileId"
    cmd.Parameters.Add("@profile", 58)
    Dim dt As DataTable = DataAccess2.DataAccess.sqlQueryDb(cmd)

    DataGridView1.DataSource = dt

My question is why is this happening?

+2  A: 

I'm no pg-sql expert, but I strongly suspect it's because you're adding a different parameter from the one you're using in the SQL statement. I think you're also using the wrong syntax to refer to a parameter. See the user manual for more information. Try this:

cmd.Connection = conn
cmd.CommandText = "select * FROM ac_profiles WHERE profileid = :profileid"
cmd.Parameters.Add("profileid", 58)
Dim dt As DataTable = DataAccess2.DataAccess.sqlQueryDb(cmd)
Jon Skeet
Your correct, I just noticed that, however you can't use : in the latest version - you get ERROR: 42601: syntax error at or near ":"This looks like a very dangerious bug to me.
Mr Shoubs
Forget the : or @, both work. the issue is the name, it is case sensative too.
Mr Shoubs
The problem appears to be using @, this is bad - you should use :.If you get something wrong like a parameter name it will give you a syntax error when using :, however if you use @ then it won't and returns all rows, @ is also case sensative.
Mr Shoubs
This is strange. Npgsql has code to provide parameter name case insensitivity. With Npgsql 2.0.9 if you enable logging you can see what Npgsql is sending to the server. Also, note that using Parameters.Add() is dangerous as the value of the parameter can be mistakenly by the DbType. In order to avoid that, MS created the AddWithValue method and we have this method also in NpgsqlParameterCollection. Check this change log of the class: http://cvs.pgfoundry.org/cgi-bin/cvsweb.cgi/npgsql/Npgsql2/src/Npgsql/NpgsqlParameterCollection.cs version 1.10 I hope it helps.
Francisco