Insert from text box/query and show in labels.

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
diogopinheiro
Posts: 3
Joined: Fri 05 Jun 2009 16:58

Insert from text box/query and show in labels.

Post by diogopinheiro » Fri 05 Jun 2009 21:27

Hello,

I want to know how to insert values into a mysql data base from a text box, and how to read from the data base and show it in labels.

I'm developiing for windows mobile using vb2005, mysql and devart dot net connect for mobile.

The code i have to add data is the following:

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
If txt_ward.Text = "" Then
MsgBox("Please insert ward name.", MsgBoxStyle.Information, "Mobile Audit'09")
End If
Dim ward As String
ward = txt_ward.Text
MySqlConnection1.Open()
MySqlCommand1.ExecuteNonQuery()


Try
Catch exception As Devart.Data.MySql.MySqlException
MsgBox(exception.Message)
End Try

MySqlConnection1.close()

End Sub

The command text i have in the MySqlCommand1:

INSERT INTO audit (Auditor_id,Audit_date,Audit_Ward) VALUES (1,2009-06-04, txt_ward.text)

Any help ?
Thanks

diogopinheiro
Posts: 3
Joined: Fri 05 Jun 2009 16:58

Post by diogopinheiro » Mon 08 Jun 2009 14:22

Anyone ? I supose its easy but i cant figured it out.
Please i needd help.

Thanks

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Wed 10 Jun 2009 08:01

Please use the parameters collection of the MySqlCommand object to assign the value from txt_ward.text to your query. For example:

Code: Select all

        Dim MySqlCommand1 As MySqlCommand = MySqlConnection1.CreateCommand()
        MySqlCommand1.CommandText = "INSERT INTO audit (Auditor_id,Audit_date,Audit_Ward) VALUES (1,2009-06-04, :param1)"
        Dim param1 As New MySqlParameter()
        param1.ParameterName = "param1"
        param1.Direction = Data.ParameterDirection.Input
        param1.MySqlType = MySqlType.Text
        param1.Value = txt_ward.text
        MySqlCommand1.Parameters.Add(param1)
        MySqlCommand1.ExecuteNonQuery()
Please notify us about the results.

diogopinheiro
Posts: 3
Joined: Fri 05 Jun 2009 16:58

Post by diogopinheiro » Sat 13 Jun 2009 20:15

Hey, thanks. It worked fine.
Can you help me in another doubt, i would be apreciated.

How can I querry the data base and show the results in labels?
You can use the same table and fields for example.

Thanks

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Mon 15 Jun 2009 15:26

Code: Select all

        Dim MySqlCommand1 As MySqlCommand = MySqlConnection1.CreateCommand()
        MySqlCommand1.CommandText = "SELECT * FROM audit WHERE Auditor_id=:param1"
        Dim param1 As New MySqlParameter()
        param1.ParameterName = "param1"
        param1.Direction = Data.ParameterDirection.Input
        param1.MySqlType = MySqlType.Int
        param1.Value = 10 //any value
        MySqlCommand1.Parameters.Add(param1)
        Dim MySqlDataReader As reader = MySqlCommand1.ExecuteReader()
        While (reader.Read()) 
          lblId.Text = reader[0].ToString()
          lblDate.Text = reader[1].ToString()
          lblWard.Text = reader[2].ToString()
        End While
Please refer to our online documentation at http://www.devart.com/dotconnect/mysql/docs/?Using.html, the Using Parameters section.

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Post by Shalex » Mon 22 Jun 2009 06:59

Aanensonex, we have deleted your post because it is out of subject area of our forum.

Post Reply