I am creating a login page using Vb.net ... i have written coding but it shows some Error ..
Here is mine coding ..
..

Dim DataClass As New DataClassesDataContext
Dim l = From Users In DataClass.Users _
Where (Function(x) x.UserName = txtUserName.Text
And x.Password = txtPassword.Text).ToList
If l.Count = 1 Then
Session("username") = txtUserName.Text
Response.Redirect("Masters.aspx")
Else
lb(Msg.Text =("Invalid Login!")

End If

...

I am having Error on line 3 , 4 and 7 ..i think the function i created is right ..but it shows errors

Could any one guide me ..

Thank you

should be:

Dim l = (From Users In DataClass.Users _
Where (Function(x) x.UserName = txtUserName.Text _
And x.Password = txtPassword.Text)).ToList

but you can simplify this...

Dim l = (From Users In DataClass.Users _
Where Users.UserName = txtUserName.Text _
And Users.Password = txtPassword.Text).ToList

Line 7 im not sure but shouldnt it be Response.Redirect("~/Masters.aspx")

Thanks geeks for u breif replay

this is working fine ..

also Now i want to display something if the user puts some wrong password or username ..ie some notifications .. like invalid login or register first .

how can i do that .. pls guide me

Edited 6 Years Ago by kevin025: n/a

IF you allow only unique user names then you can do something like that:

Dim myUser = From u in DataClass.Users WHERE u.UserName = txtUserName.Text.trim
if myUser.count = 0 then
'user does not exist --> forward to register page
return
end if 

if myUser.Password <> txtPassword.Text then
'wrong password 
return
end if

IF you allow only unique user names then you can do something like that:

Dim myUser = From u in DataClass.Users WHERE u.UserName = txtUserName.Text.trim
if myUser.count = 0 then
'user does not exist --> forward to register page
return
end if 

if myUser.Password <> txtPassword.Text then
'wrong password 
return
end if

Let me check with the mentioned code

I am using the below code for login here check mine else and if code ..


If l.Count = 1 Then
Session("username") = txtUserName.Text
Response.Redirect("Masters.aspx")
Else
1.Count =0 Then
lbl(Msg.Text ="Invalid"
End If

here in else condition i have some errors could u check with it ..

If l.Count = 1 Then
Session("username") = txtUserName.Text
Response.Redirect("Masters.aspx")
Elseif l.Count = 0 Then
Msg.Text ="Invalid"
else
'means you have more than 2 user with same name
End If

Edited 6 Years Ago by GeekByChoiCe: n/a

ok ..

again but The same error appears like PROPERTY COUNT IS READ ONLY

here is mine complete coding ..i have problems with else part ..

Dim DataClass As New DataClassesDataContext
Dim l = (From Users In DataClass.Users _
Where Users.UserName = txtUserName.Text _
And Users.Password = txtPassword.Text).ToList
If l.Count = 1 Then
Session("username") = txtUserName.Text
Response.Redirect("Masters.aspx")
Else
l.Count =0 Then
lbl(Msg.Text ="Invalid"

End If


...
thank you

yes and i told you in my last code example how to do it correctly.

Dim DataClass As New DataClassesDataContext
Dim l = (From Users In DataClass.Users _
Where Users.UserName = txtUserName.Text _
And Users.Password = txtPassword.Text).ToList
If l.Count = 1 Then
Session("username") = txtUserName.Text
Response.Redirect("Masters.aspx")
Else
l.Count =0 Then ' You try to set the Count property to a new value! This is NOT allowed
lbl(Msg.Text ="Invalid"

End If

Solution for that is also posted in my last code example.

thanks geeks ... now i got it ..

also as i asked earlier to display something if the user puts some wrong password or username ..ie some notifications .. like invalid login or register first ...how can i do it here within the above mentioned coding

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