Results 1 to 3 of 3

Thread: [2005] Why can't I retrieve from database?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    58

    Smile [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:
    1. Public Class LoginFormf
    2.  
    3.     ' TODO: Insert code to perform custom authentication using the provided username and password
    4.     ' (See [url]http://go.microsoft.com/fwlink/?LinkId=35339)[/url].  
    5.     ' The custom principal can then be attached to the current thread's principal as follows:
    6.     '     My.User.CurrentPrincipal = CustomPrincipal
    7.     ' where CustomPrincipal is the IPrincipal implementation used to perform authentication.
    8.     ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    9.     ' such as the username, display name, etc.
    10.  
    11.     Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    12.         Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\db1.mdb"
    13.         Dim con As OleDb.OleDbConnection
    14.         Dim dr As OleDb.OleDbDataReader
    15.         Dim cmd As New OleDb.OleDbCommand
    16.  
    17.         'ItemId is the column name of the database
    18.         'from item. item is the name of the table
    19.         ' PasswordTextBox is the name of the textbox
    20.  
    21.         Try
    22.  
    23.             Dim strselect As String = "Select UserName, ItemId  from item where UserName = '" & UsernameTextBox.Text & "' And ItemId = '" & PasswordTextBox.Text & "'"
    24.  
    25.             'create a new connection
    26.             con = New OleDb.OleDbConnection(strcon)
    27.             con.Open()
    28.             cmd.Connection = con
    29.             cmd.CommandText = strselect
    30.             dr = cmd.ExecuteReader
    31.  
    32.             If dr.Read Then
    33.  
    34.  
    35.                 If UsernameTextBox.Text = dr("UserName") And PasswordTextBox.Text = dr("ItemId") Then
    36.                     EditForm1.Show()
    37.  
    38.                 End If
    39.  
    40.  
    41.             Else
    42.                 MessageBox.Show("UserId or isbn is not valid")
    43.                 UsernameTextBox.Clear()
    44.                 PasswordTextBox.Clear()
    45.             End If
    46.             dr.Close()
    47.             dr.Close()
    48.  
    49.         Catch eException As Exception
    50.             MessageBox.Show("Cannot read from database")
    51.         End Try
    52.     End Sub
    53.  
    54.     Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    55.         Me.Close()
    56.     End Sub
    57.  
    58.     Private Sub UsernameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameTextBox.TextChanged
    59.  
    60.     End Sub
    61. End Class

    Second page:

    VB Code:
    1. Public Class EditForm1
    2.  
    3.     Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click
    4.  
    5.     End Sub
    6.  
    7.     Private Sub EditForm1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\db1.mdb"
    9.         Dim con As OleDb.OleDbConnection
    10.         Dim dr As OleDb.OleDbDataReader
    11.         Dim cmd As New OleDb.OleDbCommand
    12.  
    13.         Try
    14.             Dim strselect As String = "Select * from item where ItemId = '" & LoginForm.PasswordTextBox.Text & "' And UserName ='" & LoginForm.UsernameTextBox.Text & "'"
    15.  
    16.  
    17.             'create new connection
    18.             con = New OleDb.OleDbConnection(strcon)
    19.             con.Open()
    20.             cmd.Connection = con
    21.             cmd.CommandText = strselect
    22.             dr = cmd.ExecuteReader
    23.  
    24.             While dr.Read
    25.                 TextBox1.Text = dr("ItemId")
    26.                 TextBox3.Text = dr("ItemTitle")
    27.                 TextBox5.Text = dr("ItemCategory")
    28.                 ComboBox1.Text = dr("ItemClassification")
    29.                 ComboBox2.Text = dr("Status")
    30.                 TextBox4.Text = dr("AuthorName")
    31.                 TextBox8.Text = dr("DateOfPublished")
    32.                 TextBox9.Text = dr("DateCreated")
    33.                 TextBox5.Text = dr("CostPriceOfItem")
    34.                 TextBox7.Text = dr("PublisherName")
    35.                 TextBox2.Text = dr("ReferenceNo")
    36.                 TextBox6.Text = dr("Edition")
    37.  
    38.  
    39.             End While
    40.             dr.Close()
    41.  
    42.             ' dr.Close()
    43.         Catch ex As Exception
    44.             MessageBox.Show(ex.Message)
    45.         End Try
    46.     End Sub
    47.  
    48.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    49.         Dim strcon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\db1.mdb"
    50.         Dim con As OleDb.OleDbConnection
    51.         Dim dr As OleDb.OleDbDataReader
    52.         Dim cmd As New OleDb.OleDbCommand
    53.  
    54.         Try
    55.             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 & "'"
    56.             'create new connection
    57.             con = New OleDb.OleDbConnection(strcon)
    58.             con.Open()
    59.             cmd.Connection = con
    60.             cmd.CommandText = strsql
    61.             dr = cmd.ExecuteReader
    62.             MsgBox("Update Record Success!")
    63.             dr.Close()
    64.  
    65.         Catch ex As Exception
    66.             MessageBox.Show(ex.Message)
    67.         End Try
    68.  
    69.  
    70.  
    71.  
    72.         'MsgBox("Details of item has already been updated!")
    73.         'Me.Close()
    74.  
    75.     End Sub
    76. End Class

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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...

  3. #3
    Hyperactive Member babyekc's Avatar
    Join Date
    Jul 2004
    Location
    planet earth
    Posts
    270

    Re: [2005] Why can't I retrieve from database?

    do u know how to debug?
    *wink wink*

    _babyekc

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width