0

I am using this line:

For Each dtrow In FbuildingSettings.camButtonDtable.Select("Floor ='3'")

to select all items from Floor = 3
How can I add another argument the states..

.Select(Building = Mall and Floor = 3)

where it will find all data that has Mall as building, then get all from the third floor.
I tried something like : .Select("Floor ='3' And Building ="'"Mall"'"") - but it seems I am doing it wrong. I am not really good in sql.
Also, I am planning on using variables for Mall and 3.. If you would be so kind, make it that way thanks :)

UPDATE: I tried valverij answer and it comes down to this: (though Floor=3 code is still working) enter image description here

AdorableVB
  • 1,383
  • 1
  • 13
  • 44

1 Answers1

1

It looks like your multiple-condition Select has way too many quotes in it. According to this question, you should just be able to do something like this:

.Select("Floor ='3' AND Building ='Mall'")

I'm not sure if the capitalization matters there, but you'd might as well make the AND all caps while you're at it.

You can also do this with LINQ using AsEnumerable and Where:

For Each dtrow In FbuildingSettings.camButtonDtable.AsEnumerable.Where(Function(dr) dr("Floor") = "3" AndAlso dr("Building") = "Mal

UPDATE: Based on the update to your question, it looks like nBtn.Name is expecting one type, and your statement is implicitly trying to set it to something else. If you set Option Strict On at the top of your file, VB will force you to cast explicitly where necessary when setting/using your objects. If you don't want to do that, that's fine, but it might come down to doing something like this:

nBtn.Name = dtrow("ButtonText").ToString & dtrow("ID").ToString

or this:

nBtn.Name = String.Format("{0}{1}", dtrow("ButtonText").ToString, dtrow("ID").ToString)

Also, check to make sure that dtrow("ButtonText") and dtrow("ID") actually contain a value (i.e., that they aren't DBNull.Value) If that's the problem, then you can use temporary variables along with If statements to set them accordingly:

Dim buttonText As String = String.Empty
Dim id As String = String.Empty

If dtrow("ButtonText") IsNot DBNull.Value Then
    nBtn.Name &= dtrow("ButtonText").ToString
End If

If dtrow("ID") IsNot DBNull.Value Then
    nBtn.Name &= dtrow("ID").ToString
End If

Note, though, the ToString will make sure that VB grabs the String representation of the value in your DataRow. Otherwise, I think it just tries to use the type based on what was added when the DataRow was created.

Community
  • 1
  • 1
valverij
  • 4,871
  • 1
  • 22
  • 35
  • does making either of them in the front make a difference? – AdorableVB Jan 28 '14 at 02:50
  • I don't think so, but the syntax for `DataTable.Select` might be slightly different than regular SQL. Here's an article outlining the use of the row filter syntax. It's in C#, but the syntax for the `Select` should be the same: http://www.csharp-examples.net/dataview-rowfilter/ Keep in mind that `NULL` records might throw it off. – valverij Jan 28 '14 at 02:54
  • thanks for the link, can you check my updated question? – AdorableVB Jan 28 '14 at 02:59
  • @AdorableVB, it looks like there's an implicit typing issue between `nBtn.Name` and the value in your `DataRow`. I've updated my answer to include that. – valverij Jan 28 '14 at 03:19
  • got it, although, I still get the same error, I am pretty sure that there is something wrong with the `for each` line. Is there any other way that gives the same output that I want? – AdorableVB Jan 28 '14 at 03:33
  • Sorry for the trouble, I think I have my answer now. Just use `AND` :) – AdorableVB Jan 28 '14 at 03:35
  • Not a problem. I figured I'd throw the LINQ in there as an extra example, sorry if it caused some confusion. – valverij Jan 28 '14 at 03:44
  • And I just realized the LINQ example should have been a Where, not a Select. – valverij Jan 28 '14 at 07:25