I am trying to compare a session variable to another string so that i can enable or disable image buttons. I am using asp.net vb with a sql2005 express backend
P
String Compare returns 0 if the strings are equal. To evaluate for true change your comparison to = 0 rather than <> 0.
If String.Compare(string1, string2, StringComparison.InvariantCultureIgnoreCase) = 0 Then
If you envision performing such checks frequently to display different portions of a page you might want to break your pages up based on the different types (or levels) of users you expect and redirect them to a particular page upon login. Depending on how similar the pages are between users you'll likely find using master pages and/or user controls to be beneficial.
this is works!
ImageButton1.Enabled = Not Session("UserName") Is Nothing AndAlso Session("UserName").ToString.ToLower.Trim.Equals("username")
ImageButton2.Enabled = Not Session("UserName") Is Nothing AndAlso_ Session("UserName").ToString.ToLower.Trim.Equals("username")
String.Compare returns -1, 0 or 1 depending on the result:
String.Compare("A", "B", true) ' returns -1 '
String.Compare("B", "A", true) ' returns 1 '
String.Compare("B", "B", true) ' returns 0 '
In your case, ImageButton1 will be disabled if the user name would come before "medev" if you sorted them as a list. So "Adam" wound have it disabled, while "Stuart" would have it enabled. "medev" would also get it disabled, because if the strings are equal, String.Compare returns 0. My guess is that you want ImageList1 enabled for "medev":
If String.Compare(string1, string2, True) <> 0 Then
ImageButton1.Enabled = False
End If
I would also suggest using the String.Compare overload that takes a StringComparison instead of a bool to get a case insensitive compare. In my opinion it makes it a lot easier to understand what the code does:
If String.Compare(string1, string2, StringComparison.InvariantCultureIgnoreCase) <> 0 Then
ImageButton1.Enabled = False
End If