Results 1 to 5 of 5

Thread: Update field in data row

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612

    Update field in data row

    Hi guys. I know how to create a new record in a database. What I don't know is how to update one field in a database record.

    This is the code I would use to create a new record.
    VB Code:
    1. Dim datAdapt As New OleDb.OleDbDataAdapter("Select * From CigUnits", cn2)
    2. Dim commBuild As New OleDb.OleDbCommandBuilder(datAdapt)
    3. Dim datSet As New Data.DataSet()
    4. Dim datRow As Data.DataRow
    5.  
    6. datSet.Clear()
    7. datAdapt.Fill(datSet, "CigUnits")
    8. datRow = datSet.Tables("CigUnits").NewRow
    9. datRow.Item("EmpID") = Request.Cookies("EmpID").Value
    10. datRow.Item("Store") = datRead.Item("location")
    11. datRow.Item("BusiDate") = datRead.Item("Date")
    12. datRow.Item("PC") = datRead.Item("ProductCode")
    13. datSet.Tables("CigUnits").Rows.Add(datRow)
    14. datAdapt.Update(datSet, "CigUnits")

    Can someone please give me an example of some code where I can find a particular record, and then update one field in that record.

    Thanks!
    David Wilhelm

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    VB Code:
    1. Dim Conn As New OleDbConnection("Your Connectionstring")
    2. Dim Cmd As New OleDbCommand("UPDATE TableName SET fieldName = whatever", Conn)
    3.  
    4. Try
    5.    Conn.Open()
    6.    Cmd.ExecuteNonQuery()
    7.    Conn.Close()
    8. Catch ex As Exception
    9.    'catch the exception
    10. End Try
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3
    Addicted Member Rockhopper's Avatar
    Join Date
    Aug 2003
    Location
    Cape Town, South Africa
    Posts
    199
    The code that " Memnoch1207" used is exactly what i use, but just remember the 'WHERE' part of the update statement:

    Dim Cmd As New OleDbCommand("UPDATE TableName SET fieldName = whatever WHERE PrimaryKey/FieldName='" & IDofitem/CurrentNameofItem you want to update & "'", Conn)
    If you don't specify the exact record, the update statement will act on all of the records in the DB.

    If the facts don't fit the theory, change the facts. --Albert Einstein

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Memnoch1207, could you please show me an example of some code where you go through a do while loop, read one field from a record, then update another field?

    Thanks for your help.


    If you don't specify the exact record, the update statement will act on all of the records in the DB.
    Like, Duh! lol
    David Wilhelm

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2001
    Location
    Indiana
    Posts
    612
    Memnoch1207, nevermind. I can use a data reader, get the one value, then use your code to update the other field...

    Thanks again.
    David Wilhelm

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