Results 1 to 12 of 12

Thread: Need help in database Please

Threaded View

  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

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