none
Problem with delete from database RRS feed

  • Question

  • hi

    I have the following deletion code to delete a selectedrow from the database
    The deletion process is done well
    Problem I want to see a message if a row is not selected in the database

       Try
                Dim indx As Integer = DATAG_CLIENONLIN.CurrentRow.Index
                Dim result As Integer = MessageBox.Show(" Do you want to delete the row specified by number" & vbNewLine & DATAG_CLIENONLIN(1, indx).Value.ToString(), "delete", MessageBoxButtons.YesNo)
    
                If (DATAG_CLIENONLIN.SelectedRows.Count > 0) Then
                    If result = DialogResult.Yes Then
    
                        Dim com As New OleDbCommand("UPDATE CUSTOMERDETAILS_TB SET CUSTOMERDETALIS_SHOW=@CUSTOMERDETALIS_SHOW  WHERE CUSTOMERDETALIS_ID = @CUSTOMERDETALIS_ID", con) ',CUSTOMERDETALIS_DATE=@CUSTOMERDETALIS_DATE
                        Dim checkUPDAT As Boolean = False
                        con.Open()
                        com.Parameters.AddWithValue("@CUSTOMERDETALIS_SHOW", checkUPDAT)
                        com.Parameters.AddWithValue("@CUSTOMERDETALIS_ID", DATAG_CLIENONLIN(1, indx).Value.ToString())
                        If com.ExecuteNonQuery() = 1 Then
                            conn()
                            If Not indx = 0 Then
                                DATAG_CLIENONLIN.Rows(indx - 1).Selected = True
                                DATAG_CLIENONLIN.FirstDisplayedScrollingRowIndex = indx - 1
    
                            End If
                            MsgBox("delete succes")
                        Else
                            MsgBox("delete falier")
                        End If
                        con.Close()
                    ElseIf result = DialogResult.No Then
                        Exit Sub
                    End If
                Else
                    MessageBox.Show("select client", "error") : Exit Sub
                End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                con.Close()
            End Try

    Show the following message if you do not select any row

     MessageBox.Show("select client", "error") : Exit Sub

    • Edited by ahmeddc Monday, October 22, 2018 7:59 PM
    Monday, October 22, 2018 7:59 PM

Answers

  • Perhaps it would help to see a fully working example

    https://1drv.ms/u/s!AtGAgKKpqdWjjE1e3Z27JeJzuWB9


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Tuesday, October 23, 2018 3:52 AM
    Monday, October 22, 2018 11:13 PM
    Moderator
  • Hi ahmedcc,

    I tried your code and it seems that there is no problem.

    ful code

    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    
    
    Public Class Form1
        Dim con As New SqlConnection
        Dim dt As DataTable
        Dim sda As SqlDataAdapter
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Dim indx As Integer = DATAG_CLIENONLIN.CurrentRow.Index
                Dim result As Integer = MessageBox.Show(" Do you want to delete the row specified by number" & vbNewLine & DATAG_CLIENONLIN(1, indx).Value.ToString(), "delete", MessageBoxButtons.YesNo)
    
                If (DATAG_CLIENONLIN.SelectedRows.Count > 0) Then
                    If result = DialogResult.Yes Then
                        con = New SqlConnection("Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf")
                        Dim com As New SqlCommand("UPDATE Student set Age = @CUSTOMERDETALIS_ID Where Id=1", con) ',CUSTOMERDETALIS_DATE=@CUSTOMERDETALIS_DATE
                        Dim checkUPDAT As Boolean = False
                        con.Open()
                        com.Parameters.AddWithValue("@CUSTOMERDETALIS_ID", DATAG_CLIENONLIN(2, indx).Value.ToString())
                        If com.ExecuteNonQuery() = 1 Then
    
                            If Not indx = 0 Then
                                DATAG_CLIENONLIN.Rows(indx - 1).Selected = True
                                DATAG_CLIENONLIN.FirstDisplayedScrollingRowIndex = indx - 1
    
                            End If
                            MsgBox("delete succes")
                        Else
                            MsgBox("delete falier")
                        End If
                        con.Close()
                    ElseIf result = DialogResult.No Then
                        Exit Sub
                    End If
                Else
                    MessageBox.Show("select client", "error") : Exit Sub
                End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                con.Close()
            End Try
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using conn = New SqlConnection("Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf")
                conn.Open()
                sda = New SqlDataAdapter("Select * From Student", conn)
                dt = New DataTable()
                sda.Fill(dt)
                DATAG_CLIENONLIN.DataSource = dt
            End Using
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex Li-MSFT Tuesday, October 23, 2018 3:18 AM
    • Marked as answer by ahmeddc Tuesday, October 23, 2018 3:52 AM
    Tuesday, October 23, 2018 3:17 AM

All replies

  • You should first check if a row is selected then ask if you want to remove that record rather than asking then checking if a row is selected.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 22, 2018 8:15 PM
    Moderator
  • hi

    I used this condition after  try
    But are skipping I do not know why

      If (DATAG_CLIENONLIN.CurrentRow.Index < 0) Then
                MessageBox.Show("select row", "error") : Exit Sub
            End If

    Monday, October 22, 2018 8:36 PM
  • Well it's time to set a breakpoint and step thru your code while examining/inspecting values. And the following will not work

    If (DATAG_CLIENONLIN.CurrentRow.Index < 0) Then
       MessageBox.Show("select row", "error") : Exit Sub
    End If
    But what you first had if done as I suggested should work.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 22, 2018 8:57 PM
    Moderator
  • If the code works for me
    Why ask the question

    Error when not selected row

    Problem I want to see a message if a row is not selected in the database


    • Edited by ahmeddc Monday, October 22, 2018 10:17 PM
    Monday, October 22, 2018 10:10 PM
  • Perhaps it would help to see a fully working example

    https://1drv.ms/u/s!AtGAgKKpqdWjjE1e3Z27JeJzuWB9


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Tuesday, October 23, 2018 3:52 AM
    Monday, October 22, 2018 11:13 PM
    Moderator
  •   Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
        If DataGridView.SelectedRows.Count > 0 Then
          If MessageBox.Show("Do you want to delete this row", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
            For Each Row As DataGridViewRow In DGVKontak.SelectedRows
              'Do Delete
            Next
          End If
        Else
          MessageBox.Show("Please select row you want to delete", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
      End Sub


    • Edited by Xan To Tuesday, October 23, 2018 12:51 AM
    Tuesday, October 23, 2018 12:47 AM
  • Hi ahmedcc,

    I tried your code and it seems that there is no problem.

    ful code

    Imports System.Data.OleDb
    Imports System.Data.SqlClient
    
    
    Public Class Form1
        Dim con As New SqlConnection
        Dim dt As DataTable
        Dim sda As SqlDataAdapter
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Try
                Dim indx As Integer = DATAG_CLIENONLIN.CurrentRow.Index
                Dim result As Integer = MessageBox.Show(" Do you want to delete the row specified by number" & vbNewLine & DATAG_CLIENONLIN(1, indx).Value.ToString(), "delete", MessageBoxButtons.YesNo)
    
                If (DATAG_CLIENONLIN.SelectedRows.Count > 0) Then
                    If result = DialogResult.Yes Then
                        con = New SqlConnection("Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf")
                        Dim com As New SqlCommand("UPDATE Student set Age = @CUSTOMERDETALIS_ID Where Id=1", con) ',CUSTOMERDETALIS_DATE=@CUSTOMERDETALIS_DATE
                        Dim checkUPDAT As Boolean = False
                        con.Open()
                        com.Parameters.AddWithValue("@CUSTOMERDETALIS_ID", DATAG_CLIENONLIN(2, indx).Value.ToString())
                        If com.ExecuteNonQuery() = 1 Then
    
                            If Not indx = 0 Then
                                DATAG_CLIENONLIN.Rows(indx - 1).Selected = True
                                DATAG_CLIENONLIN.FirstDisplayedScrollingRowIndex = indx - 1
    
                            End If
                            MsgBox("delete succes")
                        Else
                            MsgBox("delete falier")
                        End If
                        con.Close()
                    ElseIf result = DialogResult.No Then
                        Exit Sub
                    End If
                Else
                    MessageBox.Show("select client", "error") : Exit Sub
                End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                con.Close()
            End Try
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using conn = New SqlConnection("Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf")
                conn.Open()
                sda = New SqlDataAdapter("Select * From Student", conn)
                dt = New DataTable()
                sda.Fill(dt)
                DATAG_CLIENONLIN.DataSource = dt
            End Using
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Alex Li-MSFT Tuesday, October 23, 2018 3:18 AM
    • Marked as answer by ahmeddc Tuesday, October 23, 2018 3:52 AM
    Tuesday, October 23, 2018 3:17 AM