.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

edit,update and delete in gridview

Posted By: Catherine     Posted Date: April 05, 2011    Points:5   Category :VB.Net
When i update the row it executes twice. but Updated in database without duplication and when I delete row its also deleted but execute twice with error as Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index

Here is the code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
connectionString = ""
Dim connection As OleDbConnection = New OleDbConnection(connectionString)

If Not Page.IsPostBack Then
bind()
End If

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim id As Integer
id = Int32.Parse(GridView1.DataKeys(e.RowIndex).Value.ToString())
Dim txtid As New TextBox
Dim txtname As New TextBox
Dim txtmark As New TextBox
txtid = row.FindControl("txtid")
txtname = row.FindControl("txtname")
txtmark = row.FindControl("txtmark")
connection = New OleDbConnection(connectionString)
connection.Open()
SQLstr = "update stu_tbl set name='" & txtname.Text & "',mark=" & txtmark.Text & " where id=" & id & ""
Dim cmd As New OleDbCommand(SQLstr, connection)
cmd.ExecuteNonQuery()
bind()
End sub
Public Sub bind()
connection = New OleDbConnection(connectionString)
connection.Open()
SQLstr = "SELECT id,name,mark from stu_tbl order by id"
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SQLstr, connection)
Dim ds As New DataSet()
da.Fill(ds, "stu_tbl")
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
connection.Close()
End Sub
Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
connectionString = ""
connection = New OleDbConnection(connectionString)
connection.Open()
Dim id As Integer
id = Int32.Parse(GridView1.DataKeys(e.RowIndex).Value.ToString())
(error coming in the above line)
Dim cmd As New OleDbCommand("delete from stu_tbl where id=" & id & "", connection)
result = MsgBox("Do you want to Delete", MsgBoxStyle.OkCancel, "Confirmation")
If (result = MsgBoxResult.Ok) Then
cmd.ExecuteNonQuery()

MsgBox("Record Deleted")
bind()
End If
end sub

Here is the source code:Datakeynames ="id"
<asp:TemplateField HeaderText ="Id">
<ItemTemplate>
<%#Eval("id")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtid" runat="server" Text='<%#Eval("id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>


<asp:TemplateField HeaderText ="Name">
<ItemTemplate>
<%#Eval("name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText ="Mark">
<ItemTemplate>
<%#Eval("mark")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtmark" runat="server" Text='<%#Eval("Mark") %>'></asp:TextBox>
</EditItemTemplate>

</asp:TemplateField>





Responses
Author: Catherine             
Posted Date: April 05, 2011     Points: 20   

reply please

Author: Mahesh             
Posted Date: April 05, 2011     Points: 20   

Hi,
I think you should not use "MsgBox" for messages, due to this msgbox page gets postback and the event is executed twice so remove "Msgbox", if you want to display messages then use javascript alert box.

Thanks
Mahesh

Author: Catherine             
Posted Date: April 05, 2011     Points: 20   

thank you so much. I remove messagebox its work in row_updating and not working in row_deleting. and also tell in detail where should i use javascript

Author: Catherine             
Posted Date: April 05, 2011     Points: 20   

When i debug it executes twice

Author: Mahesh             
Posted Date: April 05, 2011     Points: 20   

Hi,
Use link button in gridview as,

<asp:GridView DataKeyNames="CategoryID" ID="GridView1"
runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand"
OnRowDataBound="GridView1_RowDataBound">

<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("id") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim l As LinkButton = DirectCast(e.Row.FindControl("LinkButton1"), LinkButton)
l.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure you want to delete this record " & DataBinder.Eval(e.Row.DataItem, "id") & "')")
End If
End Sub


Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Delete" Then
' get the categoryID of the clicked row
Dim categoryID As Integer = Convert.ToInt32(e.CommandArgument)
' Delete the record
' Implement this on your own :)

End If
End Sub

Thanks
Mahesh



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend