Public Class LoginForm1


Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ahamdi2\Documents\Visual Studio 2008\Projects\sistemPengesanan\sistemPengesanan\admin.mdf;Integrated Security=True;User Instance=True")

Dim cmd As New SqlCommand("SELECT * FROM info WHERE username = '" & UsernameTextBox.Text & "' AND [password] = '" & PasswordTextBox.Text & "' ", con)

Dim cmd2 As New SqlCommand()
cmd2.CommandText = "update info set tarikh_akhir_guna='" & DateAndTime.Now & "'where username= '" & UsernameTextBox.Text & "'"


con.Open()

Dim sdr As SqlDataReader = cmd.ExecuteReader()


' If the record can be queried, it means passing verification, then open another form.
If (sdr.Read() = True) Then

MessageBox.Show("The user is valid!")

UsernameTextBox.Text = " "
PasswordTextBox.Text = ""

cmd2.ExecuteNonQuery()

Me.Hide()
Form1.Show()


Else
MessageBox.Show("Invalid username or password!")
UsernameTextBox.Text = " "
PasswordTextBox.Text = ""
End If

con.Close()

End Sub

Recommended Answers

All 5 Replies

i have an error as mentioned above on executenoquery command for cmd2..could someone help me..

im trying to login and there is no problem with that,
but i want to log the time the user login,that when the problem came
help me plz

The command "cmd2" has the property Connection that has not been initialized. To fix this, you will need to set the Connection property of "cmd2" to the variable "con"
ie. Add the following code immediately after you declare "cmd2" cmd2.Connection = con You need to understand that there are 2 ways of initializing the Connection property of a command object:
1. During the instantiation of the SqlCommand class, you can pass a connection object as the 2nd argument to the SqlCommand constructor. eg. What you did for the cmd object
2. Through direct setting of the Connection property using a connection object as i have illustrated above.

ive already change the code

Dim cmd2 As New SqlCommand("update info set tarikh_akhir_guna='" & DateAndTime.Now & "'where username= '" & UsernameTextBox.Text & "'", con)

thank you sir,but..an error occur said that:
There is already an open DataReader associated with this Command which must be closed first.error is on the cmd2.ExecuteNonQuery()

So trace back where you last used the DataReader object, and call it's Dispose method after using it.
ie. cmd2.Dispose() Otherwise, instead of using the name cmd2, simply use the name cmd3 as your variable name for the sqlcommand.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strsql As String = "Data Source=.\ITSupport;" & _
"Initial Catalog=Information;Integrated Security=true"
strsql = " insert into table_1 (name, address, age ) values ('" _
& txtname.Text & "','" _
& txtaddress.Text & "','" _
& txtage.Text & "')"
Dim sqlcmd As New SqlClient.SqlCommand
sqlcmd.CommandText = strsql
MsgBox("save")

End Sub
End Class

But my data still not exist inside the db. there is no error found.. can some help me on this asap?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.