ClientSide error checking before submit Asp.net/Vb.net

I have a column in a datagrid that I want to check to see a sequence.

The column can contain X number of items. They are integer values that represent a sequence.

So something like

1.
2.
3.
4.
5.
6.
7.

I want to give them the ability to change the sequence, but I need to ensure that there are no duplicates. Each has to be unique. Is there a way for me, when they click the submit button to check if there are duplicates, and if it is, pop up a message. Since it is a datagrid, I might even process it on the server, and then send a message back saying sorry but they arnt unique. I know how to parse through the datagrid on the server side. Just need the logic and code either client or server to do a constraint check.

Thanks.

Cristian.



Term56Asked:
Who is Participating?
 
CetusMODCommented:
PAQed with points refunded (500)

CetusMOD
Community Support Moderator
0
 
JRossi1Commented:
Try using a loop to count the distinct values in a column, then you could use a second loop to extract the rows with counts greater then 1.

Public Function GetDuplicates(ByVal table As DataTable, ByVal field As
String) As DataTable
        Dim hash As New Hashtable
        For Each row As DataRow In table.Rows
            Dim key As Object = row(field)
            If hash.Contains(key) Then
                hash(key) = CInt(hash(key)) + 1
            Else
                hash.Add(key, 1)
            End If
        Next


        Dim clone As DataTable = table.Clone()

        For Each de As DictionaryEntry In hash
            If CInt(de.Value) > 1 Then
                For Each row As DataRow In
                    table.Select(String.Format("{0}='{1}'", field, de.Key))
                    clone.ImportRow(row)
                Next
            End If
        Next
        Return clone
End Function


Then you can use it something like:


    Dim myTable As DataTable
    Dim duplicates As DataTable
    duplicates = GetDuplicates(myTable, "Zip")


0
 
Term56Author Commented:
so let me understand, for each pass through GetDuplicates, it takes one item, goes through the table, and determines if there are 2 of the same kind ?
0
 
Term56Author Commented:
solved it myself

Private Function Sequence_Correct()
        Dim dgItem As DataGridItem
        Dim strdgItemToAnalyze As String
        Dim dgItemLoop As DataGridItem
        Dim intCounter As Integer = 0
            'Loops through each item in the datagrid to ensure there are no duplicate entries
            For Each dgItem In dgList.Items
                strdgItemToAnalyze = CType(dgItem.Cells(0).FindControl("Sequence"), DropDownList).SelectedValue
                'Checks to see if there is more than 1 instance of any sequence
                intCounter = 0
                For Each dgItemLoop In dgList.Items
                    If CType(dgItemLoop.Cells(0).FindControl("Sequence"), DropDownList).SelectedValue = strdgItemToAnalyze Then
                        intCounter += 1
                    End If
                Next
                If intCounter > 1 Then
                    'If there is more than one instance, return false
                    Return False
                End If
            Next
         'If it reaches here, means there are no duplicates
          Return True
End Function
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.