Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problen : Insert Into Access database

Problen : Insert Into Access database

Scheduled Pinned Locked Moved C#
database
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ravindra Bisen
    wrote on last edited by
    #1

    A am using access mdb database and using obleDB class. when i run insert query on this table it runs successfully but record does not appear into data table. i am using .. objConnection1.Open(); objCommand = new OleDbCommand(Sqlstr, objConnection1); Data = objCommand.ExecuteNonQuery(); // it gives data = 1 my connection is ok because i am getting value using this connection when i use Executescalar() on this command object why it is.....

    M L 2 Replies Last reply
    0
    • R Ravindra Bisen

      A am using access mdb database and using obleDB class. when i run insert query on this table it runs successfully but record does not appear into data table. i am using .. objConnection1.Open(); objCommand = new OleDbCommand(Sqlstr, objConnection1); Data = objCommand.ExecuteNonQuery(); // it gives data = 1 my connection is ok because i am getting value using this connection when i use Executescalar() on this command object why it is.....

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Whats your query string? i.e. the value of Sqlstr

      Life goes very fast. Tomorrow, today is already yesterday.

      R 1 Reply Last reply
      0
      • R Ravindra Bisen

        A am using access mdb database and using obleDB class. when i run insert query on this table it runs successfully but record does not appear into data table. i am using .. objConnection1.Open(); objCommand = new OleDbCommand(Sqlstr, objConnection1); Data = objCommand.ExecuteNonQuery(); // it gives data = 1 my connection is ok because i am getting value using this connection when i use Executescalar() on this command object why it is.....

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Because you're executing a non-query[^]. As the documentation states;

        Executes a Transact-SQL statement against the connection and returns the number of rows affected.

        Your query affected 1 row, and that's what the function returns. Use a DataReader te read your row :)

        I are troll :)

        R 1 Reply Last reply
        0
        • M musefan

          Whats your query string? i.e. the value of Sqlstr

          Life goes very fast. Tomorrow, today is already yesterday.

          R Offline
          R Offline
          Ravindra Bisen
          wrote on last edited by
          #4

          string Sqlstr = "INSERT INTO student(stu_id,stu_fnm) VALUES (" + textBox1.Text.Trim() + ",'" + textBox2.Text.Trim() + "') "; // it is inserting record when i run it into sql pane . and objConnection1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=score.mdb");// is connection string // i tried same for sql server database it works fine // but i want to do it for access..

          M 1 Reply Last reply
          0
          • L Lost User

            Because you're executing a non-query[^]. As the documentation states;

            Executes a Transact-SQL statement against the connection and returns the number of rows affected.

            Your query affected 1 row, and that's what the function returns. Use a DataReader te read your row :)

            I are troll :)

            R Offline
            R Offline
            Ravindra Bisen
            wrote on last edited by
            #5

            but i want to insert record into table .

            L 1 Reply Last reply
            0
            • R Ravindra Bisen

              string Sqlstr = "INSERT INTO student(stu_id,stu_fnm) VALUES (" + textBox1.Text.Trim() + ",'" + textBox2.Text.Trim() + "') "; // it is inserting record when i run it into sql pane . and objConnection1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=score.mdb");// is connection string // i tried same for sql server database it works fine // but i want to do it for access..

              M Offline
              M Offline
              musefan
              wrote on last edited by
              #6

              well your query string look ok, apart from the fact you should be using command parameters to insert data from textBoxes. Is a row being created in the database at all? with just blank data?

              Life goes very fast. Tomorrow, today is already yesterday.

              1 Reply Last reply
              0
              • R Ravindra Bisen

                but i want to insert record into table .

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Aw, I thought you wanted to insert your rows into a DataTable object. I should learn to read decently before answering a post. In that case the ExecuteNonQuery is correct, my apologies. That would return "1" if it added a record. You might want to use parameters in your Insert-statement, that would also prevent some problems with SQL Injection and quoted strings. Take a look at OleDbParameter :)

                I are troll :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups