[RESOLVED] [2005] Login form code (no errors, does nothing)
Hi all,
I decided to rewrite my login code because my last one would only find the first record and anything after that first record (additional records) werent even found but they were in the database (mssql express).
I have a function that returns the connection string as follows:
I then have a public object for my connection and then I assign the connection string as follows:
vb Code:
Public m_objCon As New SqlClient.SqlConnection(ConString)
When I enter in values in the username and password textboxes and click on the login button, it should do many checks and then log in if those checks are met for being valid. However, when i click on the login button, I get no errors and nothing happens at all. I even went to do a breakpoint and that isnt even executed. nothing happens at all. Please see code in the following three posts and advise if you see anything out of place. i am pretty new to .net so bear with me. the code is pretty long.....
Last edited by BrailleSchool; Jul 28th, 2007 at 12:50 PM.
Re: [2005] Login form code (no errors, does nothing)
ok, i am getting very frustrated with .net right about now. no matter how i code this damned thing, it never sees past the first record!!!!!!
almost to the point of quitting this altogether. please advise what i am doing wrong. both the data sets are being populated with the SAME record regardless!!!!
complete code in txt file attachment. thanks in advance for any help.
vb Code:
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
'declare some datasets, data adapters and boolean flags
Dim objAdminUserDs As New DataSet, objRegularUserDs As New DataSet
Dim blnAdminUser, blnRegularUser, blnAccountLocked As Boolean
'set up new sql commands and assign the sql query
Dim strAdminUserSQL As String = "SELECT * " & _
"FROM Users " & _
"WHERE IsAdmin = '1'"
Dim strRegularUserSQL As String = "SELECT * " & _
"FROM Users " & _
"WHERE IsAdmin = '0'"
Dim objAdminUserDa As New SqlClient.SqlDataAdapter(strAdminUserSQL, m_objCon)
Dim objRegularUserDa As New SqlClient.SqlDataAdapter(strRegularUserSQL, m_objCon)
'Try
'open the connection if its closed
If m_objCon.State = ConnectionState.Closed Then
m_objCon.Open()
End If
'clear the data sets and then fill the data adapters with the data sets
objAdminUserDs.Clear()
objAdminUserDa.Fill(objAdminUserDs)
objRegularUserDs.Clear()
objRegularUserDa.Fill(objRegularUserDs)
'if there are more than zero rows in the admin dataset then
Re: [2005] Login form code (no errors, does nothing)
Originally Posted by BrailleSchool
i took out the try/catch block because that wasnt showing me errors and i think ive figured it out. time for more testing and will be back.
In fact, that is the first thing that catches my eye in the last bit of code. The Try...Catch block is there so if you do experience an error in your code, you can respond to it somehow. Even it is only to show a MessageBox with the error message, you should really put some code in your Catch block. Otherwise, as you've discovered, you won't get any information at all.
Don't remove the Try...Catch block. Just do something with the error when you catch it.
Re: [2005] Login form code (no errors, does nothing)
Originally Posted by nmadd
In fact, that is the first thing that catches my eye in the last bit of code. The Try...Catch block is there so if you do experience an error in your code, you can respond to it somehow. Even it is only to show a MessageBox with the error message, you should really put some code in your Catch block. Otherwise, as you've discovered, you won't get any information at all.
Don't remove the Try...Catch block. Just do something with the error when you catch it.
i have totally rewritten this code from scratch 3 times now and doesnt matter how i do it, it only sees the first record and no other records. i currently have two users in the db. the first one can login without issue and the second one "cant be found" when in fact everything is typed in right. argh! (post #6 has additional info)
Re: [2005] Login form code (no errors, does nothing)
1) So you said you removed the Try...Catch block so you could see the errors. What where they?
2) Is your IsAdmin field a number field? There should not be single quotes around the 1 and 0 if that is so.
Re: [2005] Login form code (no errors, does nothing)
i even changed it to the following an no change. can only see the first user and the dataset isnt updated with any other records. ugh. i am totally lost with .net
vb Code:
'declare some datasets, data adapters and boolean flags
Dim objUsersDs As New DataSet
Dim blnAdminUser, blnRegularUser, blnCheckForAdmin, blnAccountLocked As Boolean
'set up new sql commands and assign the sql query
Dim strUsersSQL As String = "SELECT * " & _
"FROM Users"
Dim objUsersDa As New SqlClient.SqlDataAdapter(strUsersSQL, m_objCon)
'Try
'open the connection if its closed
If m_objCon.State = ConnectionState.Closed Then
m_objCon.Open()
End If
'clear the data set and then fill the data adapter with the data set
objUsersDs.Clear()
objUsersDa.Fill(objUsersDs)
'if there are more than zero rows in the admin dataset then
Re: [2005] Login form code (no errors, does nothing)
Originally Posted by nmadd
1) So you said you removed the Try...Catch block so you could see the errors. What where they?
2) Is your IsAdmin field a number field? There should not be single quotes around the 1 and 0 if that is so.
Re: [2005] Login form code (no errors, does nothing)
So you only get one record back when you are certain there is more than one record in your database table?
How many rows does this return?
Code:
Dim strUsersSQL As String = "SELECT * " & _
"FROM Users"
Dim objUsersDa As New SqlClient.SqlDataAdapter(strUsersSQL, m_objCon)
Dim dt As New DataTable()
objUsersDa.Fill(dt)
MessageBox.Show(dt.Rows.Count.ToString())
Re: [2005] Login form code (no errors, does nothing)
when the messagebox comes up, it has the number 2 in it.
i have gotten rid of the dataset and gone to datatable instead. whats the difference between the dataset and datatable?
still no change. first user has no issues. 2nd one cant be found. ugh
Originally Posted by nmadd
So you only get one record back when you are certain there is more than one record in your database table?
How many rows does this return?
Code:
Dim strUsersSQL As String = "SELECT * " & _
"FROM Users"
Dim objUsersDa As New SqlClient.SqlDataAdapter(strUsersSQL, m_objCon)
Dim dt As New DataTable()
objUsersDa.Fill(dt)
MessageBox.Show(dt.Rows.Count.ToString())
Re: [2005] Login form code (no errors, does nothing)
Originally Posted by BrailleSchool
when the messagebox comes up, it has the number 2 in it.
That means there are two records there. That's what you want correct?
A DataSet is just a collection of DataTables. Check them both out on MSDN.
I imagine that you are getting the second one because you are overwriting the first when you loop through all of the rows in that table.
You need to step through this code and see what is going on. Slap a breakpoint on the first line in your block of code and actually walk through it and watch all of your variables and actually see how and when they change.
Re: [2005] Login form code (no errors, does nothing)
Ok. So, there is probably a better way to do this, but using something like your code, this works for me using the NorthWind database:
Code:
Public Class Form1
Private _productName As String
Private _productID As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Nwind.mdb;")
Dim SQLString1 As String = _
"SELECT ProductID, ProductName " & _
"FROM Products " & _
"WHERE ProductID = -1"
Dim SQLString2 As String = _
"SELECT ProductID, ProductName " & _
"FROM Products " & _
"WHERE ProductID = 2"
Dim da1 As New OleDbDataAdapter(SQLString1, conn)
Dim da2 As New OleDbDataAdapter(SQLString2, conn)
Dim dt1 As New DataTable()
Dim dt2 As New DataTable()
da1.Fill(dt1)
da2.Fill(dt2)
If dt1.Rows.Count > dt2.Rows.Count Then
_productID = CInt(dt1.Rows(0).Item("ProductID"))
_productName = dt1.Rows(0).Item("ProductName").ToString()
Else
_productID = CInt(dt2.Rows(0).Item("ProductID"))
_productName = dt2.Rows(0).Item("ProductName").ToString()
End If
MessageBox.Show( _
_productID & _
Environment.NewLine & _
_productName)
End Sub
End Class
Re: [2005] Login form code (no errors, does nothing)
thanks for that. i think i am making some headway here but gonna test further to make sure.
when an admin user logs into the system, they have complete access to everything. on form load, so far I have three menus that are disabled but when the admin user logs in, they need to be enabled. so in my frmLogin form, I have used DirectCast() but when I go to login as an admin, i get the following error in the screen shot.
i am not sure how to access controls on another form and had asked this question in the forum last week (didnt get a response though) but is this the way you access controls on another form or is there another way?