locked
Checking for value in specific column RRS feed

  • Question

  • Hi, how could I validate if the value from textbox is already in specific column then if is there I could insert to the database.  Thanks
    Saturday, May 5, 2018 4:32 PM

Answers

  • Hi BloodySandwich,

    If you like to insert data into sql database, and I do one example about this, I put two datagridview, one is to display datatable data, another is to displat data that inserted into database, one TextBox, one Button.

       Dim dt As New DataTable
        Private Sub Frmdatatable_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With dt
                .Columns.Add("Room No", GetType(String))
                .Columns.Add("Device ID", GetType(String))
                .Columns.Add("Device Name", GetType(String))
    
                .Rows.Add("111", "1", "Printer")
                .Rows.Add("111", "2", "Printer")
                .Rows.Add("112", "3", "Monitor")
                .Rows.Add("112", "4", "Monitor")
            End With
            DataGridView1.DataSource = dt
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            dt.DefaultView.RowFilter = "[Room No] ='" & TextBox1.Text & "'"
            If dt.DefaultView.Count > 0 Then
                DataGridView2.DataSource = dt.DefaultView
                For Each row As DataGridViewRow In DataGridView2.Rows
                    insertdata(row.Cells("Room No").Value, row.Cells("Device ID").Value, row.Cells("Device Name").Value)
                Next
                MessageBox.Show("insert successfully!")
            Else
                Return
            End If
        End Sub
        Private Sub insertdata(RoomNo As String, DeviceID As String, DeviceName As String)
            Dim str As String = "Data Source=(LOCALDB)\MSSQLLOCALDB;Initial Catalog=Testlocaldb;Integrated Security=True"
            Dim sql As String = "insert into test6 (RoomNo,DeviceID,DeviceName) values(@RoomNo,@DeviceID,@DeviceName) "
            Using conn As New SqlConnection(str)
                conn.Open()
                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@RoomNo", RoomNo)
                    cmd.Parameters.AddWithValue("@DeviceID", DeviceID)
                    cmd.Parameters.AddWithValue("@DeviceName", DeviceName)
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub

    Best Regards,

    Cherry


    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.

    • Proposed as answer by Stanly Fan Thursday, May 10, 2018 9:31 AM
    • Marked as answer by BloodySandwich Monday, May 28, 2018 7:20 PM
    Monday, May 7, 2018 7:00 AM

All replies

  • If a value from a TextBox is in a specific column of what?

    La vida loca

    Saturday, May 5, 2018 6:04 PM
  • Hi Mr Monkeyboy,

    Let's say I have 3 columns RoomNo, DeviceID, DeviceName. I would like to check if the number from textbox is the same as number for room, if yes then insert to database. There is also others columns I need to type and check ,but I'm  just looking for example how to do it . Thanks



    Saturday, May 5, 2018 6:26 PM
  • Hi

    As Mr.Monkeyboy already asked, what column?

    Is the column a DataGridViewColumn or a DataTable Column or part of the Acropolis of Athens?

    You need to consider the information that we, the readers, of your post can get from your words. There are lots of things that 'Column' can apply to.


    Regards Les, Livingston, Scotland

    Saturday, May 5, 2018 7:53 PM
  • Hey, is DataTable Column.
    Saturday, May 5, 2018 7:55 PM
  • or part of the Acropolis of Athens?


    Regards Les, Livingston, Scotland

    Now I'd like to see code for that one!


    La vida loca

    Saturday, May 5, 2018 8:09 PM
  • Hi

    Here is some code that goes a little way towards what I think you are asking for. You select the column to search and the search term. The results are shown in the bottom DGV.

    ' Form1 DataGridView1,
    ' DataGridView2, TextBox1,
    ' TextBox2, Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("freddy")
      Dim dt2 As New DataTable("freddy2")
    
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '  Size = New Size(600, 500)
        With dt
          .Columns.Add("Room No", GetType(String))
          .Columns.Add("Device ID", GetType(String))
          .Columns.Add("Device Name", GetType(String))
    
          .Rows.Add("111", "1", "Printer")
          .Rows.Add("111", "2", "Printer")
          .Rows.Add("112", "3", "Monitor")
          .Rows.Add("112", "4", "Monitor")
        End With
        With dt2
          .Columns.Add("Room No", GetType(String))
          .Columns.Add("Device ID", GetType(String))
          .Columns.Add("Device Name", GetType(String))
        End With
        DataGridView1.DataSource = dt
        DataGridView2.DataSource = dt2
        For Each c As DataColumn In dt.Columns
          ComboBox1.Items.Add(c.ColumnName)
        Next
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ComboBox1.SelectedIndex < 0 Or TextBox1.Text = Nothing Then Exit Sub
    
        Dim expr As String = "[" & ComboBox1.SelectedItem.ToString & "] Like '" & TextBox1.Text & "'"
        Dim relRows() As DataRow = dt.Select(expr)
    
        dt2.Clear()
        For Each r As DataRow In relRows
          dt2.Rows.Add(r.Item(0), r.Item(1), r.Item(2))
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Saturday, May 5, 2018 9:21 PM
  • Hi BloodySandwich,

    If you like to insert data into sql database, and I do one example about this, I put two datagridview, one is to display datatable data, another is to displat data that inserted into database, one TextBox, one Button.

       Dim dt As New DataTable
        Private Sub Frmdatatable_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With dt
                .Columns.Add("Room No", GetType(String))
                .Columns.Add("Device ID", GetType(String))
                .Columns.Add("Device Name", GetType(String))
    
                .Rows.Add("111", "1", "Printer")
                .Rows.Add("111", "2", "Printer")
                .Rows.Add("112", "3", "Monitor")
                .Rows.Add("112", "4", "Monitor")
            End With
            DataGridView1.DataSource = dt
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            dt.DefaultView.RowFilter = "[Room No] ='" & TextBox1.Text & "'"
            If dt.DefaultView.Count > 0 Then
                DataGridView2.DataSource = dt.DefaultView
                For Each row As DataGridViewRow In DataGridView2.Rows
                    insertdata(row.Cells("Room No").Value, row.Cells("Device ID").Value, row.Cells("Device Name").Value)
                Next
                MessageBox.Show("insert successfully!")
            Else
                Return
            End If
        End Sub
        Private Sub insertdata(RoomNo As String, DeviceID As String, DeviceName As String)
            Dim str As String = "Data Source=(LOCALDB)\MSSQLLOCALDB;Initial Catalog=Testlocaldb;Integrated Security=True"
            Dim sql As String = "insert into test6 (RoomNo,DeviceID,DeviceName) values(@RoomNo,@DeviceID,@DeviceName) "
            Using conn As New SqlConnection(str)
                conn.Open()
                Using cmd As New SqlCommand(sql, conn)
                    cmd.Parameters.AddWithValue("@RoomNo", RoomNo)
                    cmd.Parameters.AddWithValue("@DeviceID", DeviceID)
                    cmd.Parameters.AddWithValue("@DeviceName", DeviceName)
                    cmd.ExecuteNonQuery()
                End Using
            End Using
        End Sub

    Best Regards,

    Cherry


    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.

    • Proposed as answer by Stanly Fan Thursday, May 10, 2018 9:31 AM
    • Marked as answer by BloodySandwich Monday, May 28, 2018 7:20 PM
    Monday, May 7, 2018 7:00 AM