Click here to Skip to main content
15,851,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have this add/edit/delete project, I'm able to finish the adding function and now I'm on the viewing data from the database on text boxes. The thing is I don't know yet why data doesn't appear even if I have followed syntax forms correctly as I think so.

Here's the code I have:
VB
Private Sub register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       firstnametb.Focus()

       Call Connection()
       Try
           strSql = "SELECT * FROM add_edit_delete;"
           cmd.CommandText = strSql
           cmd.Connection = conn
           dadapter.SelectCommand = cmd

           datardr = cmd.ExecuteReader

           lastnametb.Text = datardr("LastName")
           firstnametb.Text = datardr("FirstName")
           middletb.Text = datardr("MiddleInitial")
           agetb.Text = datardr("Age")
           loctb.Text = datardr("Location")
           unametb.Text = datardr("Uname")
           pwordtb.Text = datardr("Pword")

           conn.Close()
       Catch ex As Exception

       End Try
   End Sub


MODULE:
VB
Public conn As New MySqlConnection
Public cmd As New MySqlCommand
Public dadapter As New MySqlDataAdapter
Public datardr As MySqlDataReader
Public strSql As String

Public Sub Connection()
    If conn.State = ConnectionState.Closed Then
        conn.ConnectionString = "Server = localhost; User Id = root; Password = root; Database = add_edit_delete"
        conn.Open()
        MsgBox("Connected!")
    Else
        MsgBox("Not Connected!")
    End If
End Sub


Do I have any error or whatsoever? Thanks for any help! :)
Posted
Updated 8-Aug-12 9:04am
v2

Yep - add:
If datardr.HasRows Then
	datardr.Read()

Immediately after your ExecuteReader:
 datardr = cmd.ExecuteReader

 If datardr.HasRows Then
datardr.Read()
    lastnametb.Text = datardr("LastName")
    firstnametb.Text = datardr("FirstName")
    middletb.Text = datardr("MiddleInitial")
    agetb.Text = datardr("Age")
    loctb.Text = datardr("Location")
    unametb.Text = datardr("Uname")
    pwordtb.Text = datardr("Pword")
 End If
 
Share this answer
 
Comments
Buena Velasco 8-Jul-11 7:02am    
Thanks OriginalGriff! i guess this is the part i didn't get immediately.. I've tried it but no data has appeared. The add-function's working, I really don't know what's wrong with it.
OriginalGriff 8-Jul-11 7:14am    
Does that mean it still doesn't do what you expect even after you added the Read function call?
If so, what does it do? Does the existing content of your textboxes get overwritten?
fjdiewornncalwe 8-Jul-11 7:37am    
Are you sure the add function is working? It may just not be throwing errors, but you need to ensure that the row appears in your database table before claiming that. I'm not disputing you that it is working, but worth checking first.
Buena Velasco 8-Jul-11 7:53am    
@Marcus Kramer, the add function definitely works.. I've checked it several times now, and it's totally working. :D it's the SELECT thingy I don't get, why it doesn't go the way it should be.

@Original Griff, nothing happens on the database even on the form. It just shows when I execute, no data appears even after I've added the function call. I'm trying to read the lines of codes over and over again. i hope I find the bug soon 'coz I really got no idea why it's like that.

HERE's the CODE (same code on module), I've added the HasRows and Read function:

Imports MySql.Data.MySqlClient
Public Class register
Private Sub register_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
firstnametb.Focus()

Call Connection()
Try
strSql = "SELECT * FROM add_edit_delete;"
cmd.CommandText = strSql
cmd.Connection = conn
dadapter.SelectCommand = cmd

datardr = cmd.ExecuteReader

If datardr.HasRows Then
datardr.Read()
lastnametb.Text = datardr("LastName")
firstnametb.Text = datardr("FirstName")
middletb.Text = datardr("MiddleInitial")
agetb.Text = datardr("Age")
loctb.Text = datardr("Location")
unametb.Text = datardr("Uname")
pwordtb.Text = datardr("Pword")

conn.Close()
End If
Catch ex As Exception

End Try
End Sub

Private Sub addbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click
If lastnametb.Text = "" Or firstnametb.Text = "" Or agetb.Text = "" Or loctb.Text = "" Or unametb.Text = "" Or pwordtb.Text = "" Or cpwordtb.Text = "" Then
MsgBox("Please complete the fields!", MsgBoxStyle.Exclamation, "System Message")
Else
Call Connection()

Try
strSql = "INSERT INTO register(LastName, FirstName, MiddleInitial, Age, Location, Uname, Pword) VALUES('" & lastnametb.Text & "','" & firstnametb.Text & "','" & middletb.Text & "','" & agetb.Text & "','" & loctb.Text & "','" & unametb.Text & "','" & pwordtb.Text & "')"
cmd.CommandText = strSql
cmd.Connection = conn
cmd.ExecuteNonQuery()

MsgBox("Record has been saved!", MsgBoxStyle.Information, "System Message")
conn.Close()
Me.Dispose()
Catch ex As Exception
MsgBox("Unable to save record!", MsgBoxStyle.Critical, "System Message")
conn.Close()

End Try
End If
End Sub

Private Sub cancelbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelbtn.Click
Me.Dispose()

End Sub
Private Sub clrbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrbtn.Click
lastnametb.Text = ""
firstnametb.Text = ""
middletb.Text = ""
agetb.Text = ""
loctb.Text = ""
unametb.Text = ""
pwordtb.Text = ""
cpwordtb.Text = ""
End Sub
End Class

OriginalGriff 8-Jul-11 8:31am    
Two things:
1) Add a MessageBox to show any error in your read catch block. Presently, you are ignoring any error, and I think you are getting one, because
2) Try adding "()" to your ExecuteReader statement:
datardr = cmd.ExecuteReader
becomes
datardr = cmd.ExecuteReader()

That may fix the problem.

Im having a problem in matching the textbox to the database, it only read the 1st line but not the other rows of the table. is there any code for that?? please help me... :((
Here's my code:
VB
Call Connection()

        Try
            strSql = "Select * From try"
            cmd.CommandText = strSql
            cmd.Connection = conn
            dadapter.SelectCommand = cmd

            datardr = cmd.ExecuteReader

            If TextBox1.Text = "" Then
                MsgBox("Please enter your student number!")

            ElseIf datardr.HasRows Then
                datardr.Read()

                u = datardr("studno")
                TextBox1.Text = datardr("studno")

                If TextBox1.Text = u Then
                    Label1.Text = datardr("student_name")
                    Label2.Text = datardr("contact")
                    Label3.Text = datardr("guardian_contact")
                    Label4.Text = datardr("image")
                    Label5.Text = datardr("address")
                    Label6.Text = datardr("year")
                    Label7.Text = datardr("rfcourse")
                    Label8.Text = datardr("status")
                    Label9.Text = datardr("rfidno")
                End If

                conn.Close()
            End If
        Catch ex As Exception

        End Try
    End Sub


Thanks in advance.
 
Share this answer
 
Comments
[no name] 8-Aug-12 12:28pm    
If you are having a problem then you should post your own question instead of posting a question in the form of a solution to an over year old question.
correct code for vb.net 2010

mysqlconn = New MySqlConnection
mysqlconn.ConnectionString =
"server=HP;userid=root;password=root;database=database1"
Dim READER As MySqlDataReader
Dim dadapter As New MySqlDataAdapter
Try
mysqlconn.Open()
Dim Query As String
Query = "select * from database1.table1 where id='" & TextBox2.Text & "'"

COMMAND = New MySqlCommand(Query, mysqlconn)
READER = COMMAND.ExecuteReader
While READER.Read
TextBox12.Text = READER.GetString("input1")
TextBox11.Text = READER.GetString("input2")
TextBox10.Text = READER.GetString("input3")
TextBox9.Text = READER.GetString("input4")
TextBox8.Text = READER.GetString("input5")
End While


MessageBox.Show("Data read ")
mysqlconn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
mysqlconn.Dispose()

End Try
 
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