Hello, I have my database xampp mysql and i put values from visual studio 2012.

I have the code for INSERT INTO mytable, but always i insert the value, this value appears like 0.
I dont know the reason beacause if i'm conected to the database and i send the value, the table normally show the value i write.
Someone know the problem???

Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1
    Dim Posto_1 As Integer
    Dim Posto_2 As Integer
    Dim Posto_3 As Integer
    Dim Posto_4 As Integer
    Dim Ip_Address As Integer

    Dim myConnectionString As String = "SERVER=127.0.0.1; DATABASE=base_de_dados; UID=root; PORT=3306; PASSWORD=123;"
    '*****************************************************************************************************************************************************'
    '************************************************************* ESCREVER VALORES **********************************************************************'
    '*****************************************************************************************************************************************************'
    Private Sub inserir_Click(sender As Object, e As EventArgs) Handles inserir.Click
        Dim insert_bd As String = "INSERT INTO linha(endereco_ip, t_paragem_1, t_paragem_2, t_paragem_3, t_paragem_4) VALUES ('@endereco_ip', '@posto_1_t_paragem_1', '@posto_2_t_paragem_2', '@posto_3_t_paragem_3', '@posto_4_t_paragem_4');"
        Dim connection As New MySqlConnection(myConnectionString)
        Dim command As New MySqlCommand(insert_bd, connection)

        If (posto1Text.Text = "" Or posto2Text.Text = "" Or posto3Text.Text = "" Or posto4Text.Text = "" Or ipText.Text = "") Then
            MessageBox.Show("Não foi inserido nenhum valor")
        Else
            Ip_Address = CInt(ipText.Text)
            Posto_1 = CInt(posto1Text.Text)
            Posto_2 = CInt(posto2Text.Text)
            Posto_3 = CInt(posto3Text.Text)
            Posto_4 = CInt(posto4Text.Text)

            Try
                connection.Open()
                ListBox1.Items.Add("Base de Dados --> Escrever. . .")
                command.Parameters.AddWithValue("@endereco_ip", Ip_Address)
                command.Parameters.AddWithValue("@t_paragem_1", Posto_1)
                command.Parameters.AddWithValue("@t_paragem_2", Posto_2)
                command.Parameters.AddWithValue("@t_paragem_3", Posto_3)
                command.Parameters.AddWithValue("@t_paragem_4", Posto_4)
                command.ExecuteNonQuery()
            Catch ex As Exception
                Select Case Err.Number
                    Case 1041 : MessageBox.Show("Error de conexion: Server error")
                    Case 1044 : MessageBox.Show("Error de conexion: DB Error")
                    Case 1045 : MessageBox.Show("Error de conexion: Access denied")
                    Case 1049 : MessageBox.Show("Error de conexion: DB Error - Check the DB Name")
                    Case Else
                        MessageBox.Show("Erro de conecção: " & Err.Number)
                End Select
            End Try

            connection.Close()
            connection = Nothing
            command = Nothing
        End If
    End Sub

Your codes are quite right. Only a single mistake in line no 18.

VALUES ('@endereco_ip', '@posto_1_t_paragem_1', '@posto_2_t_paragem_2', '@posto_3_t_paragem_3', '@posto_4_t_paragem_4')

No single quotation mark required before and after parameters.

It should be

VALUES (@endereco_ip, @posto_1_t_paragem_1, @posto_2_t_paragem_2, @posto_3_t_paragem_3, @posto_4_t_paragem_4)

Thanks Shark_1.
Work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.