delete a row from datagrid.. plz help..

son

Regular
Joined
Feb 21, 2004
Messages
67
hi hope all is well.. have a small problem which me cant seem to solve.. i am using vb.net

i have a webform which has datagrid and a panel with textboxes.
i am using everything in one form and either the datagrid visible or the panel of textboxes, depending on what i click, if i click the edit button t datagrid is hidden and if i click the delete button the datagrid refreshes shows a message deleted item, and the textboxes do not show..

my edit on my datagrid is working fine which basically hides the datagrid and then shows the row of the datagrid in my texboxes..

my problem is the delete button. i have checked to c if my stored procedure is working fine and it is so the problem is that my code is not executing within my delete command button which i have of my datagrid and there for no changes are being made.. my code is as follows

i am binding t datagrid as follows

Visual Basic:
dim ds as sqldatareader

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Page.IsPostBack = False Then

            DataGrid1_DataBinding()
            Panel2.Visible = False

        End If
    End Sub


Visual Basic:
Private Sub DataGrid1_DataBinding()


        Dim myconn As SqlConnection = New SqlConnection
        myconn.ConnectionString = myconfigurationsettings
        myconn.Open()

        Dim mycommand As SqlCommand = New SqlCommand
        With mycommand
            .Connection = myconn
            .CommandType = CommandType.StoredProcedure
            .CommandText = "sp_customersAll"
        End With


        ds = mycommand.ExecuteReader(CommandBehavior.CloseConnection)
        DataGrid1.DataSource = ds
        DataGrid1.DataBind()


    End Sub


this code below is for t edit and delete buttons of my datagrid
Visual Basic:
Private Sub edit(ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)

        DataGrid1.Visible = False
        Panel2.Visible = True
        If e.CommandName = "Edit" Then
            txtId.Text = e.Item.Cells(0).Text()
            txtCompanyName.Text = e.Item.Cells(1).Text()
            txtContactName.Text = e.Item.Cells(2).Text()
            txtContactTitle.Text = e.Item.Cells(3).Text()
            txtAddress.Text = e.Item.Cells(4).Text()
            txtCity.Text = e.Item.Cells(5).Text()
            txtRegion.Text = e.Item.Cells(6).Text()
            txtPostalCode.Text = e.Item.Cells(7).Text()
            txtCountry.Text = e.Item.Cells(8).Text()
            txtPhone.Text = e.Item.Cells(9).Text()
            txtFax.Text = e.Item.Cells(10).Text()
        End If

        btnsubmit.Enabled = True
 
    End Sub

    Private Sub delete(ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        DataGrid1.Visible = True
        Panel2.Visible = False
        If e.CommandName = "delete" Then
            Dim myconn As SqlConnection = New SqlConnection
            myconn.Open()


            Dim mycommand As SqlCommand = New SqlCommand
            Dim SqlParams As SqlParameter() = {New SqlParameter("@customerid", e.Item.Cells(0).Text())}
            With mycommand
                .Connection = myconn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_Deletecustomers"
                .Parameters.Remove(SqlParams)
            End With
            mycommand.ExecuteNonQuery()



          

            mycommand.Connection.Close()
            mycommand.Parameters.Clear()

            Response.Write("item deleted")



            Dim mycomm As SqlCommand = New SqlCommand
            With mycomm
                .Connection = myconn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_Allcustomers"
            End With
            Dim objDataReader As SqlDataReader = mycomm.ExecuteReader()

            DataGrid1.DataSource = objDataReader
            DataBind()
        End If
   

    End Sub


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

        Select Case e.CommandName

            Case "Edit"
                edit(e)
                Panel2.Visible = True
                DataGrid1.Visible = False

            Case "delete"
                delete(e)
                Panel2.Visible = False
                DataGrid1.Visible = True
        End Select


        btnAdd.Visible = False
    End Sub
End Class

where i have this code it is not executing

Visual Basic:
  Case "delete"
                delete(e)
                Panel2.Visible = False
                DataGrid1.Visible = True
        End Select

can someone please help would really appreciate it..
 
I know with the DataList you have to set the CommandArgument on the button to delete and then on the DataList itself set up the OnDelete event. It's probably the same with the DataGrid.
 
Probably a simple case of mismatched strings?

Select Case e.CommandName.ToUpper()
Case "EDIT"
...
Case "DELETE"
...
End Select
 
hi its still not working for me.. this is getting really frustrating.

can someone tell me how i can declare mycommand to take t parameter @customerid cause it keeps on giving me an error stating..The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects

Visual Basic:
 Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
       

        If e.CommandName = "Delete" Then
            Dim myconn As SqlConnection = New SqlConnection
            myconn.ConnectionString = myconfigurationsettings
            myconn.Open()


            

            With mycommand
                .Connection = myconn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_Deletecustomers"
              [COLOR=DarkRed]  .[B]Parameters.remove("@customerid")[/B][/COLOR] ' my error is generating
            End With



            mycommand.ExecuteNonQuery()

            mycommand.Connection.Close()
            mycommand.Parameters.Clear()

            Response.Write("item deleted")



            Dim mycomm As SqlCommand = New SqlCommand
            With mycomm
                .Connection = myconn
                .CommandType = CommandType.StoredProcedure
                .CommandText = "sp_Allcustomers"
            End With
            Dim objDataReader As SqlDataReader = mycomm.ExecuteReader()

            DataGrid1.DataSource = objDataReader
            DataBind()
        End If

    End Sub

my stored procedure is as follows

Visual Basic:
CREATE PROCEDURE sp_Deletecustomers  @customerid nvarchar

AS

DELETE  FROM customers  WHERE  customerid = @customerid
GO
.. :confused: can someone please help .. thanz
 
Last edited:
Back
Top