Question SQL Update Query issue

Ninoo0

New member
Joined
Aug 15, 2018
Messages
2
Programming Experience
Beginner
Hey guys, so I have a bit of problem regarding my current coding within my project, this issue being that I am struggling to get this code to run when trying to update an already existing record within an Access database, any assistance given would be highly appreciated
--------------------------------------------------------------------------------------------------------------------------------------------
Private Sub btn_Add_Click(sender As Object, e As EventArgs) Handles btn_Add.Click
        Try
            Dim sqlconn As New OleDb.OleDbConnection
            Dim sqlquery As New OleDb.OleDbCommand
            Dim connString As String
            connString = ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E:\A-Level Computer Science\Project\BritishAirwaysDatabase.mdb';Persist Security Info=False;")
            sqlconn.ConnectionString = connString
            sqlquery.Connection = sqlconn
            sqlconn.Open()
            sqlquery.CommandText = "INSERT INTO Bookings(BookingID, Firstname, Surname, FlightNo, Destination, Cost)VALUES(@BookingID, @Firstname, @Surname, @FlightNo, @Destination, @Cost)"
            sqlquery.Parameters.AddWithValue("@BookingID", txt_SearchID.Text)
            sqlquery.Parameters.AddWithValue("@Firstname", txt_Firstname.Text)
            sqlquery.Parameters.AddWithValue("@Surname", txt_Surname.Text)
            sqlquery.Parameters.AddWithValue("@FlightNo", txt_FlightNo.Text)
            sqlquery.Parameters.AddWithValue("@Destination", txt_Destination.Text)
            sqlquery.Parameters.AddWithValue("@Cost", txt_Cost.Text)
            sqlquery.ExecuteNonQuery()
 
            'sqlquery.CommandText = "UPDATE Bookings" &
            ' "SET Firstname = @Firstname" &
            '  "Surname = @Surname" &
            ' "FlightNo = @FlightNo" &
            ' "Destination = @Destination" &
            '"Cost = @Cost" &
            '"Where BookingID = @BookingID"
            'sqlquery.Parameters.AddWithValue("@Firstname", txt_Firstname)
            'sqlquery.Parameters.AddWithValue("@Surname", txt_Surname)
            'sqlquery.Parameters.AddWithValue("@FlightNo", txt_FlightNo)
            'sqlquery.Parameters.AddWithValue("@Destination", txt_Destination)
            'sqlquery.Parameters.AddWithValue("@Cost", txt_Cost)
            'sqlquery.ExecuteNonQuery()
            sqlconn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
 
Last edited by a moderator:

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,518
Location
Lansing, MI; USA
Programming Experience
10+
First thing I see is you say you want to do an update query but your code shows an insert query.
It looks like you have the start of code for an update query, but it's commented out so I don't know if that's where you need help or not.

Can you please specify.
 

Ninoo0

New member
Joined
Aug 15, 2018
Messages
2
Programming Experience
Beginner
Hey thanks for taking the time to look through it, yeah the commented out part is just for the sake of running the program without any issues because as of right now that part of the code isn't fully working, that's the only reasoning behind that.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,111
Location
Sydney, Australia
Programming Experience
10+
I don't really get it. You have an 'Add' button that executes an INSERT statement. Why would you think that that would update an existing record? You have code commented out that looks like it would at least go some ways to doing what you want but you say that that's just for running the program without issues. Isn;t running the program without issues what you want? How about you don;t post code here that we all know has nothing to do with what you're trying to achieve and show us the code that you have tried to write to do what you want? Once we see what you've actually done then we can see what's wrong with it and help you fix that. Showing us code that inserts a new record is useless if you want help with updating an existing record. Show us the code that you think should/could do that.
 
Top Bottom