tallman
tallman

Reputation: 71

command.parameters.addwithvalue not working

I have experienced a problem when saving a record using parameters the following is my code

try
{
    string usd = "USR" + randomnumber(001, 1000).ToString();
    dbconn.poconne.Open();
    string useradd = "insert into users values(@userid,@username,@password,@email,@department,@log)";

    using (NpgsqlCommand cmdadd = new NpgsqlCommand(useradd, dbconn.poconne))
    {
        cmdadd.Parameters.AddWithValue("@userid", usd);
    }
}
catch (Exception er)
{
    MessageBox.Show(er.Message);
}

I get this error

Error 1 'Npgsql.NpgsqlParameterCollection' does not contain a definition for 'AddWithValue' and no extension method 'AddWithValue' accepting a first argument of type 'Npgsql.NpgsqlParameterCollection' could be found (are you missing a using directive or an assembly reference?) C:\Users\ICT PGL\Documents\Visual Studio 2010\Projects\officeautomation\customercare\customercare\users.cs 34 39 customercare

Any help on solving the error please

Upvotes: 2

Views: 3346

Answers (1)

Khazratbek
Khazratbek

Reputation: 1656

Your query:

string useradd = "insert into users (id, name, password, email, department, log) values(:userid,:username,:password,:email,:department,:log)";

Your parameters:

cmd.Parameters.Add(new NpgsqlParameter("userid", NpgsqlTypes.NpgsqlDbType.Int));
cmd.Parameters.Add(new NpgsqlParameter("username", NpgsqlTypes.NpgsqlDbType.Text));
cmd.Parameters[0].Value = usd;
cmd.Parameters[1].Value = "username";

I showed just for two parameters. Add other parameters accordingly.

It is better if you appoint the columns where you are going to insert data.

Upvotes: 2

Related Questions