I am developing a windows based application in vb.net 2005 and data base is SQL2000 I have database named new base and table name is Trick and fields are ItId, ItName, ItPrice, ItQuantity, itstock
Problem is that I want to populate data gridview through search data from text box but only field names display and data is not displayed in datagridview can any one help me
This is the code I am using……..

Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Data.SqlTypes

Public Class SearchItem
Dim currRec = 0
Dim totalrec = 0
Dim dt As New DataTable
Dim ds As New DataSet()
Private Sub btnsea_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsea.Click
Dim ConnectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NewBase;Data Source=ROCK"

Dim conn As New SqlConnection(ConnectionString)
Dim cmdstr As String = "Select * from trick where ItName = '& txtsea.text &'"
Dim DataAdapter As New SqlDataAdapter(cmdstr, conn)
DGVList.DataSource = (ds.Tables(0))
End Sub

Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
End Sub
End Class

SQL table-

create table Information(roll int,name varchar(20),marks int)
insert into Information values(1,'sonia',10)
insert into Information values(2,'sonu',20)
insert into Information values(3,'soni',30)
select * from Information
Imports System.Data.SqlClient
Public Class Form1
    Dim conn As New SqlConnection("Data Source=SONIA-B408A4159\SQLEXPRESS;Initial Catalog=sonia;Integrated security=true;")
    Dim query As String
    Dim cmd As New SqlCommand
    Dim da As SqlDataReader
    Private Sub btnRetrive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetrive.Click
        Dim RowNum As Integer
        query = "Select * from Information where roll='" & txtRoll.Text & "'"
            cmd = New SqlCommand(query, conn)
            da = cmd.ExecuteReader()
            While da.Read()
                If da.HasRows Then
                    For RowNum = 0 To Me.DataGridView1.RowCount - 1
                        Me.DataGridView1.Item(0, RowNum).Value = da.GetValue(1)
                        Me.DataGridView1.Item(1, RowNum).Value = da.GetValue(2)
                End If
            End While
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
End Class

I hope u want this only if there is any problem..Or u want something else..then reply...

This article has been dead for over six months. Start a new discussion instead.