anyone can help me please!
i have a problem in coding...
i want to restrict some buttons of my form if the user is not a admin
i already tried this code..

sql = "SELECT Position FROM tbllogin WHERE username = "txtUsername.text""
NewDataSet(sql)
if .ds.Tables("a").Rows.Count= "Administrator" Then
employeeFile.btnDelete.Enabled = True
else
employeeFile.btnDelete.Enabled = False
End if

but it doesnt work please anyone can help here

At the top of your code type this:

Option Strict On

Better yet, on the menu got Tools->Options. Expand the "Projects and Solutions" node. Select "VB Defaults" and turn it on there.

This will allow Visual Studio to point you to many mistakes in your code.

Look at this line:
if .ds.Tables("a").Rows.Count= "Administrator" Then
What does:.ds.Tables("a").Rows.Count return? (Hint: an integer value)
Will an integer value ever be equal to the string "Administrator"?

Also, your SQL statement is probably not well-formed. You should be concatenating the literal with TWO SINGLE-quotes and a couple of concatenation symbols

It should look like this (assuming MSSQL is your dbms):

sql = "SELECT Position from tblLogin WHERE username = ''" & txtUsername.text & "''"

Not quite correct. The select should be

sql = "SELECT Position from tblLogin WHERE username = '" & txtUsername.text & "'"

and somewhere, sometime, somehow, someone is going to mention how you should be using parameter entry instead of concatenation to protect against SQL injection attacks.

Comments
You're right, good catch. On both items :-)

change your sql statement

sql = "SELECT Position FROM tbllogin WHERE username = ' " & txtUsername.text & " ' "
NewDataSet(sql)
if .ds.Tables("a").Rows.Count= "Administrator" Then
employeeFile.btnDelete.Enabled = True
else if ds.tables("a").Rows.Count <> "Administrator" Then
employeeFile.btnDelete.Enabled = False
End if

. hope it works

Edited 4 Years Ago by Denden17

if .ds.Tables("a").Rows.Count= "Administrator" Then

will never evalualte to True because it is comparing an integer to a string. In English, the code is saying "if the number of rows equals Administrator"

This article has been dead for over six months. Start a new discussion instead.