Insert commando doesn''t works

  • Hi for all

    In my MS Access, when I perfom a insert operation, the folling message is showed:

    "Any value has provided for one or more needed parameters"

    I'm inserting values with a method created in C#. Below the method:

    public void InsertNewArtist(string sNomCD)

    {

     string sInsert = "insert into Artistas (nome_cd)" + "values(sNomCD)";

     try

     {

      OleDbConnection OleConnect = new OleDbConnection(m_sConnection);

      OleDbCommand OleCommand = new OleDbCommand(sInsert, OleConnect);

      OleCommand.Connection.Open();

      OleCommand.ExecuteNonQuery();

      OleConnect.Close();

     }

     catch(OleDbException ex)

     {

      MessageBox.Show(ex.Message);

     }

    }

    My connection string:

    private string m_sConnection = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Alex Cutovoi\Meus documentos\CDS.mdb;" + @"User ID = Admin;Password=";

    I think that the connection is correct. So, what's going on?

    Thanks a lot

  • Can you check the column name of the insert table. Most likely, the column name does not match.

    But I may be wrong...

  • Shouldn't this

    "insert into Artistas (nome_cd)" + "values(sNomCD)"

    be

    "insert into Artistas (nome_cd) values('" + sNomCD + "')"

    You are inserting the string sNomCD and not the contents of that variable and it should be in single quotes cos its a string?

  • I have to agree.  You have not concatenated the string that was passed into the function.  As a general rule, it is best to print your sql strings to the screen to make sure you are getting what you expect.  it takes only a moment but saves a lot of time.  I know this from experience. 

    Kindest Regards,
    David

    ** Obstacles are those frightening things that appear when we take our eyes off the goal. **

  • Even better to use stored procs .

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply