Results 1 to 4 of 4

Thread: multiple delete

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    47

    multiple delete

    Hi,
    i have try to use multiple checkbox to delete users by using datagrid.

    Below is my codes:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    Dim conn As OleDbConnection
    Dim objCmd = New OleDbCommand

    conn = connectToDB()
    objCmd.commandtext = "spGetMembers"
    objCmd.CommandType = CommandType.StoredProcedure
    objCmd.connection = conn

    Dim myDataReader As OleDbDataReader
    myDataReader = objCmd.ExecuteReader

    dgDelMember.DataSource = myDataReader
    dgDelMember.DataBind()
    conn.close()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

    Dim Item
    For Each Item In Request.QueryString("chkMemDelete")
    'SQL statement here

    Dim conn As OleDbConnection
    Dim objCmd = New OleDbCommand

    conn = connectToDB()
    objCmd.commandtext = "spDelMembers"
    objCmd.CommandType = CommandType.StoredProcedure
    objCmd.connection = conn

    Dim myDataReader As OleDbDataReader
    myDataReader = objCmd.ExecuteReader

    dgDelMember.DataSource = myDataReader
    dgDelMember.DataBind()
    conn.Close()

    Next

    End Sub

    But they can't find the "chkMemDelete" as the id for checkbox is in the datagrid.

    Error:
    Object reference not set to an instance of an object.


  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    First off, your going about this the wrong way. You are still thinkin in terms of Classic ASP. Simply iterate through the datagrid's Items's collection and do a findcontrol on the checkbox name and from there, determine which items to delete.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    47
    Thank You Lethal.

    Now i'm trying the other way which you told me... it does not have error but does not delete the users. Can point me my where is my mistake? Thankz...

    here is my coding:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here

    Dim conn As OleDbConnection
    Dim objCmd = New OleDbCommand

    conn = connectToDB()
    objCmd.commandtext = "spGetMembers"
    objCmd.CommandType = CommandType.StoredProcedure
    objCmd.connection = conn

    Dim myDataReader As OleDbDataReader
    myDataReader = objCmd.ExecuteReader

    dgDelMember.DataSource = myDataReader
    dgDelMember.DataBind()
    conn.close()
    End Sub

    Private Sub dgDelMember_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgDelMember.ItemCommand
    If CType(e.Item.FindControl("chkMemDelete"), CheckBox).Checked And e.CommandName = "DeleteButton" Then
    Dim conn As OleDbConnection
    Dim objCmd = New OleDbCommand

    conn = connectToDB()
    objCmd.commandtext = "spDelMembers"
    objCmd.CommandType = CommandType.StoredProcedure
    objCmd.connection = conn

    Dim myDataReader As OleDbDataReader
    myDataReader = objCmd.ExecuteReader

    dgDelMember.DataSource = myDataReader
    dgDelMember.DataBind()
    conn.Close()
    End If
    End Sub
    End Class

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2004
    Posts
    47
    Do i have to use the dataset? I do not know how to tell the system that these are the ID that need to be deleted.

    Private Sub dgDelMember_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgDelMember.ItemCommand

    Dim dgItem As DataGridItem
    Dim result As String

    For Each dgItem In dgDelMember.Items

    If CType(dgItem.FindControl("chkMemDelete"), CheckBox).Checked And e.CommandName = "DeleteButton" Then

    Dim conn As OleDbConnection
    Dim objCmd As New OleDbCommand

    Session("Chk_CustID") = CType(dgItem.FindControl("chkMemDelete"), CheckBox).Text

    conn = connectToDB()
    objCmd.CommandText = "spDelMembers"
    objCmd.CommandType = CommandType.StoredProcedure
    objCmd.Connection = conn

    ''codes for assigning parameters
    Dim objparam As OleDbParameter

    objparam = objCmd.Parameters.Add("RETURN_VALUE", SqlDbType.Int)
    objparam.Direction = ParameterDirection.ReturnValue

    objCmd.Parameters.Add("@CustID", Session("Chk_CustID"))

    ' Execute SQL and read data from database
    objCmd.ExecuteNonQuery()

    ' Bind the records to the data list control
    dgDelMember.DataBind()

    ' Close the connection object
    conn.Close()
    End If
    Next
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width