ASP.net check value exists

Capodecina
Permabanned
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"

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>
 
Doh!

I still get the same error when using:

Code:
Imports ManHoursTableAdapters


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 categoriesAdapter As New ManHoursTableAdapters.tblCategoriesTableAdapter
        Dim categories As New ManHours.tblCategoriesDataTable

        categories = categoriesAdapter.GetCategories()

        Dim isDuplicate As Boolean = [B][U][COLOR="Red"]ManHoursTableAdapters.tblCategoriesTableAdapter.GetCategories[/COLOR][/U][/B](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
 
Thanks, but it doesn't like me adding the e.Value line to there..

The line highlighted gives me the error

Value of type 'ManHours.tblCategoriesRow' cannon be converted to 'ManHours.tblCategories.DataTable'.

Code:
Imports ManHoursTableAdapters


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 categoriesAdapter As New ManHoursTableAdapters.tblCategoriesTableAdapter
        Dim categories As New ManHours.tblCategoriesDataTable

        [B]categories = categoriesAdapter.GetCategories(e.Values("Name"))[/B]

        If categories.Count > 0 Then
            Label1.Text = "Record already exists, please try again"
        End If
    End Sub

End Class
 
Still getting the same error:

Error 1 Value of type 'ManHours.tblCategoriesRow' cannot be converted to 'ManHours.tblCategoriesDataTable'. H:\Visual Studio 2005\WebSites\ManHoursUPDATED\Management\Copy of Categories.aspx.vb 13 22 H:\...\ManHoursUPDATED\

Code:
Imports ManHoursTableAdapters


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 categoriesAdapter As New ManHoursTableAdapters.tblCategoriesTableAdapter
        Dim categories As ManHours.tblCategoriesDataTable

        categories = [COLOR="Red"][B]categoriesAdapter.GetCategories(e.Values("Name"))[/B][/COLOR]

        If categories.Count > 0 Then
            Label1.Text = "Record already exists, please try again"
        End If
    End Sub

End Class

:(
 
That's still leaving me with the same error :(

The GetCategories query looks fine to me?

SELECT Name FROM lward.tblCategories ORDER BY Name
 
That allows me to run it, but when I enter a value that already exists it doesn't set the label and instead crashes and gives the following error:

Violation of PRIMARY KEY constraint 'PK_tblCategories_1'. Cannot insert duplicate key in object 'tblCategories'.
The statement has been terminated.


Eeeek :(

Thanks for this by the way, you're a good guy :D
 
I tried that but it doesn't stop it :\

Code:
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting

        Dim categoriesAdapter As New ManHoursTableAdapters.tblCategoriesTableAdapter()

        Dim categories As ManHours.tblCategoriesDataTable = categoriesAdapter.GetCategories

        If categories.Count = 1 Then
            Label1.Text = "Record already exists, please try again"
            e.Cancel = True
        End If
    End Sub
 
thats good, you just need to set it for the correct situation now.

It's getting frustrating because I can't get it to work. Not that many people really seem to know that much about ASP either so i'm struggling with a couple of things, especially this.
 
My programming isn't great so bear with me, i'm guessing i've done this totally the wrong way as it's not working...

The highlighted line gives the error "Type DataRow not defined"

Code:
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting

        Dim categoriesAdapter As New ManHoursTableAdapters.tblCategoriesTableAdapter()
        Dim categories As ManHours.tblCategoriesDataTable = categoriesAdapter.GetCategories()

        For each dr as [B][COLOR="Red"]DataRow[/COLOR][/B]

            If TextBox1.Text = dr("Name") Then
                e.Cancel = True
                Exit For

            End If
        Next
    End Sub
 
I'm not quite sure I understand how you're using e.Values name in that If statement.

Is it ok to use there because it's inside the for statement which says DataRow in categories.rows()?

Thanks for this help it's appreciated
 
Right what you need is the rows collection for the tblcategories Data Table.
You have this datatable set in the code below, now try stepping through that.

Code:
Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs) Handles DetailsView1.ItemInserting

        Dim categoriesAdapter As New ManHoursTableAdapters.tblCategoriesTableAdapter()
        Dim categories As ManHours.tblCategoriesDataTable = categoriesAdapter.GetCategories()

        For each dr as DataRow [COLOR="Red"]in categories.rows()[/COLOR]

            If [COLOR="Red"]e.Values("Name")[/COLOR] = dr("Name") Then
                e.Cancel = True
                Exit For

            End If
        Next
    End Sub

Still get DataRow is not defined on the line:

For Each dr As DataRow In categories.Rows()
 
That's cracked it, thank you very much :)

I don't suppose you could do me a huge favour and comment that code so I can understand a little better what's going on. I suppose I understand all of it besides the whole e.Values thing, I just don't really get properly how it works.
 
I'll give it a go tonight if I have time, I've never used DetailsView:eek:

Ok :)

How come? I've found it to be pretty good for setting up a basic form time entry control.

I suppose if you want more customisation you'd just use the form view.
 
Yeah that's the way to do it of course. I'm only just learning so i'll get to that eventually. Thanks for commenting the code that's spot on.

I don't use the SqlConnection etc but I do make use of the ObjectDataSource which just links to the query you created in the DataSet.

I know how to bind data to gridviews and so on without using a control but I can't see much of a downside to using the ObjectDataSource.

Thanks again!

How long have you been coding ASP.net?
 
Ahh so you've had some good programming experience before hand.

It's all still quite new to me, I think ASP.net is paticularly useful if you need to develop something fast though.
 
Back
Top Bottom