1

I'm trying to save winforms input to firebird database using following code

string connString = ConfigurationManager.AppSettings["ConnectionString"];
            FbConnection fbConn = new FbConnection(connString);
            fbConn.Open();
            FbTransaction tran = fbConn.BeginTransaction();

            try
            {                
                string insertCmdStr = "INSERT INTO ARTICLES(ID,Group,Code,Name) ";
                insertCmdStr += "VALUES(@id,@group,@code,@name)";
                FbCommand addDataCmd = new FbCommand(insertCmdStr, fbConn, tran);                

                addDataCmd.ExecuteNonQuery();                   
            }
            catch (FbException ex)
            {
                tran.Rollback();
                fbConn.Close();
                throw new Exception(ex.Message);
            }

            tran.Commit();
            fbConn.Close();

After more that one minute (?) I'm getting following error:

Must declare command parameters

1 Answer 1

3

The command text (insertCmdStr) contains the placeholders for 4 parameters (@id...) , but you don't add any parameter to the command object, thus the error.
To fix it, you need to know what values to give for every parameter and add that parameter to the Parameters collection of the command.

As an example, you should have this code, (but use your variables for the value part)

string insertCmdStr = "INSERT INTO ARTICLES(ID,Group,Code,Name) ";
insertCmdStr += "VALUES(@id,@group,@code,@name)";
FbCommand addDataCmd = new FbCommand(insertCmdStr, fbConn, tran);                
addDataCmd.Parametes.AddWithValue("@id", variable_containing_the_id_value);
addDataCmd.Parametes.AddWithValue("@group",variable_containing_the_group_value);
addDataCmd.Parametes.AddWithValue("@code",variable_containing_the_code_value);
addDataCmd.Parametes.AddWithValue("@name",variable_containing_the_name_value);
addDataCmd.ExecuteNonQuery(); 

Also, keep in mind that if the id column is an identity column, then you should not pass any value for that field, remove the reference to the ID field in the column list and also the placeholder and the parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.