Results 1 to 12 of 12

Thread: Need help in database Please

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Need help in database Please

    Hi,
    I am new and speak spanish,but this is a great forum and i try to explain my problem. I have a exercise to connecta to a database all work, but when i create a new record the record is no visible until i close and reopen my app.
    the problem is in btnCommit . I think i need 2 thinks first what row fill navegate sub when i add the record for example my rows is the variable (inc). What inc you recommend and the second i think, i need clear the dataset and refill with new data from the data adapter if the data adapter have the update? and refill the data set to view the new record without close the app. Please Help!!
    Sorry my english!! and i attach the project (Remember change the string to the database conection)
    I code is:
    vb.net Code:
    1. Public Class Form1
    2.     Dim inc As Integer
    3.     Dim MaxRows As Integer
    4.     Dim dbSource As String
    5.     Dim con As New SqlClient.SqlConnection
    6.     Dim ds As New DataSet
    7.     Dim da As SqlClient.SqlDataAdapter
    8.     Dim sql As String
    9.    
    10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    11.         dbSource = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Carl\Desktop\VBNET\Netfiles\databases\AddressBook.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    12.         con.ConnectionString = dbSource
    13.         con.Open()
    14.         sql = "SELECT * FROM tblContacts"
    15.         da = New SqlClient.SqlDataAdapter(sql, con)
    16.         da.Fill(ds, "AddressBook")
    17.  
    18.         con.Close()
    19.  
    20.         MaxRows = ds.Tables("AddressBook").Rows.Count
    21.         inc = 0
    22.         NavigateRecords()
    23.  
    24.     End Sub
    25.  
    26.     Private Sub NavigateRecords()
    27.         txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
    28.         txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2)
    29.     End Sub
    30.  
    31.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    32.         If inc <> MaxRows - 1 Then
    33.             inc = inc + 1
    34.             NavigateRecords()
    35.         Else
    36.             MsgBox("Last Record")
    37.         End If
    38.     End Sub
    39.  
    40.     Private Sub BtnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnPrevious.Click
    41.         If inc > 0 Then
    42.             inc = inc - 1
    43.             NavigateRecords()
    44.         Else
    45.             MsgBox("First Record")
    46.         End If
    47.     End Sub
    48.  
    49.     Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    50.         If inc <> MaxRows - 1 Then
    51.             inc = MaxRows - 1
    52.             NavigateRecords()
    53.         End If
    54.     End Sub
    55.  
    56.     Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    57.         If inc <> 0 Then
    58.             inc = 0
    59.             NavigateRecords()
    60.         End If
    61.     End Sub
    62.  
    63.     Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    64.         Dim cb As New SqlClient.SqlCommandBuilder(da)
    65.  
    66.         ds.Tables("AddressBook").Rows(inc).Item(1) = txtFirstName.Text
    67.         ds.Tables("AddressBook").Rows(inc).Item(2) = txtSurname.Text
    68.  
    69.         da.Update(ds, "AddressBook")
    70.  
    71.         MsgBox("Data updated")
    72.     End Sub
    73.  
    74.     Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
    75.         btnCommit.Enabled = True
    76.         btnAddNew.Enabled = False
    77.         btnUpdate.Enabled = False
    78.         btnDelete.Enabled = False
    79.         txtFirstName.Clear()
    80.         txtSurname.Clear()
    81.     End Sub
    82.  
    83.     Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    84.         btnCommit.Enabled = False
    85.         btnAddNew.Enabled = True
    86.         btnUpdate.Enabled = True
    87.         btnDelete.Enabled = True
    88.         inc = 0
    89.         NavigateRecords()
    90.     End Sub
    91.  
    92.     Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
    93.         If inc <> -1 Then
    94.             Dim cb As New SqlClient.SqlCommandBuilder(da)
    95.             Dim dsNewRow As DataRow
    96.             dsNewRow = ds.Tables("AddressBook").NewRow()
    97.             dsNewRow.Item("FirstName") = txtFirstName.Text
    98.             dsNewRow.Item("Surname") = txtSurname.Text
    99.             ds.Tables("AddressBook").Rows.Add(dsNewRow)
    100.             da.Update(ds, "AddressBook")
    101.             ds.Tables("AddressBook").AcceptChanges()
    102.             ds.Tables("AddressBook").Clear()
    103.             con.Open()
    104.             sql = "SELECT * FROM tblContacts"
    105.             da = New SqlClient.SqlDataAdapter(sql, con)
    106.             da.Fill(ds, "AddressBook")
    107.  
    108.             con.Close()
    109.  
    110.             inc = MaxRows - 1
    111.             NavigateRecords()
    112.             MsgBox("New Record added to the Database")
    113.             btnCommit.Enabled = False
    114.             btnAddNew.Enabled = True
    115.             btnUpdate.Enabled = True
    116.             btnDelete.Enabled = True
    117.         End If
    118.     End Sub
    119.  
    120.     Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    121.         If MessageBox.Show("Do you really want to Delete this Record?", _
    122. "Delete", MessageBoxButtons.YesNo, _
    123. MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No Then
    124.             MsgBox("Operation Cancelled")
    125.             Exit Sub
    126.         End If
    127.         Dim cb As New SqlClient.SqlCommandBuilder(da)
    128.         ds.Tables("AddressBook").Rows(inc).Delete()
    129.         MaxRows = MaxRows - 1
    130.         Select Case inc
    131.             Case Is < 1
    132.                 inc = 1
    133.             Case Is >= MaxRows - 1
    134.                 inc = MaxRows - 1
    135.             Case Else
    136.  
    137.         End Select
    138.         NavigateRecords()
    139.         da.Update(ds, "AddressBook")
    140.     End Sub
    141. End Class
    Attached Files Attached Files
    Last edited by Hack; Jul 16th, 2009 at 05:47 AM. Reason: Added Highlight Tags

  2. #2

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Re: Need help in database Please

    No work for what???LOL
    i use this:
    vb.net Code:
    1. Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
    2.         If inc <> -1 Then
    3.             Dim cb As New SqlClient.SqlCommandBuilder(da)
    4.             Dim dsNewRow As DataRow
    5.             dsNewRow = ds.Tables("AddressBook").NewRow()
    6.             dsNewRow.Item("FirstName") = txtFirstName.Text
    7.             dsNewRow.Item("Surname") = txtSurname.Text
    8.             ds.Tables("AddressBook").Rows.Add(dsNewRow)
    9.             da.Fill(ds, "AddressBook")
    10.             da.Update(ds, "AddressBook")
    11.             inc = MaxRows - 1
    12.             NavigateRecords()
    13.             MsgBox("New Record added to the Database")
    14.             btnCommit.Enabled = False
    15.             btnAddNew.Enabled = True
    16.             btnUpdate.Enabled = True
    17.             btnDelete.Enabled = True
    18.         End If
    19.     End Sub
    Last edited by Hack; Jul 16th, 2009 at 05:48 AM. Reason: Added Highlight Tags

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Re: Need help in database Please

    nop check this

    Private Sub NavigateRecords()
    txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
    txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2)
    End Sub
    and

    dsNewRow.Item("FirstName") = txtFirstName.Text
    dsNewRow.Item("Surname") = txtSurname.Text

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Re: Need help in database Please

    no i no use the wizard i try to learn first the code logistic, check this

    Private Sub NavigateRecords()
    txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
    txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2)
    End Sub
    and

    dsNewRow.Item("FirstName") = txtFirstName.Text
    dsNewRow.Item("Surname") = txtSurname.Text

  7. #7
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: Need help in database Please

    so it sounds like the problem is that you ARE updating your database, you just aren't updating your form's controls to display the new data (textbox, DataGridView, listbox, or whatever it may be)

    You would need to have a procedure that would basically go through all your database information-displaying controls and "re-get" the new values. hope that makes sense.

    a data-bound control would show you the new info right away as soon as you did the Fill/Update combination, your self-created controls will not, so you have to do it manually with code.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Re: Need help in database Please

    you have a example

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Need help in database Please

    What are you using on your form?

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Thumbs up Re: Need help in database Please

    Hi,
    I find the problem and is easy i no refresh the row count after update the dataset and data adapter the bottom is now:

    Code:
    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
            If inc <> -1 Then
                Dim cb As New SqlClient.SqlCommandBuilder(da)
                Dim dsNewRow As DataRow
    
                dsNewRow = ds.Tables("AddressBook").NewRow()
    
                dsNewRow.Item("FirstName") = txtFirstName.Text
                dsNewRow.Item("Surname") = txtSurname.Text
                ds.Tables("AddressBook").Rows.Add(dsNewRow)
                da.Update(ds, "AddressBook")
                da.Fill(ds, "AddressBook")
    
                MaxRows = ds.Tables("AddressBook").Rows.Count
    
                inc = MaxRows - 1
                NavigateRecords()
                MsgBox("New Record added to the Database")
    
                btnCommit.Enabled = False
                btnAddNew.Enabled = True
                btnUpdate.Enabled = True
                btnDelete.Enabled = True
    
                
            End If
        End Sub

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Need help in database Please

    1. You should use a bindingsource and a bindingnavigator control instead of coding your own next/previous/firt/last... buttons. This will make your life a whole lot easier.
    2. When you add a new datarow to you addressbook datatable in button commit click, you update the database and that all yoo have to do. There no need to clear the datatable and re-fill it. To update the count, you just re-read the row count from the addressbook datatable. And I saw that you already spotted this in your last post
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2006
    Posts
    11

    Cool Re: Need help in database Please

    Yes and thanks !!
    But i try to learn all functions in code first. if anyone have a example of data binding to this program. Y try to activate the autocomplete and when the auto complete suggest the program get the complete row for the others field.
    Any help?

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