Jump to content

Adding to Array / Pulling Data from Arrays


Damein
 Share

Recommended Posts

Hey guys,

 

I just started poking around in VB today so I could use it to do some SQL work. I made decent head way until I hit this block.

 

I can grab the data from the Database and put it into a DataSet or DataTable but I want to be able to autofill some textboxes with the results as well.

 

Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient

Public Class Form1

    Public phoneNumbers

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim constr As New String _
        ("Data Source=*****;port=3306;Initial Catalog=WDL-Test;User Id=AdminWebLink3;password=****")
        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand("SELECT customerID, Name, Email, Phone FROM Contacts")
                Using sda As New MySqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd
                    Dim ds As New DataSet()
                    sda.Fill(ds)
                    customerDropDown.DataSource = ds.Tables(0)
                    customerDropDown.DisplayMember = "customerID"
                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

                        Dim phoneNumbers As String() = (From myRow In ds.Tables(0).AsEnumerable
                                                        Select myRow.Field(Of String)("Phone")).ToArray
                    Next

                End Using
            End Using
        End Using

    End Sub

    Private Sub getCustomerName_Click(sender As Object, e As EventArgs) Handles getCustomerName.Click
        MessageBox.Show(customerDropDown.Text + " " + Convert.ToString(customerDropDown.SelectedValue))
    End Sub

    Private Sub customerDropDown_SelectedIndexChanged(sender As Object, e As EventArgs) Handles customerDropDown.SelectedIndexChanged
        customerNumberBox.Text = phoneNumbers(3)
    End Sub


End Class

 

In the above it auto grabs the data from the table selected and populates a Drop down box with the customerID. I then am going to assign values to arrays "Name", "Email" and "Phone" that when they select an item from the drop down box it will auto fill in the appropriate text boxes. I know I don't have all of the array assignments of the customerDropDown_SelectedIndexChanged setup properly, that will be done when I find out how I can populate the textboxes with actual data.

 

Currently it errors out with a NULL code because for some reason it doesn't find any data in the array. So I think I am just misunderstanding how to properly populate an array/make it public.

 

Thanks for the help!

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

*** EDIT ***

 

After posting this I realized that the reason it was failing was because it was being called before the array could be populated. If I placed it inside the Button clicked function it worked just fine. So I guess my question is now, how do I keep it in the same function (When the drop down box is changed) and it still work?

 

Thanks!

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

This can be closed. I've solved it by a rough method and will continue to expand on it and maybe improve, but its good for now.

 

Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient

Public Class Form1

    Public customerName As String()
    Public customerEmail As String()
    Public customerPhone As String()
    Public formLoaded As Boolean


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

        formLoaded = False

        Dim constr As New String _
        ("Data Source=******;port=3306;Initial Catalog=WDL-Test;User Id=AdminWebLink3;password=*****")

        Using con As New MySqlConnection(constr)
            Using cmd As New MySqlCommand("SELECT customerID, Name, Email, Phone FROM Contacts")


                Using sda As New MySqlDataAdapter()
                    cmd.Connection = con
                    sda.SelectCommand = cmd

                    Dim ds As New DataSet()
                    sda.Fill(ds)
                    customerDropDown.DataSource = ds.Tables(0)
                    customerDropDown.DisplayMember = "customerID"

                    For i As Integer = 0 To ds.Tables(0).Rows.Count - 1

                        customerName = (From myRow In ds.Tables(0).AsEnumerable
                                        Select myRow.Field(Of String)("Name")).ToArray

                        customerEmail = (From myRow In ds.Tables(0).AsEnumerable
                                         Select myRow.Field(Of String)("Email")).ToArray

                        customerPhone = (From myRow In ds.Tables(0).AsEnumerable
                                         Select myRow.Field(Of String)("Phone")).ToArray

                    Next



                End Using
            End Using
        End Using

        formLoaded = True

    End Sub

    Private Sub customerDropDown_SelectedIndexChanged(sender As Object, e As EventArgs) Handles customerDropDown.SelectedIndexChanged
        If formLoaded = True Then
            Dim count As String = customerDropDown.SelectedIndex
            customerNumberBox.Text = customerPhone(count)
            customerEmailBox.Text = customerEmail(count)
            customerNameBox.Text = customerName(count)
        End If

    End Sub

End Class

 

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...