Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

im having problem with this code, a want to view a count of record into a text box from mysql but when i click the button it will only unload the window and nothings happen. Please help, so far this is my code for your reference.

VB
Imports MySql.Data.MySqlClient

Public Class frm_login
    Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click
        txt_count.Text = idverifier()
        frm_main.txt_remshd.Text = txt_login.Text
    End Sub

    Private Sub pb_exit1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pb_exit1.Click
        '  End
    End Sub

    Private Sub txt_login_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_login.TextChanged
        Me.txt_login.CharacterCasing = CharacterCasing.Upper
    End Sub

    Function idverifier() As Integer
        Using sqlconnection As New MySqlConnection(cnString)
            sqlconnection.Open()
            Dim sqlcommand As New MySqlCommand("Select COUNT(*) FROM tbl_account WHERE shd_agent = 'JONATHANT'")
            Return sqlcommand.ExecuteScalar()
            sqlconnection.Close()
        End Using
    End Function
End Class
Posted
Updated 27-May-13 9:52am
v4

Your query should looks like:
SQL
SELECT COUNT(*)
FROM tbl_account
WHERE shd_agent LIKE 'JONA%'

Above query mathes: JONATHAN, JONAHTAN, JONATTHA, etc.

If you want to get exact match:
SQL
SELECT COUNT(*)
FROM tbl_account
WHERE shd_agent = 'JONATHAN'


See here:
http://dev.mysql.com/doc/refman/5.0/en/pattern-matching.html[^]
 
Share this answer
 
Comments
athan_makubex 27-May-13 15:29pm    
Hi Maciej Los thank you for your quick response. I tried your suggestion but still when i click the button the window unloads. Any idea why it is hapenning? thanks
Maciej Los 27-May-13 15:33pm    
Please, debug your program and tell me in which line what kind of error you are getting...
Maciej Los 27-May-13 16:04pm    
OK, i see changes now.
Do you have 2 forms and do you want to pass data between forms?
Do you set DialogResult property for btn_Login button? Which?
Hi Maciej Los,

I've solved it! thanks for your help. I use this code instead of using function.

VB
Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click

        Dim con As New MySqlConnection(cnString)
        con.Open()
        Dim cmd As New MySqlCommand("Select Count(*) from tbl_account where shd_agent = 'JONATHAN' ", con)
        Dim i As Integer = cmd.ExecuteScalar()
        cmd = Nothing
        con.Close()
        txt_count.Text = i

End Sub
 
Share this answer
 
There is already a solution but, here is an alternative one for interested people;

Record count in DataGridView and MySQLTable, comparing them with each other. I think it will be useful.

http://koraykaraman.com/koray/software/vb-net/record-count-mysql-vb-net.html[^]

Koray
 
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