Page 1 of 1

Insert from text box/query and show in labels.

Posted: Fri 05 Jun 2009 21:27
by diogopinheiro
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

Posted: Mon 08 Jun 2009 14:22
by diogopinheiro
Anyone ? I supose its easy but i cant figured it out.
Please i needd help.

Thanks

Posted: Wed 10 Jun 2009 08:01
by Shalex
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.

Posted: Sat 13 Jun 2009 20:15
by diogopinheiro
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

Posted: Mon 15 Jun 2009 15:26
by Shalex

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.

Posted: Mon 22 Jun 2009 06:59
by Shalex
Aanensonex, we have deleted your post because it is out of subject area of our forum.