0

I need to know how to show my search item in a data grid? There is error in grid display part. Can you help me to solve it?

Imports System.Data.OleDb
Imports System.Text



Public Class mainpage
    Inherits System.Windows.Forms.Form
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim cn4 As OleDbConnection
    Dim cmd4 As OleDbCommand
    'Dim cn As OleDbConnection
    'Dim cmd As OleDbCommand
    Dim dr As OleDbDataReader

    Private PrintGrid As DataGridViewPaintParts

    Dim con As New OleDb.OleDbConnection
    Dim cmdOle As New OleDb.OleDbCommand
    Dim dsOle As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim dtOle As New DataTable
    Dim sql As String


Private Sub find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find.Click


        Dim a As String
        'Dim str As String

        cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\HARTI_RES_PRO.accdb;")
        cn.Open()

        Dim whrCond As StringBuilder
        whrCond = New StringBuilder()
        whrCond.Append(" where ")
        'whrCond.Append("Source_Of_Funding LIKE '%" & ComboBox1.SelectedValue & "%'")
        If ComboBox1.SelectedValue = "Researcher's Name" Then
            whrCond.Append(" Researcher LIKE '%" & TextBox1.Text & "%'")

        ElseIf ComboBox1.SelectedValue = "Project title " Then
            whrCond.Append(" Project_title LIKE '%" & TextBox1.Text & "%'")

        ElseIf ComboBox1.Text = "Project code" Then
            whrCond.Append(" Code LIKE '%" & TextBox1.Text & "%'")
            'str = "select * from QAruni where Code LIKE '%" & TextBox1.Text & "%'"
        ElseIf ComboBox1.SelectedValue = "Division" Then
            whrCond.Append(" Division LIKE '%" & TextBox1.Text & "%'")

        ElseIf ComboBox1.SelectedValue = "Co-researchers" Then
            whrCond.Append(" Co_Researchers LIKE '%" & TextBox1.Text & "%'")

        ElseIf ComboBox1.SelectedValue = "Source of funding" Then
            whrCond.Append(" Source_of_funding LIKE '%" & TextBox1.Text & "%'")
        End If
        a = "select * from QAruni" & whrCond.ToString()
        cmd = New OleDbCommand("select * from QAruni" & whrCond.ToString(), cn)
        'MsgBox(a)
        'dr = cmd.ExecuteReader
        'While dr.Read()
        'End While

      da.SelectCommand = cmdOle
        da.Fill(dsOle, "QAruni") ' This Table name
        DataGridView1.DataSource = dsOle
        DataGridView1.DataMember = "QAruni" ' This Table name
        DataGridView1.ReadOnly = True[/COLOR]

 cn.Close()
End sub

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Oxiegen
0

Try this:

Imports System.Data.OleDb
   Imports System.Text

   Public Class mainpage : Inherits System.Windows.Forms.Form
      Dim con As New OleDb.OleDbConnection
      Dim cmdOle As New OleDb.OleDbCommand
      Dim da As OleDb.OleDbDataAdapter
      Dim dtOle As DataTable
      Dim sql As String


      Private Sub find_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles find.Click

         con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\HARTI_RES_PRO.accdb;")
         con.Open()

         Dim whrCond As StringBuilder
         whrCond = New StringBuilder()
         whrCond.Append(" where ")
         Select Case ComboBox.SelectedItem
            Case "Researcher's Name":
               whrCond.Append(" Researcher LIKE '%" & TextBox1.Text & "%'")
            Case "Project title":
               whrCond.Append(" Project_title LIKE '%" & TextBox1.Text & "%'")
            Case "Project code":
               whrCond.Append(" Code LIKE '%" & TextBox1.Text & "%'")
            Case "Division":
               whrCond.Append(" Division LIKE '%" & TextBox1.Text & "%'")
            Case "Co-researchers":
               whrCond.Append(" Co_Researchers LIKE '%" & TextBox1.Text & "%'")
            Case "Source of funding":
               whrCond.Append(" Source_of_funding LIKE '%" & TextBox1.Text & "%'")
         End Select

         sql = "select * from QAruni" & whrCond.ToString()
         cmdOle = New OleDbCommand(sql, con)

         dtOle = New DataTable("QAruni")
         da = New OleDb.OleDbDataAdapter(cmdOle)
         da.Fill(dtOle)
         con.Close()

         DataGridView1.DataSource = dtOle
         DataGridView1.ReadOnly = True
      End sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.