emailaddresses from msaccess column separated by semicolon in 1 textbox

ud2008

Well-known member
Joined
Jul 5, 2010
Messages
148
Programming Experience
Beginner
In vb2010 I am trying to create a small program, I have a textbox and a combobox.

The combobox items are dynamically loaded from an XML file. And has items that correspond with the Groupname column in the access database.

The msaccess database has 3 columns (excl id), groupname, realname, emailaddress.

When I select in the combobox an item, records who has the same name in the groupname column, should select all the email addresses (seperated by a ";") and put them into the textbox.

For example:

When selected Group1 in the combobox.

1: Group1, Rick, rick @ rick.com
2: Group2, David, david @ david.com
3: Group1, Louise, louise @ louise.com

In this case the emailaddresses of records 1 and 3 should be into the textbox:

rick @ rick.com; louise @ louise.com

At the moment I receive an error message, I have tried to google the message but haven't found a solution for it:

Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.DataRow'

I have the following code, which isn't working yet, I hope someone can help me with this?
VB.NET:
Private Sub ToAddressesSelect()
        Try
            Dim cmd As OleDbCommand = New OleDbCommand("SELECT Groupname, Emailaddress FROM email WHERE Groupname = '" + ComboBox1.SelectedText + "'", con)
            con.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows = True Then
                For Each row As DataRow In dr
                    'While dr.Read()
                    TextEdit1.Text = dr("Emailaddress").ToString()
                    'End While
                Next
            Else
                MessageBox.Show("No data found!")
            End If
            con.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub EmailSonglist_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        DevExpress.UserSkins.BonusSkins.Register()
        DevExpress.UserSkins.OfficeSkins.Register()

        Dim fileI As FileInfo = New FileInfo(Application.StartupPath + "\groups.xml")
        If fileI.Exists Then
            Dim doc As XDocument = XDocument.Load(Application.StartupPath + "\groups.xml")
            ComboBox1.Items.Add("To...")
            For Each element As XElement In doc.Descendants("Group")
                ComboBox1.Items.Add(element.Value)
            Next
            ComboBox1.SelectedIndex = 0
        Else
            MessageBox.Show("There is no addressbook available, please create one.")
            Process.Start(Application.StartupPath + "\Addressbook.exe")
        End If
    End Sub

Thanks for any help.
 
I have tried the following code, but all I got is when I click a single row and then click the To button the emailaddress from that selected row is add to the textbox multiple times (separated by a semicolon).

So I am on the way, but still it isn't what I want, which is just as the office outlook way of adding emailaddresses to the To textbox.

So here is the code I have sofar:
VB.NET:
Private Sub ToButton_Click(sender As System.Object, e As System.EventArgs) Handles ToButton.Click
        Dim tempstr As String
        Dim cnt As Integer
        Dim FirstValue As Boolean = True
        For cnt = 0 To (DataGridViewX1.Rows.Count - 1)
            If Not FirstValue Then
                tempstr += "; "
            End If
            tempstr += DataGridViewX1.SelectedCells(3).Value
            FirstValue = False
        Next
        ToTextBox.Text = tempstr
    End Sub
 
Back
Top