none
Check to see if something is in a datagrid view RRS feed

  • Question

  • Hello,

    What would be the best method to check to see if 2 items are in a dataview. The user would input a product key and user id into two seperate textboxes. If the two items that are in the text box, match and they are on the same row then activate the program and proceed to the login. (the login will work the same way only with a username and password). What code would be the best to use to do this? Please also note that the information in the datagrid view is taken from a database.


    Wednesday, July 18, 2018 9:42 PM

All replies

  • Hi

    Here is one of many ways to do this. It is unlikely that this is the 'best way', but it is very straightforward to understand. Ask 10 coders which is the 'best way' and you would likely get different answers from both of them.

    This is a test example and is a stand alone project.

    ' Form1 with blank DataGridView1
    ' and TextBox1 and TextBox2
    Option Strict On
    Option Explicit On
    Class Form1
      Dim myTable As New DataTable("Freddy")
      Dim BS As New BindingSource
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' some test data
        With myTable
          .Columns.Add("Product Key")
          .Columns.Add("User ID")
    
          .Rows.Add("ABC-99", 1)
          .Rows.Add("DEF-199", 2)
          .Rows.Add("ZZY-999", 3)
          .Rows.Add("CAB-11", 4)
          .Rows.Add("FGH-111", 5)
        End With
        BS.DataSource = myTable
        DataGridView1.DataSource = BS
      End Sub
      Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged, TextBox1.TextChanged
        Dim r As Integer = -1
        For Each dr As DataRow In myTable.Rows
          r += 1
          If dr.Item(0).ToString = Trim(TextBox1.Text) AndAlso dr.Item(1).ToString = Trim(TextBox2.Text) Then
            ' CONTINUE APPLICATION
            ' CREDENTIALS MATCH
            MessageBox.Show("USER: " & dr.Item(1).ToString & " match found.")
            Exit For
          End If
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, July 18, 2018 10:34 PM
    Wednesday, July 18, 2018 10:33 PM
  • Hi 

    This is another way, I hope I can help you.

    Imports System.Data.SqlClient
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From Login", conn)
                dt = New DataTable()
                sda.Fill(dt)
                DataGridView1.DataSource = dt
            End Using
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If tb_Username.Text <> Nothing And tb_Password.Text <> Nothing Then
                For Each item As DataGridViewRow In DataGridView1.Rows
                    If item.Cells(0).Value = tb_Username.Text And item.Cells(1).Value = tb_Password.Text Then
                        MainForm.Show()
                        Me.Hide()
                    End If
                Next
            Else
                MessageBox.Show("Username and password cannot be empty")
            End If
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 19, 2018 5:09 AM
  • Hello Alex,

    I tried your code and I was able to access my database. However, when I went to check to see if the user id and the product key were in there, I got the following error from visual studio:

    "An unhandled exception of type 'System.InvalidCastExpection' occured in Microsoft.VisualBasic.dll

    Addtional Information: Conversion from string "What was in textbox 1" to type 'Double' is not valid."

    Also, I forgot to mention that the database has an account activated feature which I would like to change to a yes in the database and have the program check to see if the account is activated at login, if not, it doesn't login.

    Why is the error being thrown and how can I accomplish the account active checking and adding yes to the database?

    Friday, July 20, 2018 2:23 AM
  • Hello Alex,

    I tried your code and I was able to access my database. However, when I went to check to see if the user id and the product key were in there, I got the following error from visual studio:

    "An unhandled exception of type 'System.InvalidCastExpection' occured in Microsoft.VisualBasic.dll

    Addtional Information: Conversion from string "What was in textbox 1" to type 'Double' is not valid."

    Has the DataGridView read out the data in the database?Please provide your code so I can assist you.

    I am not sure where there is data conversion.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 20, 2018 3:03 AM
  • Alex,

    Here is the form's entire code:

    Imports System.Data.SqlClient
    
    Public Class NWIDC_Activation
        Dim constr As String = "Server=SERVER;Database=DATABASE;User=USERNAME;Pwd=PASSWORD;"
        Dim conn As SqlConnection
        Dim sda As SqlDataAdapter
        Dim dt As DataTable
        Public SQL As New SQLControl
    
        Private Sub NWIDC_Activation_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Using conn = New SqlConnection(constr)
                conn.Open()
                sda = New SqlDataAdapter("Select * From nwidc_users", conn)
                dt = New DataTable()
                sda.Fill(dt)
                dgvData.DataSource = dt
            End Using
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If TextBox1.Text <> Nothing And TextBox2.Text <> Nothing Then
                For Each item As DataGridViewRow In dgvData.Rows
                    If item.Cells(0).Value = TextBox1.Text And item.Cells(1).Value = TextBox2.Text Then
                        Form1.Show()
                        MessageBox.Show("The NWIDC is offically activated. You may now login.")
                        My.Settings.FirstLaunch = False
                        Me.Hide()
                    End If
                Next
            Else
                MessageBox.Show("Username and password cannot be empty")
            End If
        End Sub
    End Class

    The form however does display the information in a data grid view like it should.

    Hope this helps and thank you for your help!


    Friday, July 20, 2018 3:39 AM
  • Hi

    try to modify the code

     DataGridView1.AllowUserToAddRows = False

    If item.Cells(0).Value.ToString() = TextBox1.Text And item.Cells(1).Value.ToString() = TextBox2.Text


    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 20, 2018 5:16 AM