I created a function which checks if an username already exists in DB.
I am using SqlCommand
with sql-parameterd to avoid Sql-Injection. To execute I should use the class SqlAccess
, because the whole existing project is built with that.
The Code I've written...
'Check if username already exists'
Public Shared Function usernameExists(Username As String) As Boolean
Dim sqlCommand As SqlCommand = New SqlCommand("Select UserLogin From [User] where UserLogin = '@Username'")
Dim param As SqlParameter = New SqlParameter("Username", SqlDbType.VarChar)
param.Value = Username
sqlCommand.Parameters.Add(param)
Try
SQLAccess.OpenSQLConnection(sqlCommand, SetDBConnectionData) 'SqlDBConnectionData is a function which returns a structSQlAccess'
Dim dsUsers As DataSet = SQLAccess.SQLQueryToDS(sqlCommand)
If dsUsers.Tables(0).Rows.Count > 0 Then
Return True
Else
Return False
End If
Catch ex As Exception
Return True 'TODO - Error Mgs
End Try
End Function
But it doesn't work, the DataSet
does not contain rows. Could anyone please show me how to do it properly?