hi Every one i am having problem in deleting record from textboxes and gridview. i have customer table and booking table. what i want to do is when i click the delete button the customer and its related booking should be deleted. i have written a code which onlt deletes customer but it doesnt delete the related booking in the data gridview.i am also attaching my screen shot to give a better idea. and here is my code. The textboxes contains the customer information and gridview contains booking information.Cust_id is foregin key in booking table. please help.

Private Sub btnDelCus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelCus.Click
Dim i As Integer
i = MsgBox("Are you sure you want to delete the record?", vbYesNo, "Sweat and Shape Gym")
If i = vbYes Then

        Dim conn As New SqlClient.SqlConnection
        Dim cmd As New SqlClient.SqlCommand
        Dim Adaptor As New SqlClient.SqlDataAdapter
        Dim dataset As New DataSet


        Try
            Dim Con As New SqlConnection("Data Source=SAAD-PC\SQLEXPRESS;Initial Catalog=photoshoot;Integrated Security=True")


            Con.Open()
            cmd.Connection = Con
            cmd.CommandText = "Delete from Customer where Cust_id = '" & Cust_idTextBox.Text & "'"
            cmd.ExecuteNonQuery()
            MsgBox("Record Deleted!")
            Cust_idTextBox.Text = ""
            LastNameTextBox.Text = ""
            FirstNameTextBox.Text = ""

            AddressTextBox.Text = ""


            Post_CodeTextBox.Text = ""
            TelephoneTextBox.Text = ""
            Emp_idTextBox.Text = ""
            EmailTextBox.Text = ""



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        conn.Close()
        Try
            ' ORIG ------------------------------------------------------------
            conn.Open()

            cmd = New SqlCommand("select * from Customer", conn)
            Adaptor = New SqlDataAdapter(cmd)
            dataset.Clear()
            Adaptor.Fill(dataset, "NewBookingdataset1")
            NewBookingDataGridView.DataSource = dataset.Tables("customer")
            '---------------------------------------------------------
            conn.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        NewBookingDataGridView.Refresh()
    End If
End Sub

That is the problem . I can delete the values of customer table but i dont know how to delete the information from grid for the booking table.
i get an error "non execute query connection string not been intialized". please help

in this code you are just dealing with customer table , just you should have one thing always in your mind , always first delete the related record then the main records , for example we have a customer name ali , now we also have 3 records in table books , in order to delete ali from the customer we have to delete 3 records of ali from the table books ,now use same code and just change the query and in query just change the name of table , then after execute the query and deleting the records form the table books then delete record of your perticular customer.

This article has been dead for over six months. Start a new discussion instead.