'Form does not send input to MySQL in VB
So I wanted a user to input data into the form, after clicking "Create", it shows that it successfully added the data. However, it doesn't appear in my MySQL Database, it only triggers the auto-increment of ID, other columns have no data.
Here's the code for the button CREATE:
Imports MySql.Data.MySqlClient
Module DBConnection Public Function strconnection() As MySqlConnection Return New MySqlConnection("server=localhost;user id=root;password=;database=bankmanagement") End Function Public strcon As MySqlConnection = strconnection()
Public result As String
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public dt As New DataTable
'for create or add function
Public Sub Create(ByVal sql As String)
Try
strcon.Open()
With cmd
.Connection = strcon
.CommandText = sql
result = cmd.ExecuteNonQuery
If result = 0 Then
MessageBox.Show("Failed to save data!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show("Data has been saved in the database (Please click the Refresh button)", "SUCCESS", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
strcon.Close()
End Try
End Sub
This is my SQL Query for adding something on the form:
Private Sub SButton1_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
Try
Create("INSERT INTO customer (cust_name,cust_add,phoneNo) VALUES('" & cust_name.Text & "', '" & cust_add.Text & "', '" & phoneNo.Text & "')")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What is the possible error here? Although the program runs in succession?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
