Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi,

i have created Module.vb as database directory in the name of ConnDB.vb and Private sub ConnD()


i want to try search record which is showing in Datagridview1 and filtering rows ... i have tried but its seems to be error....

Note :

1. I want to Search CODE in txtsearch textbox

Please suggest basic search record sql statement

What I have tried:

VB
Imports System.Data.OleDb


Public Class SongList
    Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader
    Dim sql As String
    Dim dataFile As String
    Dim ds As New DataSet
    Dim con As New OleDbConnection
    Dim da As New OleDb.OleDbDataAdapter

    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        Song_Entry.Show()
        Close()

    End Sub

    Public Sub Load()
        ConnD()
        Sql = "select * from SONGS"
        cmd = New OleDbCommand(sql, conn)
        dr = cmd.ExecuteReader()
        DataGridView1.Rows.Clear()

        Do While dr.Read = True
            DataGridView1.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4))

        Loop

        conn.Close()

    End Sub



    Private Sub SongList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Load()
        Me.KeyPreview = True
    End Sub

    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
        Close()

    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        Dim CODE, ETITLE, TTITLE, SONGT As String

        CODE = txtCode.Text
        ETITLE = txtET.Text
        TTITLE = txtTT.Text
        SONGT = RichTextBox1.Text


        Try
            sql = "update SONGS set ETITLE = ?,TTITLE = ?,SONGT = ? where CODE = ? "
            ConnD()
            cmd = New OleDbCommand(sql, conn)
            cmd.Parameters.AddWithValue("ETITLE", ETITLE)
            cmd.Parameters.AddWithValue("TTITLE", TTITLE)
            cmd.Parameters.AddWithValue("SONGT", SONGT)
            cmd.Parameters.AddWithValue("CODE", CODE)

            cmd.ExecuteNonQuery()
            Console.WriteLine(Command)
            MessageBox.Show("Updated")


            conn.Close()
            txtCode.Enabled = False
            txtET.Enabled = False
            txtTT.Enabled = False
            RichTextBox1.Enabled = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        ConnD()
        sql = "select * from SONGS where CODE = ?"
        cmd = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("CODE", DataGridView1.CurrentRow.Cells(0).Value.ToString())

        dr = cmd.ExecuteReader()

        If (dr.Read) Then
            txtCode.Text = dr(0).ToString
            txtET.Text = dr(1).ToString
            txtTT.Text = dr(2).ToString
            RichTextBox1.Text = dr(3).ToString

        End If


        conn.Close()




    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Dim CODE As String

        CODE = txtCode.Text

        Try
            sql = "delete from SONGS where CODE = ? "
            ConnD()
            cmd = New OleDbCommand(sql, conn)
            
            cmd.Parameters.AddWithValue("CODE", CODE)

            cmd.ExecuteNonQuery()
            Console.WriteLine(Command)
            MessageBox.Show("Deleted")

            Load()

            conn.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtsearch_TextChanged(sender As Object, e As EventArgs) Handles txtsearch.TextChanged
       
    End Sub

    Private Sub DataGridView1_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick
        Dim form As New SongShow
        
        form.RichTextBox1.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
        form.RichTextBox2.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
        form.RichTextBox3.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString()
        form.ShowDialog()
    End Sub

    Private Sub SongList_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Escape Then

            txtEsearch.Clear()
            txtsearch.Clear()
            txtTsearch.Clear()
            txtsearch.Focus()

        End If
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click

    End Sub

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles txtEsearch.TextChanged
        Dim selStart As Integer = txtEsearch.SelectionStart
        txtEsearch.Text = txtEsearch.Text.ToUpper()
        txtEsearch.SelectionStart = selStart
    End Sub

    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click

        'for add
        txtCode.Enabled = True
        txtET.Enabled = True
        txtTT.Enabled = True
        RichTextBox1.Enabled = True
    End Sub

    Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
        If e.KeyCode = Keys.Enter Then

            Dim form As New SongShow

            form.RichTextBox1.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
            form.RichTextBox2.Text = DataGridView1.CurrentRow.Cells(3).Value.ToString()
            form.RichTextBox3.Text = DataGridView1.CurrentRow.Cells(4).Value.ToString()
            form.ShowDialog()
        End If
    End Sub
End Class
Posted
Updated 4-Feb-20 13:14pm
v3
Comments
Richard Deeming 29-Jan-20 11:44am    
If you want us to help you fix an error, you need to tell us what the error is and where it's thrown from.

Click the green "Improve question" link and update your question. Add the full details of the exception, and indicate which line of code it comes from.
Member 14621280 29-Jan-20 11:47am    
Private Sub txtsearch_TextChanged(sender As Object, e As EventArgs) Handles txtsearch.TextChanged

ConnD()

Dim sql = "SELECT * from SONGS WHERE "
If Not String.IsNullOrWhiteSpace(txtsearch.Text) Then
sql = sql & "CODE LIKE @CODE OR "
cmd.Parameters.AddWithValue("@CODE", txtsearch.Text)
End If
conn.Close()


End Sub
Richard Deeming 29-Jan-20 11:51am    
And the error is???
Member 14621280 29-Jan-20 11:56am    
did'nt show any error and nothing filtering
Richard Deeming 29-Jan-20 11:59am    
Well, based on the code snipped you posted in your comment - which isn't part of the code you posted in your question! - it's hardly surprising.

You call ConnD. You create a string with a SQL query, and add a parameter to your OleDbCommand. You then close the connection and return.

There's nothing in that method to execute the SQL query, so it's hardly surprising that the SQL query doesn't execute.

1 solution

Few suggestions:

Instead of using a reader in Load method to fill the grid, get the data into a data table and use that as a data source. See DbDataAdapter.Fill Method (System.Data.Common) | Microsoft Docs[^] and DataGridView.DataSource Property (System.Windows.Forms) | Microsoft Docs[^]

In btnUpdate_Click, try using named parameters in your query that match the parameter names you use when adding the value. In other words
VB
sql = "update SONGS set ETITLE = @ETITLE, TTITLE = @TTITLE, SONGT = @SONGT where CODE = @CODE"

Always remember to use the Using Statement - Visual Basic | Microsoft Docs[^] to properly dispose the database objects

For practical examples I suggest going through Properly executing database operations[^] . The logic is the exact same for OleDb objects
 
Share this answer
 
Comments
Member 14621280 29-Jan-20 12:37pm    
No auctually update is everything is ok only thing is searching record is having problem
Wendelius 29-Jan-20 12:38pm    
I understand that but please read all the suggestions.


Maciej Los 29-Jan-20 14:00pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900