ASP.net check value exists

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.
 
Well you need to iterate through collection of datarows, using the foreach statement

Check that the row isn't null or that it's rowstate is not deleted. Then compare field "Name" against the e.Values("Name").
 
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
 
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
 
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
 
e is an object of DetailsViewInsertEventArgs that is passed into the method DetailsView1_ItemInserting, e has a property values that contains the values for the new record. So that is the new "item" that could cause the issue so you can compare that against each item in the current collection defined as categories.rows to see if it exists and then you can handle it as you wish.
 
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.
 
Code:
'/// <summary>
'/// Invoked by a DetailsView ItemInserting Event
'/// </summary>
'/// <param name="sender">object that invoked event</param>
'/// <param name="e">object of DetailsViewInsertEventArgs, containing data to be inserted</param>
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()

        'Iterate through rows collection from categories datatable
        For each dr as DataRow in categories.rows()

            'Compare inserted data to current datarow
            'e.Values is a indexed property relating to data to be inserted.
            If e.Values("Name") = dr("Name") Then
                'If same, cancel insert and stop iterating
                e.Cancel = True
                Exit For
            End If

        Next 'Look at next row until empty
    End Sub
 
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.
 
Something like that, It's like I've never used the drag and drop database connections always code or reuse code for greater control.
 
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?
 
First proper project end of October, been doing win forms.net on and off since .net 1.0 beta years ago
 
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.
 
Well, thats the idea, unfortunately anything I need never quite matches so I'm always extending and inheriting. I haven't been able to use a simple grid view:mad:

Just keep looking at examples on MSDN and sites like http://www.codeproject.com
 
Back
Top Bottom