Adding data to a database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pachalo
    New Member
    • Sep 2008
    • 3

    Adding data to a database

    I would like to add data to a database using text Boxes and l should be able to retrieve that data into a datagrid

    So far l have managed to create a connection between a datagrid and the data base but am failling to have that connection between the text boxes and the database.


    So far this is the scenerio:

    l have an
    OleDbConnection
    an OleDbDataAdapte r
    a dataset named DsSession
    an Access database named Try and its location is C:\try\try.mdb
    a table named Session

    in the table l have the following fields:
    Computer Number
    Start time
    Amount Per second
    End time
    Total amount Total time

    On the form l have the following text boxes that corresponds to respective columns of the database

    txtComputerNumb er
    txtStart
    txtAmount
    txtEndTime
    txttime

    so l would like using these textBoxes to add data in the database in their respective columns..
    please help
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    You have to write the SQL query if you want to add the data from the textboxes on form into the database.

    Steps for executing the SQl query are as follows :

    1> Write the sql query using .Net code
    Code:
    Dim Sql
    Sql = "Insert into Session(
    Computer Number
    Start time
    Amount Per second
    End time
    Total amount Total time)
    Values
    (
    
    " & txtComputerNumber &"
    " &txtStart &"
    " & txtAmount &"
    " & txtEndTime &"
    " & txttime &"
    )
    2> Execute above defined sql query using OledbCommand Object.
    For this define oledbcommand object and its properties.

    Code:
    Dim objCommand as new oledbCommand()
    
    ''Define the properties of oledbcommand object.
    
    objCommand.connection = conn
    objCommand.CommandText = SQL
    objCommand.CommandType = Text
    objCommand.ExecuteNonQuery
    3> When step 2 code is run data from above textboxes is inserted into the database.


    Originally posted by Pachalo
    I would like to add data to a database using text Boxes and l should be able to retrieve that data into a datagrid

    So far l have managed to create a connection between a datagrid and the data base but am failling to have that connection between the text boxes and the database.


    So far this is the scenerio:

    l have an
    OleDbConnection
    an OleDbDataAdapte r
    a dataset named DsSession
    an Access database named Try and its location is C:\try\try.mdb
    a table named Session

    in the table l have the following fields:
    Computer Number
    Start time
    Amount Per second
    End time
    Total amount Total time

    On the form l have the following text boxes that corresponds to respective columns of the database

    txtComputerNumb er
    txtStart
    txtAmount
    txtEndTime
    txttime

    so l would like using these textBoxes to add data in the database in their respective columns..
    please help

    Comment

    • PRR
      Recognized Expert Contributor
      • Dec 2007
      • 750

      #3
      you need to insert from textbox to db? in that case you need to have a insert query...
      do have a look at this:
      Database
      i guess this is clarify all your queries...

      Comment

      • Pachalo
        New Member
        • Sep 2008
        • 3

        #4
        l have tried the above code but i can not be abble to view the results since am having a Syntax error under the INSERT INTO SESSION.. the error reads Character constant must contain exactly one character.
        so l dont know what this error is trying to communicate across please help


        Originally posted by shweta123
        Hi,

        You have to write the SQL query if you want to add the data from the textboxes on form into the database.

        Steps for executing the SQl query are as follows :

        1> Write the sql query using .Net code
        Code:
        Dim Sql
        Sql = "Insert into Session(
        Computer Number
        Start time
        Amount Per second
        End time
        Total amount Total time)
        Values
        (
        
        " & txtComputerNumber &"
        " &txtStart &"
        " & txtAmount &"
        " & txtEndTime &"
        " & txttime &"
        )
        2> Execute above defined sql query using OledbCommand Object.
        For this define oledbcommand object and its properties.

        Code:
        Dim objCommand as new oledbCommand()
        
        ''Define the properties of oledbcommand object.
        
        objCommand.connection = conn
        objCommand.CommandText = SQL
        objCommand.CommandType = Text
        objCommand.ExecuteNonQuery
        3> When step 2 code is run data from above textboxes is inserted into the database.

        Comment

        Working...