14 Replies - 918 Views - Last Post: 15 September 2011 - 01:54 AM Rate Topic: -----

#1 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 06:04 PM

helo.. just want to ask what would be the possible code if there are no records in database.. example.. when you click search.. then there! well.. here is my code take a look of this thanks..
Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If textSearch.Text = "" Then
            MsgBox("Please Enter an Employee name", MsgBoxStyle.Exclamation, "Invalid!")
        Else
            Dim cons As New MySqlConnection("Database=phnsvd;Server=localhost;user id=root;password= ;port=3306;charset=utf8")
            Dim cmds As New MySqlCommand("SELECT * FROM employee WHERE empName = '" & textSearch.Text & "'", cons)
            Dim rdrs As MySqlDataReader
            cons.Open()
            rdrs = cmds.ExecuteReader()
            rdrs.Read()
            tbname.Text = rdrs(1)
            tbLastname = rdrs(2)
            tbadd.Text = rdrs(3)
            tbbday.Text = rdrs(4)
            tbcnum.Text = rdrs(5)
            tbstatus.Text = rdrs(6)
            tbnumdep.Text = rdrs(7)
            tbdatehired.Text = rdrs(8)
            tbjobtit.Text = rdrs(9)
            basicSalary.Text = rdrs(10)
            semiSalary.Text = rdrs(11)
            overtime.Text = rdrs(12)
            tax.Text = rdrs(13)
            sss.Text = rdrs(14)
            pagibig.Text = rdrs(15)
            philhealth.Text = rdrs(16)
            tardiness.Text = rdrs(17)
            sickly.Text = rdrs(18)
            tardiness.Text = rdrs(19)
            sssLoan.Text = rdrs(20)
            cash.Text = rdrs(21)
            pagibigLoan.Text = rdrs(22)


            Me.tbname.Enabled = False
            Me.tbLastname.Enabled = False
            Me.tbadd.Enabled = False
            Me.tbbday.Enabled = False
            Me.tbcnum.Enabled = False
            Me.tbstatus.Enabled = False
            Me.tbnumdep.Enabled = False
            Me.tbdatehired.Enabled = False
            Me.tbjobtit.Enabled = False
            Me.basicSalary.Enabled = False
            Me.semiSalary.Enabled = False
            Me.overtime.Enabled = False
            Me.tax.Enabled = False
            Me.sss.Enabled = False
            Me.pagibig.Enabled = False
            Me.philhealth.Enabled = False
            Me.tardiness.Enabled = False
            Me.sickly.Enabled = False
            Me.tardiness.Enabled = False
            Me.sssLoan.Enabled = False
            Me.cash.Enabled = False
            Me.pagibigLoan.Enabled = False

            textSearch.Text = ""

            cons.Close()
        End If
    End Sub

This post has been edited by modi123_1: 14 September 2011 - 06:08 PM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

#2 Tehdead  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 07:36 PM

Well I've never worked with MySql in VB but if their is no "record" of
said employee then wouldn't checking your reader object return nothing aswell?

Dim rdrs As MySqlDataReader
 cons.Open()
 rdrs = cmds.ExecuteReader()
 rdrs.Read()

 if not rdrs(0) = nothing then
'your code
else
msgbox("No such employee exists")
end if 



again never worked with MySql but looking at your code, if tbname.Text = rdrs(1). then you can check to make sure the rdrs(1) actually contains something. (i used rdrs(0) becuase I like make checks at the beginning of a sequence)
Was This Post Helpful? 1
  • +
  • -

#3 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 08:36 PM

hmm rdrs 0.? we didint included that in the search anymore.. i've tried it already but it didnt work.. i place the code like this..
            Dim cons As New MySqlConnection("Database=phnsvd;Server=localhost;user id=root;password= ;port=3306;charset=utf8")
            Dim cmds As New MySqlCommand("SELECT * FROM employee WHERE empName = '" & textSearch.Text & "'", cons)
            Dim rdrs As MySqlDataReader
            cons.Open()
            rdrs = cmds.ExecuteReader()
            rdrs.Read()
            If Not textSearch.Text = Nothing Then
                MsgBox("No such employee exists. Would you like to Add?", MsgBoxStyle.Information, "Invalid")
                tbname.Text = rdrs(1)
                tbadd.Text = rdrs(2)
                tbbday.Text = rdrs(3)
                tbcnum.Text = rdrs(4)
                tbstatus.Text = rdrs(5)
                tbnumdep.Text = rdrs(6)
                tbdatehired.Text = rdrs(7)
                tbjobtit.Text = rdrs(8)
                basicSalary.Text = rdrs(9)
                semiSalary.Text = rdrs(10)
                overtime.Text = rdrs(11)
                tax.Text = rdrs(12)
                sss.Text = rdrs(13)
                pagibig.Text = rdrs(14)
                philhealth.Text = rdrs(15)
                tardiness.Text = rdrs(16)
                sickly.Text = rdrs(17)
                tardiness.Text = rdrs(18)
                sssLoan.Text = rdrs(19)
                cash.Text = rdrs(20)
                pagibigLoan.Text = rdrs(21)


This post has been edited by macosxnerd101: 14 September 2011 - 08:37 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 Tehdead  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 09:29 PM

If Not textSearch.Text = Nothing Then


This just confuses me as it is always true(I.E. what happens when you search for Little Richie?), ofcourse inless you don't put anything in the TextSearch which then it would make this statement false.

As for rdrs(0) I was just assuming since your using .Read() that your rdrs array should start at 0 and not 1. An it ponders me that you skip it... must be there for some reason, did it error? or just return nothing?...Did you try multiple "searches" ?

Though if it doesn't always return a value then it would be best to use rdrs(1)

If Not rdrs(1) = Nothing Then

Was This Post Helpful? 0
  • +
  • -

#5 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 10:04 PM

hmm.. just the same.. it run but it creates error just like the first one..

can you try to look again in my codes sir.?

    Private Sub searchButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click
        If textSearch.Text = "" Then
            MsgBox("Please Enter an Employee name", MsgBoxStyle.Exclamation, "Invalid!")   
        Else
            Dim cons As New MySqlConnection("Database=phnsvd;Server=localhost;user id=root;password= ;port=3306;charset=utf8")
            Dim cmds As New MySqlCommand("SELECT * FROM employee WHERE empName = '" & textSearch.Text & "'", cons)
            Dim rdrs As MySqlDataReader
            cons.Open()
            rdrs = cmds.ExecuteReader()
            rdrs.Read()

            If Not rdrs(1) = Nothing Then
                Dim a As String
                a = MsgBox("No such employee exists. Would you like to Add?", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "Invalid")
                If a = vbYes Then
                    Me.Show()
                Else
                    MainMenu.Show()
                End If

                tbname.Text = rdrs(1)
                tbadd.Text = rdrs(2)
                tbbday.Text = rdrs(3)
                tbcnum.Text = rdrs(4)
                tbstatus.Text = rdrs(5)
                tbnumdep.Text = rdrs(6)
                tbdatehired.Text = rdrs(7)
                tbjobtit.Text = rdrs(8)
                basicSalary.Text = rdrs(9)
                semiSalary.Text = rdrs(10)
                overtime.Text = rdrs(11)
                tax.Text = rdrs(12)
                sss.Text = rdrs(13)
                pagibig.Text = rdrs(14)
                philhealth.Text = rdrs(15)
                tardiness.Text = rdrs(16)
                sickly.Text = rdrs(17)
                tardiness.Text = rdrs(18)
                sssLoan.Text = rdrs(19)
                cash.Text = rdrs(20)
                pagibigLoan.Text = rdrs(21)



                Me.tbname.Enabled = False
                Me.tbadd.Enabled = False
                Me.tbbday.Enabled = False
                Me.tbcnum.Enabled = False
                Me.tbstatus.Enabled = False
                Me.tbnumdep.Enabled = False
                Me.tbdatehired.Enabled = False
                Me.tbjobtit.Enabled = False
                Me.basicSalary.Enabled = False
                Me.semiSalary.Enabled = False
                Me.overtime.Enabled = False
                Me.tax.Enabled = False
                Me.sss.Enabled = False
                Me.pagibig.Enabled = False
                Me.philhealth.Enabled = False
                Me.tardiness.Enabled = False
                Me.sickly.Enabled = False
                Me.tardiness.Enabled = False
                Me.sssLoan.Enabled = False
                Me.cash.Enabled = False
                Me.pagibigLoan.Enabled = False
                cons.Close()
            End If

            textSearch.Text = ""
            End If

    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 Tehdead  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 10:16 PM

That error message would be of great use, without it I have no clue.
Was This Post Helpful? 0
  • +
  • -

#7 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 10:23 PM

this was the said error sir..

Invalid attempt to access a field before calling Read()
Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 10:39 PM

First you have to know that when you say rdrs(1) means the second column of the table. Then we always use while loop to loop in data reader like
 While rdrs.Read()
   'do manipulation here

 End While

This makes sure it reads when the data are in, and to assign the value of a column to a certain variable we say
tbname.Text = rdrs.Item(1)
and not
tbname.Text = rdrs(1)
and they should be in while
Since you know that rdrs(0) means column 1, then it is adviced to use name instead of index like rdrs("Column name").
Was This Post Helpful? 1
  • +
  • -

#9 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 10:53 PM

yay! it works ! :DD haha thank you sir!
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 10:57 PM

Glad we could help :)
Was This Post Helpful? 1
  • +
  • -

#11 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 11:23 PM

yay! hmm.. it seems i need another help sir.. on how to updATE the user password.. the code goes like this
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim con As New MySqlConnection("Database=phnsvd;server=localhost;user id=root; password= ;port=3306; charset=utf8")
        Dim cmd As New MySqlCommand("UPDATE userlogin SET `login` VALUE = '" & Me.tbnewpass.Text & "' ,  = '" & Me.tbconpass.Text & "'", con)
        Dim rdr As MySqlDataReader
        con.Open()
        rdr = cmd.ExecuteReader()
        rdr.Read()
        con.Close()
        MsgBox("Password has been updated!", MsgBoxStyle.OkOnly)
    End Sub


Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 14 September 2011 - 11:34 PM

Your update command is wrong, it should look like
"UPDATE userlogin SET login = '" & Me.tbnewpass.Text & "' ,SOMETHING MISSING HERE  = '" & Me.tbconpass.Text & "' WHERE ...."

I dont know how your table looks like
UPDATE tableName 
SET columnName = newVAlue
WHERE condition

Also you are saving so: cmd.ExecuteNonQuery()
Was This Post Helpful? 0
  • +
  • -

#13 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 15 September 2011 - 12:26 AM

hmm.. it just have the old password.. next is the new password \the confirmation.. hmm.. we just want to update the set log in password w/c is "Password" in the userlogin table.. and the column name is login..
Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 15 September 2011 - 12:38 AM

You must have a WHERE condition or all table will be updated to a new value. like
"UPDATE userlogin SET login = 'newPassword' WHERE userName = 'userName'"
this makes sure we only update one row which this user is..

This post has been edited by smohd: 15 September 2011 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

#15 licel  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 79
  • Joined: 13-September 11

Re: NO DATA FOUND IN DATABASE WHAT WOULD BE THE CODE?

Posted 15 September 2011 - 01:54 AM

aah thanks a lot! weve finished the change password already.. hmm :)) well.. can i ask another.?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1