I am trying to add record of entered inputs to data table but it's not working. I have tried this.
vb.net
Imports System.Data
Imports System.Configuration
Imports MySql.Data.MySqlClient
Partial Class index
Inherits System.Web.UI.Page
Dim con As New MySqlConnection("Data Source=204.11.58.166;port=3306;Initial Catalog=quistaBooking;User Id=my_username;password=my_password ;")
Protected Sub confirmBook_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles confirmBook.Click
Dim emailID, contactNo, source, destination, duration, distance, fare, datetime, vehicle As String
emailID = email.Text
contactNo = contact.Text
source = txtSource.Text
destination = txtDestination.Text
duration = dvDuration.Text
distance = dvDistance.Text
datetime = datetimepicker.Text
vehicle = selectVehicle.SelectedItem.ToString
Try
Dim str1 As String = "INSERT INTO logistics ('email', 'contact', 'source', 'destination', 'duration', 'distance', 'dateTime', 'vehicleType') values ('" + emailID + "', '" + contactNo + "', '" + source + "', '" + destination + "', '" + duration + "', '" + datetime + "', '" + vehicle + "')"
Dim data As MySqlDataReader
Dim adapter As New MySqlDataAdapter
Dim command As New MySqlCommand
command.CommandText = str1
command.Connection = con
adapter.SelectCommand = command
data = command.ExecuteReader
con.Close()
Catch ex As Exception
Response.Write(ex)
End Try
End Sub
End Class
Error
System.InvalidOperationException: Connection must be valid and open. at MySql.Data.MySqlClient.MySqlConnection.Throw(Exception ex) at MySql.Data.MySqlClient.MySqlCommand.CheckState() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at index.confirmBook_Click(Object sender, EventArgs e) in E:\\MY WEB\\Quista\\Website\\index.aspx.vb:line 58
Can any one help me where I am going wrong?
UPDATED ERROR
MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''email', 'contact', 'source', 'destination', 'duration', 'distance', 'dateTime',' at line 1 at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at index.confirmBook_Click(Object sender, EventArgs e) in E:\\MY WEB\\Quista\\Website\\index.aspx.vb:line 59
Looking at the SQL query:
"INSERT INTO logistics
('email', 'contact', 'source', 'destination', 'duration', 'distance', 'dateTime', 'vehicleType')
values ('" + emailID + "', '" + contactNo + "', '" + source + "', '" + destination + "', '" + duration + "', '" + datetime + "', '" + vehicle + "')"
I could already see issues:
More things:
data = command.ExecuteReader
makes no sense because it is a INSERT
there is nothing to read you should be using command.ExecuteNonQuery()
The more I look at your code, the more issues I find, I rewrote it for you:
Try
con.Open()
command.Connection = con
command.CommandText = "INSERT INTO logistics (`email`, `contact`, `source`, `destination`, `duration`, `distance`, `dateTime`, `vehicleType`) values (@emailID, @contactNo, @source, @destination, @duration, @datetime, @vehicle)"
command.Prepare()
command.Parameters.AddWithValue("@emailID", emailID)
command.Parameters.AddWithValue("@contactNo", contactNo)
command.Parameters.AddWithValue("@source", source)
command.Parameters.AddWithValue("@destination", destination)
command.Parameters.AddWithValue("@duration", duration)
command.Parameters.AddWithValue("@datetime", datetime)
command.Parameters.AddWithValue("@vehicle", vehicle)
command.Parameters.AddWithValue("@text", "One")
command.ExecuteNonQuery()
Catch ex As MySqlException
MessageBox.Show("Error " & ex.Number & " has occurred: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Connection must be valid and open
Add con.Open()
before data = command.ExecuteReader
You have an error in your SQL syntax
You have 8 fields and try insert only 7 values. Missing value distance
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.