Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Error code "For Each Row As DataGridViewRow In DataGridView1.Rows". Need someone to check code!

Posted on 2011-04-19
8
Medium Priority
?
603 Views
Last Modified: 2012-05-11
Hello,
I'm trying to create code to avoid entry duplicates into database. As I'm a begginer, having now problems.
First I have write a code, which is working but only reading first row of gridview.
 If TextBox1.Text = DataGridView1.Rows(0).Cells(0).Value Then
            MsgBox("Duplicate", MsgBoxStyle.Critical + vbOKOnly, "")
        Else
            MsgBox("OK")
        End If

Open in new window


Then I was want  somehow that code will read entire rows of column/item 0. But of course it is not working, as getting errors that values from access database (format is TEXT) from string to integer is not possible.
I understand what errors means, but do not know how to change below code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim test As String
        For Each Row As DataGridViewRow In DataGridView1.Rows
            test = Row.Cells.Item(0).Value
            If TextBox1.Text = DataGridView1.Rows(test).Cells(0).Value Then
                MsgBox("Duplicate", MsgBoxStyle.Critical + vbOKOnly, "")
            Else
                MsgBox("OK")
            End If
        Next
    End Sub

Open in new window


Thank you in advance for help to anyone.
0
Comment
Question by:dejandejanovic
8 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 800 total points
ID: 35423009
What is the datasource of the grid? If its a datatable, you dont need to loop

dTable.DefaultView.RowFilter = "columnname=" & textbox1.Text
If dTable.DefaultView.Count > 0 Then
   msgbox "Duplicate"
End If
0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 400 total points
ID: 35423072
The code that you have shown should work. For the error you describe (access database), you do not show any code.

Could you show us the code where your error occurs? You will need to cast / convert the access field you are trying to compare with the datagridview amount to integer.
0
 
LVL 9

Assisted Solution

by:mayank_joshi
mayank_joshi earned 800 total points
ID: 35423086
i ve made some corrections.you may try this:-

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each Row As DataGridViewRow In DataGridView1.Rows
            If TextBox1.Text =Row.Cells(0).Value Then
                MsgBox("Duplicate", MsgBoxStyle.Critical + vbOKOnly, "")
            Else
                MsgBox("OK")
            End If
        Next
    End Sub

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dejandejanovic
ID: 35423629
CodeCruiser,

datasource is BindingSource, or in my case TestBindingSource.

how do you declare "dTable"?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35423697
The bindingsource would have a dataset/datatable as its source. dTable was an example. You can replace it with the name of your datatable or if you are using a dataset then use dataset.tables(0)
0
 

Author Comment

by:dejandejanovic
ID: 35424277
All codes are working. But I'm still getting Constrain error for duplicate. How can disable it, or avoid it?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 35424342
So the filtering is working now?

To disable constraints, use
 DataSet.EnforceConstraints = False
0
 

Author Closing Comment

by:dejandejanovic
ID: 35426586
Yes, yes filtering is working. I have found that two things are working with duplicates: one is the code for which I have open this post, another is primary key in database, which I have remove it. So, now only one code preveting duplicate entry.

Thank you for help, Expert members.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question