Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to retrieve data from the database based on the enter value on the textbox and display the result on the corresponding textboxes. But i am having trouble doing it. So far all i can do is retrieve data of only one person which was recorded first in the database. It doesn't compare with the remaining rows in the database. So i really need help..help me out guys....Many many thanks in advance...My code as is follows;

VB
Private Function Retrieve_EmpNo() As Integer

        Dim connection As New OleDbConnection(My.Settings.ConnectionString)
        Dim adapter As New OleDbDataAdapter("SELECT EmpNo FROM Emp_Details", connection)
        Dim dt As New DataTable
        Dim filter As DataView = Nothing
        Dim ds As New DataSet()

        adapter.Fill(ds, "Emp_Details")

        If TextBox1.Text = ds.Tables(0).Rows(0)(0) Then
            Return 1
        Else
            Return 0
        End If


    End Function


  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Short

        i = Retrieve_EmpNo()

        If i = 1 Then
            Dim str As String

            str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|PWDdb.mdb"
            connection.ConnectionString = str
            command.CommandType = CommandType.Text
            connection.Open()
            command.Connection = connection
            adapter.SelectCommand = command
            adapter.TableMappings.Add("table", "Emp_Details")
            ds = New Data.DataSet("Emp_Details")
            adapter.Fill(ds)

            TextBox2.DataBindings.Add("Text", ds, "table.Name")
            TextBox3.DataBindings.Add("Text", ds, "table.Department")
            TextBox4.DataBindings.Add("Text", ds, "table.Designation")
            TextBox5.DataBindings.Add("Text", ds, "table.Basicpay")
        Else
            MessageBox.Show("Employee ID not found!!", "PWD", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
 End Sub
Posted
Updated 12-May-10 2:22am
v2

damaxy wrote:
So far all i can do is retrieve data of only one person which was recorded first in the database. It doesn't compare with the remaining rows in the database.


Thats what you are doing here

damaxy wrote:
If TextBox1.Text = ds.Tables(0).Rows(0)(0) Then
Return 1
Else
Return 0
End If


You are checking only for 1st record, if first empno is entered in TextBox then you are displaying it, otherwise you are not displaying any record.

You have to change this "if" condition if you want to display other records too.
 
Share this answer
 
v2
I'm not entirely clear what you think you're doing with what you've written.

What is in TextBox1? (By the way, you should really set your own names for the controls so it's clear...like txtEmpNo. Then, others wouldn't have to guess...nor would you in the future).

Secondly, it doesn't look like you understand databases or how to extract information from them. I assume this is what you want:

A person types in an Employee Number. Then, you go into the database and see if it's valid. If it is, then you get the information about that employee.

So, you need to actually look into SQL and what is available. First, you need to specify a "WHERE" paramater if you want to search for something specific. Secondly, I would look at the SQLCommand.ExecuteScalar method. This will return only the first column and the first row of the query. So, if your query said
SQL
SELECT dayNumber FROM JulianCalendar WHERE (Month=3 AND Day=2 AND Year=2007)

it would return "2454162" if there was a match in the database or Nothing

You also don't set a SELECT string on your button click to set the DataBindings...and really, I wouldn't even use DataBindings. I would just fill the table with the select, which should only give you one row and then just fill the TextBox values with what is in the columns.

You're going about this task all wrong.
 
Share this answer
 
If I were you I would use a more structured Object Oriented approach. What you should do is create an Employee Class with properties such as EmployeeNumber, Name, Department, Designation, BasicPay. Then you could have a shared method in that class that returns an Employee based on an EmployeeNumber like this:

VB.NET
Public Shared Function GetEmployee(ByVal EmployeeNumber As Integer, ByVal ConnectionString As String) As Employee

        Using conEmployees As New OleDbConnection(ConnectionString)
            Dim newEmployee As New Employee
            Dim cmdEmployee As New OleDbCommand("SELECT Name, Department, Designation, BasicPay FROM Emp_Details WHERE EmployeeNumber = @EmpNum", conEmployees)
            cmdEmployee.Parameters.AddWithValue("@EmpNum", EmployeeNumber)
            conEmployees.Open()
            Dim dbrEmployee As OleDbDataReader = cmdEmployee.ExecuteReader
            While dbrEmployee.Read
                With newEmployee
                    If Not IsDBNull(dbrEmployee(0)) Then .Name = dbrEmployee.GetString(0) Else .Name = String.Empty
                    If Not IsDBNull(dbrEmployee(1)) Then .Department = dbrEmployee.GetString(1) Else .Department = String.Empty
                    If Not IsDBNull(dbrEmployee(2)) Then .Designation = dbrEmployee.GetString(2) Else .Designation = String.Empty
                    If Not IsDBNull(dbrEmployee(3)) Then .BasicPay = dbrEmployee.GetDecimal(3) Else .BasicPay = 0D
                End With
            End While
            Return newEmployee
        End Using

    End Function


Then in your click event handler you just need to do this:

VB
Try
            Dim CurrentEmployee As Employee = Employee.GetEmployee(EmployeeNumber, My.Settings.ConnectionString, )
            txtName.Text = CurrentEmployee.Name
            txtDepartment.Text = CurrentEmployee.Department
            txtDesignation.Text = CurrentEmployee.Designation
            txtBasicPay.Text = CurrentEmployee.BasicPay
        Catch ex As OleDbException
            MsgBox("There was an error retieving Employee Data.")
        End Try


Hope this helps

Happy Coding
 
Share this answer
 
v2
Now thats what i'm talking about..thanks alot, zimvbcoder..i know the way that i did was confusing..thats why i asked for help..i got what i was looking for..God bless ya...
 
Share this answer
 
Comments
Wayne Gaylard 13-May-10 9:39am    
Pleasure and Thank you. Although I would have thought it deserved more than a 2 vote.
Hello,
Looking at your question u want to retrieve data from the database right then hers your code:

I used SQL as a database u can do the same with OLEDB just change the provider name and your done

Imports System.Data.SqlClient

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True")
        con.Open()
        Dim query As String = "select pass from mytable where user1='" + TextBox1.Text + "'"

        Dim cmd As SqlCommand = New SqlCommand(query, con)
        Dim da As SqlDataReader = cmd.ExecuteReader()
        If (da.Read() = True) Then
            TextBox2.Text = da(0).ToString()
            TextBox2.UseSystemPasswordChar = True
        Else
            MessageBox.Show("Database scan complete no such user found")

        End If

        con.Close()
        con.Dispose()
    End Sub
End Class


Do rate my answer once you find it useful
Thanks & Regards
Radix :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900