- Joined
- 31 Dec 2003
- Posts
- 5,172
- Location
- Barrow-In-Furness
I'm inserting one value into a table and everything works fine, the only problem is when the value has already been entered.
I'm using a DetailsView with just one input field, how can I check if the value exists already so that the application doesn't crash and an Label or error is just displayed instead?
I'm trying to do it using the following method but it's not working..i'm getting the following error on the line i've highlighted:
"Reference to non shared member requires an object reference"
I'm using a DetailsView with just one input field, how can I check if the value exists already so that the application doesn't crash and an Label or error is just displayed instead?
I'm trying to do it using the following method but it's not working..i'm getting the following error on the line i've highlighted:
"Reference to non shared member requires an object reference"
Code:
Partial Class ManHours_Categories
Inherits System.Web.UI.Page
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting
Dim isDuplicate As Boolean = ManHoursTableAdapters.tblCategoriesTableAdapter.GetCategories(e.Values("Name")) IsNot Nothing
If isDuplicate Then
Label1.Text = String.Format("Category '(0)' already exists", e.Values("Name"))
e.Cancel = True
End If
End Sub
End Class
Code:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Copy of Categories.aspx.vb" Inherits="ManHours_Categories" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
Currently active categories:<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="Name" DataSourceID="ObjectDataSource1" GridLines="None" Width="141px">
<Columns>
<asp:BoundField DataField="Name" ReadOnly="True" ShowHeader="False" SortExpression="Name">
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories"
TypeName="ManHoursTableAdapters.tblCategoriesTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_Name" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Original_Name" Type="String" />
</UpdateParameters>
</asp:ObjectDataSource>
<br />
<br />
<br />
To create a new time code below, enter the name and click Insert.<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Name"
DataSourceID="ObjectDataSource2" DefaultMode="Insert" GridLines="None" Height="86px"
Width="296px">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name">
<ItemStyle VerticalAlign="Top" />
<HeaderStyle VerticalAlign="Top" />
</asp:BoundField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetCategories"
TypeName="ManHoursTableAdapters.tblCategoriesTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp:Parameter Name="Original_Name" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Original_Name" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
</InsertParameters>
</asp:ObjectDataSource>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</asp:Content>