0

This is my code. I want to update the data behind the Save button, but when I hit it, it refreshes the page and does not show updated data. It also does not update in the Database.

also is there a way that this does not refresh, something like an update panel?

Code:

    Dim conn As SqlConnection
    Dim cmd As SqlCommand

    Dim fname As String
    Dim lname As String
    Dim DOB As String
    Dim PatientNumber As String
    Dim address1 As String
    Dim address2 As String
    Dim town As String
    Dim county As String
    Dim postcode As String
    Dim ContactNumber As String
    Dim Email As String


    PatientNumber = CInt(txtPatientNo.Text)
    fname = txtFName.Text
    lname = txtLName.Text
    DOB = txtDOB.Text
    address1 = txtAddress1.Text
    address2 = txtAddress2.Text
    town = txtTown.Text
    county = txtCounty.Text
    postcode = txtPostcode.Text
    ContactNumber = txtContact.Text
    Email = txtEmail.Text


    Dim cmdstring As String = "UPDATE PatientDetails SET FirstName = @FNAME, Surname = @LNAME, DateOfBirth = @DOB, Address = @ADDRESS1, Address2 = @ADDRESS2, Town = @TOWN, County = @COUNTY, Postcode = @POSTCODE, ContactNumber = @CONTACTNUMBER, EmailAddress = @EMAIL WHERE PatientNumber = @PatientNumber"

    conn = New SqlConnection("Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\myAppointments\App_Data\Database.mdf;Integrated Security=True")
    cmd = New SqlCommand(cmdstring, conn)

    cmd.Parameters.AddWithValue("@PATIENTNUMBER", PatientNumber)
    cmd.Parameters.AddWithValue("@FNAME", fname)
    cmd.Parameters.AddWithValue("@LNAME", lname)
    cmd.Parameters.AddWithValue("@DOB", DOB)
    cmd.Parameters.AddWithValue("@ADDRESS1", address1)
    cmd.Parameters.AddWithValue("@ADDRESS2", address2)
    cmd.Parameters.AddWithValue("@TOWN", town)
    cmd.Parameters.AddWithValue("@COUNTY", county)
    cmd.Parameters.AddWithValue("@POSTCODE", postcode)
    cmd.Parameters.AddWithValue("@CONTACTNUMBER", ContactNumber)
    cmd.Parameters.AddWithValue("@EMAIL", Email)

    conn.Open()

    cmd.ExecuteNonQuery()

    conn.Close()
  • 2
    Have you stepped through the code with the debugger? Did it take the expected path through the code? did the Patient Number match what you expect? If you run the update command directly in SSMS, does it success? – mason Apr 03 '17 at 17:37
  • What @mason said. Use http://stackoverflow.com/a/265261/832052 to get the actual query text, execute it in SSMS, and see if it succeeds. – djv Apr 03 '17 at 18:00
  • Try and avoid using `AddWithValue`. Also another note, wrap your commands & connections in `Using` statements so they get properly disposed of when you are done using them. Turn `Option Strict On`, this statement `PatientNumber = CInt(txtPatientNo.Text)` wont compile; you have `PatientNumber` declared as a string, but you are then `Casting` it to an `Integer` type. – Trevor Apr 03 '17 at 19:08
  • Do you have this enclosed into a `Try Catch` statement? If so, what is the error you get, also try to convert always your datatypes to fit the database type for example use `DateTime` for `DOB` instead of `String` – 3vts Apr 03 '17 at 19:29

0 Answers0