tags:

views:

116

answers:

5

I have a datatable. I need to fetch a certain column value based on the user input. For example, lets say the datatable has two columns CountryID and CountryName.

I need to find CountryID in the datatable based on the user input country name. I could just open a connection with DB and run the query select countryID from Country where countryName = @userinput. Is there anyway i could do this on the datatable.

A: 

I suppose you could use a DataView object instead, this would then allow you to take advantage of the RowFilter property as explained here:

http://msdn.microsoft.com/en-us/library/system.data.dataview.rowfilter.aspx

private void MakeDataView() 
{
    DataView view = new DataView();

    view.Table = DataSet1.Tables["Countries"];
    view.RowFilter = "CountryName = 'France'";
    view.RowStateFilter = DataViewRowState.ModifiedCurrent;

    // Simple-bind to a TextBox control
    Text1.DataBindings.Add("Text", view, "CountryID");
}
Dal
Thanks, i haven't tried it yet. Can i move the value to a variable instead of binding to a text field?
peace
Not tested, but you could try:string CountryName = view.Table.Rows[0]["CountryName"].ToString();
Dal
+1  A: 
foreach(Datarow row in Datatable.Rows) {
    if (row["CountryName"].ToString() == userInput) {
        return row["CountryID"];
    }
}

while this may not compile directly you should get the idea, also I'm sure it would be vastly superior to do the query through SQL as a huge datatable will take a long time to run through all the rows.

Jimmy
Simple and straight to the point but as you said, this can cost alot in the later future.
peace
All in all nothing is going to be more efficient than just using SQL, LINQ to Datatables and Datatables / Dataviews will all have to load all of the records into memory and then query them
Jimmy
Another option is simply just using linq to sql
Jimmy
A: 
        string countryName = "USA";
        DataTable dt = new DataTable();
        int id = (from DataRow dr in dt.Rows
                  where (string)dr["CountryName"] == countryName
                  select (int)dr["id"]).FirstOrDefault();
Seattle Leonard
of course the `DataTable` would be populated from your DB
Seattle Leonard
I might go with your solution................
peace
A: 

Datatables have a .Select method, which returns a rows array according to the criteria you specify. Something like this:

Dim oRows() As DataRow

oRows = dtCountries.Select("CountryName = '" & userinput & "'")

If oRows.Count = 0 Then
   ' No rows found
Else
   ' At least one row found. Could be more than one
End If

Of course, if userinput contains ' character, it would raise an exception (like if you query the database). You should escape the ' characters (I use a function to do that).

ACB
A: 

I'm hesitant which solution is more efficient - Dal's or Seattle Leonard?

peace
peace, in this instance I think Seattle's might be the better option because it deals with the DataTable object, thus no need to convert to using a DataView... Seattle's code uses LINQ - Closest thing to doing a proper SQL query in your code :)
Dal