new in vb.net

  • 10 years ago

    hello all, iam ghazi from lebanon, i liked to register and start discussions with all about vb.net. in fact iam new in vb.net but working in php language and oracle\mysql\access databases. so please i would like to ask in vb.net how to link and manage the ACCESS database with multiple tables from vb.net?

    iam waiting ur replying soon....

    my regards.

    ghazi

  • 10 years ago

    Hi,

    VB.Net uses the server explorer to manage your data connections.  You need to browse to your data base or database server using the server explorer.  Once you have found the required data store you can then make a connection using the Data Link Properties dialog.  That will enable you to input your database login id and passwords/provider tyepe, etc, using the various tabs. 

    The connection manager is slightly different for SQL/Access and other database types, but without any information about this, I can only give you a general guide overview.

    Once you have added a connection (and there are several options that you can configure on the way) you should be able to use server explorer to browse the various datasets/tables that your database has stored (e.g within Access).

    These items will display in Server Explorer and you can then drag them directly from Server Explorer to the Design screen.  This will create a connection of the appropriate type automatically.  If you have already created Stored Procedures you can drag these to the Design window also.  These will create the appropriate Command Objects, which can then be added to your code.

    You need to ensure that your command objects reference the appropriate connection type, e.g your OleDbCOmmand (for Access) must use an OleDbConnection as its connection.  Once these are in place you can then use appropriate Data Adapters and Data Reader methods to obtain the data that you want to interact with in your application.

     

    Not too sure I can really help much more than that,m except by suggesting you get a book on ADO.Net or similar.

    Good luck.

    Gerard

     

  • 10 years ago

    hi Gerard, thanks for replaying

    i've created a connection with access database OleDbConnection as its connection and i used appropriate Data Adapters and Data Reader methods to obtain the data that i want to interact with in the application. it works appropriatly with 1 table but i face a problem when i create other table with a forign key to the parent table, the vb.net cant understand the forign key directly.... here the code i wrote, just testing.

    ====================================================

    Imports

    System.Data

    Imports

    System.Xml

    Public

    Class Form1

    Dim inc As Integer

    Dim maxRows As Integer

    Dim con As New OleDb.OleDbConnection

    Dim ds As New DataSet

    Dim da As OleDb.OleDbDataAdapter

    Dim sql As String

     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

    If inc <> maxRows - 1 Then

    inc = inc + 1

    NavigateRecords()

    Else

    MsgBox(

    "No more Rows")

    End If

    NavigateRecords()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Call load_form()

    Call test_fct(14)

    End Sub

    Private Sub test_fct(ByVal x As Integer)

    MsgBox(x)

    End Sub

    Private Sub load_form()

    If (con.State = ConnectionState.Closed) Then

    con.ConnectionString =

    "PROVIDER = Microsoft.Jet.OLEDB.4.0; Data Source = C:\db1.mdb"

    con.Open()

    End If

    sql =

    "SELECT * FROM AddressBook"

    da =

    New OleDb.OleDbDataAdapter(sql, con)

    da.Fill(ds,

    "AddressBook")

    maxRows = ds.Tables(

    "AddressBook").Rows.Count

    inc = 0

    If (con.State = ConnectionState.Connecting) Then

    con.Close()

    con.Dispose()

    con =

    Nothing

    End If

     

    End Sub

    Private Sub NavigateRecords()

    txtFirstName.Text = ds.Tables(

    "AddressBook").Rows(inc).Item(1)

    txtSurname.Text = ds.Tables(

    "AddressBook").Rows(inc).Item(2)

    txtAddress.Text = ds.Tables(

    "AddressBook").Rows(inc).Item(3)

    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click

    If inc <> 0 Then

    inc = inc - 1

    NavigateRecords()

    ElseIf inc = -1 Then

    MsgBox(

    "No records")

    ElseIf inc = 0 Then

    MsgBox(

    "First record")

    End If

    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click

    If inc <> 0 Then

    inc = 0

    NavigateRecords()

    End If

    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click

    If inc <> maxRows - 1 Then

    inc = maxRows - 1

    NavigateRecords()

    End If

    End Sub

    Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click

    End Sub

    Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Label7.Text = ds.Tables(

    "AddressBook").Rows.Count

    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables(

    "AddressBook").Rows(inc).Item(1) = txtFirstName.Text

    ds.Tables(

    "AddressBook").Rows(inc).Item(2) = txtSurname.Text

    ds.Tables(

    "AddressBook").Rows(inc).Item(3) = txtAddress.Text

    da.Update(ds,

    "AddressBook")

    MsgBox(

    "Data updated")

    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click

    btnCommit.Enabled =

    True

    btnAddNew.Enabled =

    False

    btnUpdate.Enabled =

    False

    btnDelete.Enabled =

    False

    txtFirstName.Clear()

    txtSurname.Clear()

    txtAddress.Clear()

     

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    btnCommit.Enabled =

    False

    btnAddNew.Enabled =

    True

    btnUpdate.Enabled =

    True

    btnDelete.Enabled =

    True

    NavigateRecords()

    End Sub

    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click

    'If inc <> -1 Then

    If txtFirstName.Text.Length = 0 Or txtSurname.Text.Length = 0 Or txtAddress.Text.Length = 0 Then

    MsgBox(

    "Complete the confirmation")

    Else

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    Dim dsNewRow As DataRow

    dsNewRow = ds.Tables(

    "AddressBook").NewRow

    dsNewRow.Item(

    "FirstName") = txtFirstName.Text

    dsNewRow.Item(

    "Surname") = txtSurname.Text

    dsNewRow.Item(

    "Address") = txtAddress.Text

    ds.Tables(

    "AddressBook").Rows.Add(dsNewRow)

    da.Update(ds,

    "AddressBook")

    MsgBox(

    "New record added to the database")

    btnCommit.Enabled =

    False

    btnAddNew.Enabled =

    True

    btnUpdate.Enabled =

    True

    btnDelete.Enabled =

    True

    Call load_form()

    End If

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    If MessageBox.Show("Do you really want to Delete this Record?", _

    "Delete", MessageBoxButtons.YesNo, _

    MessageBoxIcon.Warning) = Windows.Forms.DialogResult.No

    Then

     

    MsgBox(

    "Operation Cancelled")

    Exit Sub

    End If

    ds.Tables(

    "AddressBook").Rows(inc).Delete()

    maxRows = maxRows - 1

    inc = 0

    NavigateRecords()

    da.Update(ds,

    "AddressBook")

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtphone.TextChanged

    End Sub

    End

    Class

    ===========================================

    regards..

    ghazi

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook