[2005] Why can't I retrieve from database?
I can't show the fields from the database after I enter the username and password. What should I do? Can give me codes as eg?
My first page to call the second page's code:
VB Code:
Public Class LoginFormf
' TODO: Insert code to perform custom authentication using the provided username and password
' (See [url]http://go.microsoft.com/fwlink/?LinkId=35339)[/url].
' The custom principal can then be attached to the current thread's principal as follows:
' My.User.CurrentPrincipal = CustomPrincipal
' where CustomPrincipal is the IPrincipal implementation used to perform authentication.
' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
' such as the username, display name, etc.
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\db1.mdb"
Dim con As OleDb.OleDbConnection
Dim dr As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand
'ItemId is the column name of the database
'from item. item is the name of the table
' PasswordTextBox is the name of the textbox
Try
Dim strselect As String = "Select UserName, ItemId from item where UserName = '" & UsernameTextBox.Text & "' And ItemId = '" & PasswordTextBox.Text & "'"
'create a new connection
con = New OleDb.OleDbConnection(strcon)
con.Open()
cmd.Connection = con
cmd.CommandText = strselect
dr = cmd.ExecuteReader
If dr.Read Then
If UsernameTextBox.Text = dr("UserName") And PasswordTextBox.Text = dr("ItemId") Then
EditForm1.Show()
End If
Else
MessageBox.Show("UserId or isbn is not valid")
UsernameTextBox.Clear()
PasswordTextBox.Clear()
End If
dr.Close()
dr.Close()
Catch eException As Exception
MessageBox.Show("Cannot read from database")
End Try
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub UsernameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameTextBox.TextChanged
End Sub
End Class
Second page:
VB Code:
Public Class EditForm1
Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
End Sub
Private Sub EditForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\db1.mdb"
Dim con As OleDb.OleDbConnection
Dim dr As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand
Try
Dim strselect As String = "Select * from item where ItemId = '" & LoginForm.PasswordTextBox.Text & "' And UserName ='" & LoginForm.UsernameTextBox.Text & "'"
'create new connection
con = New OleDb.OleDbConnection(strcon)
con.Open()
cmd.Connection = con
cmd.CommandText = strselect
dr = cmd.ExecuteReader
While dr.Read
TextBox1.Text = dr("ItemId")
TextBox3.Text = dr("ItemTitle")
TextBox5.Text = dr("ItemCategory")
ComboBox1.Text = dr("ItemClassification")
ComboBox2.Text = dr("Status")
TextBox4.Text = dr("AuthorName")
TextBox8.Text = dr("DateOfPublished")
TextBox9.Text = dr("DateCreated")
TextBox5.Text = dr("CostPriceOfItem")
TextBox7.Text = dr("PublisherName")
TextBox2.Text = dr("ReferenceNo")
TextBox6.Text = dr("Edition")
End While
dr.Close()
' dr.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\db1.mdb"
Dim con As OleDb.OleDbConnection
Dim dr As OleDb.OleDbDataReader
Dim cmd As New OleDb.OleDbCommand
Try
Dim strsql As String = "Update item set ReferenceNo = '" & TextBox2.Text & "',ItemTitle = '" & TextBox3.Text & "', AuthorName='" & TextBox4.Text & "', DateOfPublished='" & TextBox8.Text & "', PublisherName='" & TextBox7.Text & "', ItemClassification='" & ComboBox1.Text & "',CostPriceOfItem='" & TextBox5.Text & "',Edition='" & TextBox6.Text & "',ItemCategory='" & ComboBox3.Text & "',Status='" & ComboBox2.Text & "', DateCreated='" & TextBox9.Text & "',DateUpdated='" & Format(DateTimePicker1.Value) & "' where ItemId='" & TextBox1.Text & "'"
'create new connection
con = New OleDb.OleDbConnection(strcon)
con.Open()
cmd.Connection = con
cmd.CommandText = strsql
dr = cmd.ExecuteReader
MsgBox("Update Record Success!")
dr.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'MsgBox("Details of item has already been updated!")
'Me.Close()
End Sub
End Class
Re: [2005] Why can't I retrieve from database?
Set a breakpoint where you check the username and pass in code, and see what the values are... step line by line to see the flow of the code in order to figure out what is going on... basic debugging techniques...
Re: [2005] Why can't I retrieve from database?