SO community

I get began with ADO.Internet and am a newcomer to programming. I've had some support already out of this community that has been very helpful coupled with another question.

Basically, I'm developing a simple home windows form having a combobox of worker names along with a button to ensure that once the user clicks the button the chosen employee's current email address seems inside a textbox around the form.

During my project I've database as well as on loading the shape I connect with my database and make up a datatable as proven below:

Public Class GetEmployeeDataset

Private tbl As New DataTable

Public Sub New()
    Dim strConn, strSQL As String
    strConn = "Data Source=.\SQLExpress;Initial Catalog=MyDatabase;Integrated Security=True;"
    strSQL = "SELECT * FROM EmployeeTable"

    Dim da As New SqlDataAdapter(strSQL, strConn)
    da.Fill(tbl)
End Sub

End Class

At this time I've two questions:

  1. I'm using DataTable instead of a DataSet because, when i comprehend it, a DataSet is an accumulation of DataTables and that i have only one easy DataTable here (5 posts, 100 rows) therefore it appears simpler just to utilize a DataTable. Anything wrong with this?
  2. Since I've got a DataTable ('tbl' during my example) how do i query that DataTable to retrieve the right data i.e. EmailAddress according to worker title? In SQL it might be "Choose EmailAddress WHERE EmployeeName = SelectedItem". I've checked out while using Choose approach to the DataTable but with no success after which I went lower the RowFilter approach to DataView but with no success.

Can anybody point me within the right direction please?

Alex

I do not use DataSets or DataTables whatsoever. The only real reason I've discovered for their services is if you wish to hold all the data "offline," make changes towards the data, then commit individuals changes to the database when needed.

If all you are doing is getting data in the server according to user interaction, it might be overkill to utilize a DataTable.

Edit: Oh, and also to answer your actual questions:

  1. Sounds fine because you just have one table.
  2. Make use of the Choose method around the DataTable (that will return a range of DataRows that fulfill the where condition)

Remaining using the ADO objects, this is the way you could utilize the DataView...

  Dim dv As DataView
  Dim strFilter As String
  Dim strEmail As String = ""  

  strFilter = "EmployeeName = '" & cbo.Text & "'"  

  dv = tbl.DefaultView
  dv.RowFilter = strFilter  

  If dv.Count > 0 Then
     strEmail = dv(0).Item("EmailAddress").ToString
  End If

An easy enumeration will have the desired effect:

string email = string.Empty;
foreach (Row row in tbl.Rows)
{
    string employeeName = (string)row["EmployeeName"];
    if (employeeName == "John")
    {
        email = (string)row["Email"];
        break;
    }
}

or try LINQ:

var email = (from row in tbl.Rows
            where (string)row["EmployeeName"] == "John"
            select (string)row["Email"]).First ();

I am not checking the code in Versus so "typos reserved". Sorry for C# version but I'm not sure VB.