CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2007
    Posts
    84

    OleDbCommand problem

    hi,
    i have a code that i cant make it work
    Code:
    string myName = "homerfavenir";
                string source = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=user.mdb";
                string input = @"INSERT INTO Users (firstname) values (" + myName + ")";
                OleDbConnection conn = new OleDbConnection(source);
                conn.Open();
    
                // Do something useful
    
                OleDbCommand cmd = new OleDbCommand(input,conn);
                cmd.ExecuteNonQuery();
                conn.Close();
    error
    Code:
    No value given for one or more required parameters.
    thanks

  2. #2
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: OleDbCommand problem

    I think that myName shoud be qouted in SQL to get something like INSERT INTO Users (firstname) values ('myName')

    Try
    Code:
       string input = @"INSERT INTO Users (firstname) values ('" + myName + "')";
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Oct 2007
    Posts
    84

    Re: OleDbCommand problem

    thanks!
    ok, but when the INSERT is success, i need to close the application and open it again to update the table and see the record.
    how can i refresh it so that when i insert a record it will show automatically

    tia

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: OleDbCommand problem

    ok, but when the INSERT is success, i need to close the application and open it again to update the table and see the record.
    how can i refresh it so that when i insert a record it will show automatically
    How about reading the information from the table using another command before you close the connection? The SQL should be simple
    Code:
    "SELECT * FROM Users"
    How you read the data depends on how you've structured your application and how you want to display the data. The two simple options are using a OleDbDataReader and custom class representing the user or using OleDbDataAdapter and a DataSet. I generally go for the former approach but second is equally valid.
    Using OleDBDataReader
    Using OleDbDataAdapter
    These links should get you going.
    Last edited by nelo; January 5th, 2010 at 11:05 AM. Reason: Correctness...

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: OleDbCommand problem

    It is good practice to enumare all required columns in select clause rather than using "*" because it is more robust forward schema changes.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Oct 2007
    Posts
    84

    Re: OleDbCommand problem

    thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured