pitaridis
Well-known member
- Joined
- Nov 18, 2005
- Messages
- 63
- Programming Experience
- 10+
I usually display, edit and delete information using a GridView and I think that it is very easy. The following example shows how I usually do it.
The above code uses the AccessDataSource control because the data have been stored in an Access Database. If I have to get data from an SQL server I use the equivalent SqlDataSource control. My problem is that I had to use this logic to access, edit and delete data inside a server side control. This means that I have to make it 100% programmatically. After converting the above code I had the following code.
This code displays the data but when I try to edit, delete, sort or change page an error message occurs. For example when I try to delete data the error message is the following:
The GridView '' fired event RowDeleting which wasn't handled.
Can someone help me or suggest a different approach order to solve my problem?
Thanks.
VB.NET:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Cid"
DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="Cid" HeaderText="Cid" ReadOnly="True" SortExpression="Cid" />
<asp:BoundField DataField="Cname" HeaderText="Cname" SortExpression="Cname" />
<asp:BoundField DataField="DispPosition" HeaderText="DispPosition" SortExpression="DispPosition" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/Database.mdb"
DeleteCommand="DELETE FROM [Categories] WHERE [Cid] = ?"
InsertCommand="INSERT INTO [Categories] ([Cid], [Cname], [DispPosition]) VALUES (?, ?, ?)"
SelectCommand="SELECT [Cid], [Cname], [DispPosition] FROM [Categories]"
UpdateCommand="UPDATE [Categories] SET [Cname] = ?, [DispPosition] = ? WHERE [Cid] = ?">
<DeleteParameters>
<asp:Parameter Name="Cid" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Cid" Type="String" />
<asp:Parameter Name="Cname" Type="String" />
<asp:Parameter Name="DispPosition" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Cid" Type="String" />
<asp:Parameter Name="Cname" Type="String" />
<asp:Parameter Name="DispPosition" Type="Int32" />
</InsertParameters>
</asp:AccessDataSource>
</form>
</body>
</html>
The above code uses the AccessDataSource control because the data have been stored in an Access Database. If I have to get data from an SQL server I use the equivalent SqlDataSource control. My problem is that I had to use this logic to access, edit and delete data inside a server side control. This means that I have to make it 100% programmatically. After converting the above code I had the following code.
VB.NET:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Dim AccessDataSource1 As New AccessDataSource
Dim Gridview1 As New GridView
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Setup AccessDataSource
AccessDataSource1.DataFile = "~/App_Data/Database.mdb"
AccessDataSource1.DeleteCommand = "DELETE FROM [Categories] WHERE [Cid] = ?"
AccessDataSource1.InsertCommand = "INSERT INTO [Categories] ([Cid], [Cname], [DispPosition]) VALUES (?, ?, ?)"
AccessDataSource1.SelectCommand = "SELECT [Cid], [Cname], [DispPosition] FROM [Categories]"
AccessDataSource1.UpdateCommand = "UPDATE [Categories] SET [Cname] = ?, [DispPosition] = ? WHERE [Cid] = ?"
AccessDataSource1.DeleteParameters.Add("Cid", "String")
AccessDataSource1.UpdateParameters.Add("Cid", "String")
AccessDataSource1.UpdateParameters.Add("Cname", "String")
AccessDataSource1.UpdateParameters.Add("DispPosition", "Int32")
AccessDataSource1.InsertParameters.Add("Cid", "String")
AccessDataSource1.InsertParameters.Add("Cname", "String")
AccessDataSource1.InsertParameters.Add("DispPosition", "Int32")
' Setup GridView
GridView1.AllowPaging = True
GridView1.AllowSorting = True
GridView1.AutoGenerateColumns = False
Dim DataKeyNames() As String = {"Cid"}
GridView1.DataKeyNames = DataKeyNames
Dim Cid As New BoundField
Cid.DataField = "Cid"
Cid.HeaderText = "Cid"
Cid.ReadOnly = True
Cid.SortExpression = "Cid"
Cid.Visible = False
GridView1.Columns.Add(Cid)
Dim Cname As New BoundField
Cname.DataField = "Cname"
Cname.HeaderText = "Cname"
Cname.SortExpression = "Cname"
GridView1.Columns.Add(Cname)
Dim DispPosition As New BoundField
DispPosition.DataField = "DispPosition"
DispPosition.HeaderText = "DispPosition"
DispPosition.SortExpression = "DispPosition"
GridView1.Columns.Add(DispPosition)
Dim MyCommandField As New CommandField()
MyCommandField.ShowSelectButton = True
MyCommandField.ShowDeleteButton = True
Gridview1.Columns.Add(MyCommandField)
GridView1.DataSource = AccessDataSource1
GridView1.DataBind()
Panel1.Controls.Add(Gridview1)
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</form>
</body>
</html>
This code displays the data but when I try to edit, delete, sort or change page an error message occurs. For example when I try to delete data the error message is the following:
The GridView '' fired event RowDeleting which wasn't handled.
Can someone help me or suggest a different approach order to solve my problem?
Thanks.