views:

2120

answers:

3

I have the following code:

try
{
    //Create connection
    SQLiteConnection conn = DBConnection.OpenDB();

    //Verify user input, normally you give dbType a size, but Text is an exception
    var uNavnParam = new SQLiteParameter("@uNavnParam", SqlDbType.Text) { Value = uNavn }; 
    var bNavnParam = new SQLiteParameter("@bNavnParam", SqlDbType.Text) { Value = bNavn };
    var passwdParam = new SQLiteParameter("@passwdParam", SqlDbType.Text) {Value = passwd};
    var pc_idParam = new SQLiteParameter("@pc_idParam", SqlDbType.TinyInt) { Value = pc_id };
    var noterParam = new SQLiteParameter("@noterParam", SqlDbType.Text) { Value = noter };
    var licens_idParam = new SQLiteParameter("@licens_idParam", SqlDbType.TinyInt) { Value = licens_id };

    var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
    "VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam')", conn);
    insertSQL.Parameters.Add(uNavnParam); //replace paramenter with verified userinput
    insertSQL.Parameters.Add(bNavnParam);
    insertSQL.Parameters.Add(passwdParam);
    insertSQL.Parameters.Add(pc_idParam);
    insertSQL.Parameters.Add(noterParam);
    insertSQL.Parameters.Add(licens_idParam);
    insertSQL.ExecuteNonQuery(); //Execute query

    //Close connection
    DBConnection.CloseDB(conn);

    //Let the user know that it was changed succesfully
    this.Text = "Succes! Changed!";
}
catch(SQLiteException e)
{
    //Catch error
    MessageBox.Show(e.ToString(), "ALARM");
}

It executes perfectly, but when I view my "brugere" table, it has inserted the values: '@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam' literally. Instead of replacing them.

I have tried making a breakpoint and checked the parameters, they do have the correct assigned values. So that is not the issue either.

I have been tinkering with this a lot now, with no luck, can anyone help?

Oh and for reference, here is the OpenDB method from the DBConnection class:

public static SQLiteConnection OpenDB()
{
    try
    {
        //Gets connectionstring from app.config
        const string myConnectString = "data source=data;";

        var conn = new SQLiteConnection(myConnectString);
        conn.Open();
        return conn;
    }

    catch (SQLiteException e)
    {
        MessageBox.Show(e.ToString(), "ALARM");
        return null;
    }
}
+11  A: 

I think you should remove the quotes around your parameter names in the INSERT statement.

So instead of

VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam',
        '@noterParam', '@licens_idParam')

use

VALUES (@uNameParam, @bNavnParam, @passwdParam, @pc_idParam,
        @noterParam, @licens_idParam)
Ronald Wildenberg
Sorry, no, I tried that as well. It would give the following error: "Insufficient parameters supplied to the command"
CasperT
Ah, I see your problem. Not all parameter names are the same: @uNavnParam versus @uNameParam. It's a simple typo :)
Ronald Wildenberg
And you should still remove the single quotes around your parameters.
Ronald Wildenberg
Yeah you are right, so the fuss was just a stupid typo :(("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id) VALUES (@uNavnParam, @bNavnParam, @passwdParam, @pc_idParam, @noterParam, @licens_idParam)", conn); = Wrong input error. I hope it is not a another silly typo.
CasperT
This is probably something else. You could try to find out what the exact SQL statement is that is sent to your database and check the error message that is generated if you execute the statement directly.
Ronald Wildenberg
If I keep all the single quotes, it works, but it inserts the @ values literally. If I don't, it seems like it tries to send:INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id) VALUES (@uNavnParam, @bNavnParam, @passwdParam, @pc_idParam, @noterParam, @licens_idParamAs query:(
CasperT
Then it must be the value of one or more of your parameters. Are you sure that the data type for each column is correct? Both @pc_idParam and @licens_idParam are TinyInt's, so it's weird that your database accepts the values '@pc_idParam' and '@licens_idParam'.
Ronald Wildenberg
Check the data types of your parameters, if not sure use insertSQL.Parameters.AddWithValue("@ParameterName", parameterValue); instead of creating the parameters and adding later.
Jorge Villuendas
A: 

Thanks to rwwilden and Jorge Villuendas, the answer is:

var insertSQL = new SQLiteCommand("INSERT INTO Brugere (navn, brugernavn, password, pc_id, noter, licens_id)" +
" VALUES (@uNavnParam, @bNavnParam, @passwdParam, @pc_idParam, @noterParam, @licens_idParam)", conn);
insertSQL.Parameters.AddWithValue("@uNavnParam", uNavn);
insertSQL.Parameters.AddWithValue("@bNavnParam", bNavn);
insertSQL.Parameters.AddWithValue("@passwdParam", passwd);
insertSQL.Parameters.AddWithValue("@pc_idParam", pc_id);
insertSQL.Parameters.AddWithValue("@noterParam", noter);
insertSQL.Parameters.AddWithValue("@licens_idParam", licens_id);

insertSQL.ExecuteNonQuery(); //Execute query
CasperT
A: 

replace

VALUES ('@uNameParam', '@bNavnParam', '@passwdParam', '@pc_idParam', '@noterParam', '@licens_idParam')

with

VALUES (?, ?, ?, ?, ?, ?)

Stark