I am trying to get the "recid" value (the key) for the record I want to delete. I've been trying different ways for a couple hours and I can't get the right one. Can you help?
Here are samples of the different ways I tired to get the value...nothing is working. Thanks
strRecid =(GridView1.Rows[(int)e.RowIndex].Cells[1].Text)
strRecid = GridView1.SelectedRow.Cells(0).FindControl("lblRecId").ToString
strRecid = GridView1.SelectedRow.FindControl("lblRecId")
Dim txtRecid
As TextBox = GridView1.SelectedRow.Cells(0).FindControl("lblRecId")
Dim strRecId As
String = GridView1.DataKeys(Convert.ToInt32(e.RowIndex).Value.ToString)
This is taking place in the: GridView1_RowDeleting sub routine.
here it is. All the unsuccessful assignments commented out.
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim MyConn1 As New OleDbConnection(Globals.MyPath)
Dim cmdDelete As OleDbCommand
Dim strRecid
' Dim rowno = e.RowIndex
' dim txtBox as textbox = GridView1.Rows[rowno].Cells[0].Controls[0]
' Dim s As String = txtBox.Text.ToString()
' dim lblRecid as Label = this.GridView1.Rows[e.RowIndex].FindControl("lblRecId") as Label
' strrecid =(GridView1.Rows[(int)e.RowIndex].Cells[1].Text)
' strRecid = GridView1.SelectedRow.Cells(0).FindControl("lblRecId").ToString
' strRecid = GridView1.SelectedRow.FindControl("lblRecId")
' Dim txtRecid As TextBox = GridView1.SelectedRow.Cells(0).FindControl("lblRecId")
' Dim strRecId As String = GridView1.DataKeys(Convert.ToInt32(e.RowIndex).Value.ToString)
' strRecid = txtRecid.Text
cmdDelete = New OleDbCommand("Delete from tblMake " & _
"Where Recid = " & strRecId & ";", MyConn1)
MyConn1.Open()
cmdDelete.ExecuteNonQuery()
MyConn1.Close()
BindAutoMake()
End Sub
If your <asp:Label ID=lblRecId runat=server text='<%# Databinder.Eval(Container,"DataItem.RecId") %>'></asp:Label> contains
RecId then try this way
Protected Sub GridView1_RowDeleting(ByVal sender
As Object,
ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
Handles GridView1.RowDeleting
Dim MyConn1
As New OleDbConnection(Globals.MyPath) Dim cmdDelete
As OleDbCommand
cmdDelete = New OleDbCommand("Delete from tblMake " & _ "Where Recid = " & CType(GridView1.Rows(e.RowIndex).FindControl("lblRecId"), Label).Text &
";", MyConn1)
Yes, recently i began to think I was handling in the wrong event. It should probably go in the rowcommand event.
I also redid the gridview a little bit to make the Recid field a bound field instead of a label. I still can not get the recid key field of the record...So help is still needed. But I wanted to show the undated html and code (see below)
Key value still unassigned. I will try previous answer from post to see if that works. I've been spinning on this for a day now, your help is much appreciated. On a side note...this is the proper way to handle a delete of a database row isn't it?
Protected Sub GridView1_RowCommand(ByVal sender
As Object, ByVal e
As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim MyConn1 As New OleDbConnection(Globals.MyPath) Dim cmdDelete As OleDbCommand
If e.CommandName = "Delete"Then
Dim strRecId As String
strRecId = ?????? how to key record key from first column
The delete button needs a command argument, CommandArgument = ' <%# Eval("RecID") %>'
strRecId = ?????? how to key record key from first column
should be
strRecId = e.CommandArgument
You may want to change the "Delete" command name to something other than a default such as "DeleteRecord" or you may be forced to include events you may not need.
StrangerMike
On a side note...this is the proper way to handle a delete of a database row isn't it?
You should at least consider using stored procedures, and possibly separate entities for executing queries and non-queries against the database.
If you give the Command name as Delete.. It goes for the row command as it is a custom button and Looks for Row_deleting Event as give the command name as Delete which is like a inbuilt command name for gridview Delete..
Better Post your code before asking a question. So that person helping u will understand u r problem Clearly
Yahoo IM : dotnetruler
I am trying to keep Frequently Asked Questions in my blog
If you give the Command name as Delete.. It goes for the row command as it is a custom button and Looks for Row_deleting Event as give the command name as Delete which is like a inbuilt command name for gridview Delete..
The reason I mentioned possibly skipping the default is that when deleting by a unique id you already have all the info you need. Thus the GridView delete events are pointless. Granted this is not always the case, if you want to utilize the delete events
then use "delete" as the command name.
StrangerMike
Contributor
2705 Points
1724 Posts
gridview delete row command
Jan 27, 2009 08:59 PM|LINK
Hi,
I am trying to get the "recid" value (the key) for the record I want to delete. I've been trying different ways for a couple hours and I can't get the right one. Can you help?
Here is the control that holds the key:
<asp:Label ID=lblRecId runat=server text='<%# Databinder.Eval(Container,"DataItem.RecId") %>'></asp:Label>
Here are samples of the different ways I tired to get the value...nothing is working. Thanks
strRecid =(GridView1.Rows[(int)e.RowIndex].Cells[1].Text) strRecid = GridView1.SelectedRow.Cells(0).FindControl("lblRecId").ToString strRecid = GridView1.SelectedRow.FindControl("lblRecId") Dim txtRecid As TextBox = GridView1.SelectedRow.Cells(0).FindControl("lblRecId") Dim strRecId As String = GridView1.DataKeys(Convert.ToInt32(e.RowIndex).Value.ToString)This is taking place in the: GridView1_RowDeleting sub routine.
marx.net
Participant
1068 Points
224 Posts
Re: gridview delete row command
Jan 27, 2009 09:10 PM|LINK
Can you post your grid view code?
Thanks.
StrangerMike
Contributor
2705 Points
1724 Posts
Re: gridview delete row command
Jan 28, 2009 02:04 PM|LINK
here it is. All the unsuccessful assignments commented out.
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting Dim MyConn1 As New OleDbConnection(Globals.MyPath) Dim cmdDelete As OleDbCommand Dim strRecid ' Dim rowno = e.RowIndex ' dim txtBox as textbox = GridView1.Rows[rowno].Cells[0].Controls[0] ' Dim s As String = txtBox.Text.ToString() ' dim lblRecid as Label = this.GridView1.Rows[e.RowIndex].FindControl("lblRecId") as Label ' strrecid =(GridView1.Rows[(int)e.RowIndex].Cells[1].Text) ' strRecid = GridView1.SelectedRow.Cells(0).FindControl("lblRecId").ToString ' strRecid = GridView1.SelectedRow.FindControl("lblRecId") ' Dim txtRecid As TextBox = GridView1.SelectedRow.Cells(0).FindControl("lblRecId") ' Dim strRecId As String = GridView1.DataKeys(Convert.ToInt32(e.RowIndex).Value.ToString) ' strRecid = txtRecid.Text cmdDelete = New OleDbCommand("Delete from tblMake " & _ "Where Recid = " & strRecId & ";", MyConn1) MyConn1.Open() cmdDelete.ExecuteNonQuery() MyConn1.Close() BindAutoMake() End Submarx.net
Participant
1068 Points
224 Posts
Re: gridview delete row command
Jan 28, 2009 03:05 PM|LINK
If your <asp:Label ID=lblRecId runat=server text='<%# Databinder.Eval(Container,"DataItem.RecId") %>'></asp:Label> contains RecId then try this way
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim MyConn1 As New OleDbConnection(Globals.MyPath)
Dim cmdDelete As OleDbCommand
cmdDelete = New OleDbCommand("Delete from tblMake " & _
"Where Recid = " & CType(GridView1.Rows(e.RowIndex).FindControl("lblRecId"), Label).Text & ";", MyConn1)
MyConn1.Open()
cmdDelete.ExecuteNonQuery()
MyConn1.Close()
BindAutoMake()
End Sub
Thanks.
JZoerman
Participant
847 Points
210 Posts
Re: gridview delete row command
Jan 28, 2009 03:19 PM|LINK
Another option, set the CommandArgument of the delete button to RecID with a CommandName like "DeleteRecord" and simply handle in RowCommand event.
Sarasota Web Design
StrangerMike
Contributor
2705 Points
1724 Posts
Re: gridview delete row command
Jan 28, 2009 04:09 PM|LINK
Yes, recently i began to think I was handling in the wrong event. It should probably go in the rowcommand event.
I also redid the gridview a little bit to make the Recid field a bound field instead of a label. I still can not get the recid key field of the record...So help is still needed. But I wanted to show the undated html and code (see below)
Key value still unassigned. I will try previous answer from post to see if that works. I've been spinning on this for a day now, your help is much appreciated. On a side note...this is the proper way to handle a delete of a database row isn't it?
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim MyConn1 As New OleDbConnection(Globals.MyPath)
Dim cmdDelete As OleDbCommand
If e.CommandName = "Delete" Then
Dim strRecId As String
strRecId = ?????? how to key record key from first column
cmdDelete = New OleDbCommand("Delete from tblMake " & _
"Where Recid = " & strRecId & ";", MyConn1)
MyConn1.Open()
cmdDelete.ExecuteNonQuery()
MyConn1.Close()
BindAutoMake()
End If
End Sub
Gridview Define:
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" CellPadding="1" ShowFooter="True" ForeColor="#333333" GridLines="None" Height="408px" Width="32px" AutoGenerateColumns="False"> <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#E3EAEB" /> <Columns> <asp:BoundField DataField="RecId" ReadOnly=True HeaderText="RecId" /> <asp:TemplateField HeaderText="Auto Make"> <ItemTemplate> <asp:Label ID=lblMake runat=server text='<%# Databinder.Eval(Container,"DataItem.AutoMake") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="newMake" runat=server /> </FooterTemplate> <EditItemTemplate> <asp:TextBox id="txtMake" runat=server Text=' <%# Databinder.Eval(Container, "DataItem.AutoMake") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="DelButton" runat=server Text="Delete" CommandName="Delete" CausesValidation=False></asp:LinkButton> </ItemTemplate> <FooterTemplate> <asp:Button CommandName="Insert" Text="Insert" OnClick="DoInsert" ID="InsButton" runat=server /> </FooterTemplate> </asp:TemplateField> </Columns> <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#FFFFC0" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView>marx.net
Participant
1068 Points
224 Posts
Re: gridview delete row command
Jan 28, 2009 04:19 PM|LINK
Change this line
<asp:LinkButton ID="DelButton" runat=server Text="Delete" CommandName="Delete" CausesValidation=False></asp:LinkButton>
To
<asp:LinkButton ID="DelButton" runat=server Text="Delete" CommandName="Delete" CausesValidation=False CommandArgument='<%# Databinder.Eval(Container,"DataItem.RecId") %>'></asp:LinkButton>
and in vb file Change
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim MyConn1 As New OleDbConnection(Globals.MyPath)
Dim cmdDelete As OleDbCommand
If e.CommandName = "Delete" Then
Dim strRecId As String
strRecId = ?????? how to key record key from first column
cmdDelete = New OleDbCommand("Delete from tblMake " & _
"Where Recid = " & strRecId & ";", MyConn1)
MyConn1.Open()
cmdDelete.ExecuteNonQuery()
MyConn1.Close()
BindAutoMake()
End If
End Sub
To
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim MyConn1 As New OleDbConnection(Globals.MyPath)
Dim cmdDelete As OleDbCommand
If e.CommandName = "Delete" Then
cmdDelete = New OleDbCommand("Delete from tblMake " & _
"Where Recid = " & e.CommandArgument.ToString() & ";", MyConn1)
MyConn1.Open()
cmdDelete.ExecuteNonQuery()
MyConn1.Close()
BindAutoMake()
End If
End Sub
N.B : Make sure you still have Row_deleting event handler method but Empty without any code. Otherwise it will gives you error.
Thanks.
JZoerman
Participant
847 Points
210 Posts
Re: gridview delete row command
Jan 28, 2009 04:24 PM|LINK
Couple problems,
The delete button needs a command argument, CommandArgument = ' <%# Eval("RecID") %>'
strRecId = ?????? how to key record key from first column
should be
strRecId = e.CommandArgument
You may want to change the "Delete" command name to something other than a default such as "DeleteRecord" or you may be forced to include events you may not need.
You should at least consider using stored procedures, and possibly separate entities for executing queries and non-queries against the database.
Sarasota Web Design
dotnetruler
Contributor
4230 Points
753 Posts
Re: gridview delete row command
Jan 28, 2009 04:32 PM|LINK
aspx code.. ID is key of my table
<
asp:linkbutton id="btnDelete" runat="server" commandargument='<%# Eval("ID") %>' commandname="Delete" onclientclick="return Confirm(this);">Delete</asp:linkbutton>CS Code
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "Delete")
int ID = Convert.ToInt32(e.CommandArgument);
}protected
void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e){
BindData();
}
If you give the Command name as Delete.. It goes for the row command as it is a custom button and Looks for Row_deleting Event as give the command name as Delete which is like a inbuilt command name for gridview Delete..
Yahoo IM : dotnetruler
I am trying to keep Frequently Asked Questions in my blog
JZoerman
Participant
847 Points
210 Posts
Re: gridview delete row command
Jan 28, 2009 05:02 PM|LINK
The reason I mentioned possibly skipping the default is that when deleting by a unique id you already have all the info you need. Thus the GridView delete events are pointless. Granted this is not always the case, if you want to utilize the delete events then use "delete" as the command name.
Sarasota Web Design