1

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

2 Answers 2

1

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:

  • You are quoting the field name, that is syntax error.
  • You are not using parametrized queries, that is a security issue but could also be a syntax error as well. for example destination would need quote if it was a string. That is the reason you need to use parameters.

More things:

  • Connection was never opened
  • 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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I tried this but showing some other error now. Please refer the updated error above.. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.