Results 1 to 15 of 15

Thread: editing an sql row

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    editing an sql row

    I built a program that basically lets you navigate,edit,add,delete records in an access database. When i changed the connection over to an sql table "ish" hit the fan. the adding function and editing function turned into mush. i fixed the add function, but the edit is hairy. when i save it will keep the new value in the text box, but come to reopening the program the old value came back. i guess osme code is in order
    VB Code:
    1. 'edit button
    2. Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
    3.         If btnAdd.Text = "&Cancel" Then
    4.             LockText()
    5.             btnEdit.Enabled = True
    6.             btnDelete.Enabled = True
    7.             btnAdd.Text = "&Add"
    8.             btnSave.Enabled = False
    9.             RejectChanges()
    10.             blnEdit = False
    11.         Else
    12.             unlockText()
    13.             NoNavigation()
    14.             btnSave.Enabled = True
    15.             btnAdd.Text = "&Cancel"
    16.             btnEdit.Enabled = False
    17.             btnDelete.Enabled = False
    18.             blnEdit = True
    19.             displayrecordposition()
    20.         End If
    21. end sub
    22. 'save button (don't mind what I have for edit already)
    23.  Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    24.         If blnAdd = True Then
    25.             Try
    26.                 Dim newrow As DataRow = DsSQL1.Dem.NewRow
    27.                 newrow("first") = txtFirst.Text
    28.                 newrow("last") = txtLast.Text
    29.                 newrow("ssn") = txtSSN.Text
    30.                 newrow("dob") = txtBday.Text
    31.                 DsSQL1.Dem.Rows.Add(newrow)
    32.             Catch exc As Exception
    33.                 MessageBox.Show("Unable to add record." & _
    34.                 ControlChars.NewLine & exc.Message, "dem")
    35.             End Try
    36.             blnAdd = False
    37.             lblRecordNumber.Text = "Record added"
    38.         End If
    39.         If blnEdit = True Then
    40. 'i know i need to fix this but to what?
    41.             Dim editrow As DataRow = DsSQL1.Dem.NewRow
    42.             editrow("first") = txtFirst.Text
    43.             editrow("last") = txtLast.Text
    44.             editrow("ssn") = txtSSN.Text
    45.             editrow("dob") = txtBday.Text
    46.             DsSQL1.Dem.Rows.Add(editrow)
    47.  
    48.         End If
    49.  
    50.         LockText()
    51.         GoNavigation()
    52.         btnSave.Enabled = False
    53.         btnAdd.Text = "&Add"
    54.         blnIsDirty = True
    55.         btnEdit.Enabled = True
    56.         btnDelete.Enabled = True
    57.  
    58.  
    59.     End Sub

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: editing an sql row

    i just have question what is blnEdit ? which control
    here is the code
    VB Code:
    1. Dim strSQLSelect As String = "SELECT * FROM TableName"
    2.         Dim adapter As New OleDbDataAdapter(strSQLSelect, conn)
    3.         Dim ds As New DataSet
    4.         adapter.Fill(ds, "TableName")
    5.         ' Modify the in-memory records in the DataSet
    6.         Dim tbl As DataTable = ds.Tables("TableName")
    7.         tbl.PrimaryKey = New DataColumn() _
    8.                          { _
    9.                            tbl.Columns("PrimaryKey") _
    10.                          }
    11.         Dim row As DataRow = tbl.Rows.Find(PrimaryKey)
    12.  
    13.         row.Item("Filed1") = Txtfiled2.Text
    14.         row.Item("Feild2") = Txtfiled2.Text
    15. .......
    16.        
    17.         ' Reconnect the DataSet and update the database
    18.         Dim cb As New OleDbCommandBuilder(adapter)
    19.        
    20.         adapter.Update(ds, "TableName")

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    blnedit is declared at the modular level and is set to true or false in the btnedit procedure.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    ok this is really odd. when I dim the adapter as new oledbadapter and oldecommand are not defined. If I already have my connection with my tables established in the server explorer through the wizard will this code chage?

  5. #5
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: editing an sql row

    if u r trying to connect to access so plz import
    VB Code:
    1. Imports System.Data.OleDb
    and if u r using sql
    VB Code:
    1. Imports System.Data.SqlClient
    and u have to change the adapter and command into sqladapter and sqlcommand

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    Quote Originally Posted by nana_81
    if u r trying to connect to access so plz import
    VB Code:
    1. Imports System.Data.OleDb
    and if u r using sql
    VB Code:
    1. Imports System.Data.SqlClient
    and u have to change the adapter and command into sqladapter and sqlcommand
    thanks for that import statement, it helped a bit. although I am getting errors. I modded the code I got earlier because I already had my dataset and such defined and loaded at form load.
    VB Code:
    1. 'i have a problem with this code
    2.  Dim row As DataRow = tbl.Rows.Find(PrimaryKey)'it says primarykey
    3. 'isnt declared

  7. #7
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: editing an sql row

    u should specify ur primary key. if ur primary key in the table is ID so write that

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    I tried substituting primarykey with the name of my pk field, tried adding a ",field". do i have to declare it or what do you mean?

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: editing an sql row

    DataRowCollection.Find() requires the primary key value of the row you want to find. If you have three rows with primary key values 1, 2 and 3 and you want to find the middle row, you use <TableName>.Rows.Find(2)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    Quote Originally Posted by jmcilhinney
    DataRowCollection.Find() requires the primary key value of the row you want to find. If you have three rows with primary key values 1, 2 and 3 and you want to find the middle row, you use <TableName>.Rows.Find(2)
    how does this help me edit my row? should I make a new variable that has a value of the pk when you are on that record and put for example: table.row.find(intPK)?

  11. #11
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: editing an sql row

    here is the code
    VB Code:
    1. UserID=txtUserID.text
    2. Dim conn As OleDbConnection = New OleDbConnection
    3.         conn.ConnectionString = strCn
    4.         'Open(connection)
    5.         If conn.State <> ConnectionState.Open Then
    6.             conn.Open()
    7.  
    8.         Dim strSQLSelect As String = "SELECT * FROM Users"
    9.         Dim adapter As New OleDbDataAdapter(strSQLSelect, conn)
    10.         Dim ds As New DataSet
    11.         adapter.Fill(ds, "Users")
    12.         ' Modify the in-memory records in the DataSet
    13.         Dim tbl As DataTable = ds.Tables("Users")
    14.         tbl.PrimaryKey = New DataColumn() _
    15.                          { _
    16.                            tbl.Columns("UserID") _' UserID is the primary key in Users table
    17.                          }
    18.         Dim row As DataRow = tbl.Rows.Find(IDUser)' variable i declared and i stored the required UserID
    19.  
    20.         row.Item("UserID") = IDUser
    21.         row.Item("UserName") = TxtName.Text
    22.       row.Item("UserNameArabic") = TxtArabic.Text
    23.         row.Item("UserNotes") = TxtNotes.Text
    24.  
    25.         ' Reconnect the DataSet and update the database
    26.         Dim cb As New OleDbCommandBuilder(adapter)
    27.         'Connect()
    28.         adapter.Update(ds, "Users")
    29.  
    30.         If conn Is Nothing Then
    31.             If conn.State = ConnectionState.Open Then
    32.                 conn.Close()
    33.             End If
    34.             ' Dispose connection
    35.             conn.Dispose()
    36.         End If
    i hope this clear
    Last edited by nana_81; May 24th, 2005 at 01:27 PM.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    what is strCn

    i matched this line with mine
    UserID=txtUserID.text
    did you declared userid as string?
    now i have a procedure at my form closing that is supposed to finish the update. for example it looks good when i hit the save button. then i have a trigger that if i do change something it asks me update my dataset again. will this interfere or is worthless code?

  13. #13
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: editing an sql row

    StrCn my string connection ,access is saved in bin folder
    VB Code:
    1. Dim strCn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "\DataBaseName.mdb"
    IDUser
    VB Code:
    1. Dim IDUser As String
    UserID field ,in my database ,its DataType is Text
    is that helpful i wish

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    162

    Re: editing an sql row

    I am using sql. I already got everythign to work with access. maybe it is more friendly, i dunno. another problem is with this code it is opening another connection when i am saving therefore dismissing any changes and reloading the conneciton all over again. what i think the problem is that with
    VB Code:
    1. Dim tbl As DataTable = DsSQL1.Tables("dem")
    2.             tbl.PrimaryKey = New DataColumn() _
    3.                             { _
    4.                              tbl.Columns("ssn") _
    5.                          }
    6.             Dim row As DataRow = DsSQL1.Dem.Rows.Find(ssn)
    this set of code it is not finding and changing the record. my primary key is varchar in my table. i dont know how to convert it in vb. i dont know why they made editing records so complicated.

  15. #15
    Addicted Member
    Join Date
    Jan 2005
    Posts
    136

    Re: editing an sql row

    if u face many problems using this why. why dont u use an update statment and then excute it.

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