tags:

views:

52

answers:

2

Possible Duplicate:
Checking for an SQL result in VB.NET

I have login form which is redirect user for their levels but before to that if there is no any user on the data table I would like to redirect to create admin form. I have all the forms but I didn't manage to redirect them because I don't know how to create statement. Could you please help me about solution.

    Dim con As SqlCeConnection
    Dim command As SqlCeCommand
    con = New SqlCeConnection("Persist Security Info=False;Data Source=.\database.sdf;Password=26091974-emre;File Mode=shared read")
    con.Open()
    command = New SqlCeCommand("select * from users where Name=? and Password=?", con)

    Dim param1, param2 As SqlCeParameter

    param1 = New SqlCeParameter("Name", uname.Text)

    param2 = New SqlCeParameter("Password", pwd.Text)

    command.Parameters.Add(param1)
    command.Parameters.Add(param2)

    Dim reader As SqlCeDataReader = command.ExecuteReader

    If (reader.Read = True) Then
        role = reader.GetString(1)

    Else
        MsgBox("Invalid Login")
    End If

I have this code is working. What to write for

Private Sub frmlogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
+1  A: 

Not sure I completely understand your question, but if the table is empty then reader.Read() would evaluate to False.

I think what you want is a SQL Statement checking the Count of the users table. Something like

command = New SqlCeCommand("SELECT COUNT(Name) as NameCount FROM Users", con)

Then you would evaluate the count by doing something like

Dim reader as SqlCeDataReader = command.ExecuteReader() 
While(reader.Read())
   if reader("NameCount") = 0 then
       'Redirect to Admin Form

   else
       'Run all your current logic here to find the user from the DB
   end if
End While
Overhed
Ok but how to declare Namecount? Sorry for my poor english.
Hakan
Sorry -- NameCount should go in quotes, as it's referencing a column name in your DB. I'm changing my post to reader("NameCount") now.
Overhed
+1  A: 

I would recommend trying the DataReader's HasRows property to determine if one or more rows was returned to the DataReader object.

if (reader.HasRows)
{

    reader.Read();
    role = reader.GetString(1)

}
else
{
    // invalid login
}
dretzlaff17
Thank you very much for helping me.
Hakan