hi can anyone help me . when the user enter the name in a textbox and click the search button, the output should be display in the datagrid... Help plz have to submit my project in 8 days

Dani AI

Generated

Quick answer for : filter on the server, return a DataTable, then bind that DataTable to your grid. That keeps the UI snappy for large tables and avoids loading everything into the page. was on the right track with “tie the grid to a filter,” but don’t do it by stuffing the textbox value directly into a SQL string — that leads to syntax bugs and SQL injection.

Two common mistakes seen above (and why they break things): 1) concatenating user text into SQL (vulnerable and fragile); 2) using a DataReader check incorrectly or binding a reader that has already been read to the end. Also be sure connections are closed/disposed — use Using blocks so you do not leak connections, and avoid calling ExecuteReader/ExecuteNonQuery twice for the same command.

A safe, simple pattern (VB.NET Web Forms) is: prepare a parameterized query, fill a DataTable with a DataAdapter, then set the grid DataSource = table and DataBind. Example (conceptual — adjust names to your project):

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
    Dim sql As String = "SELECT Id, Name, Email FROM Users WHERE Name LIKE @term"
    Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConn").ConnectionString)
        Using cmd As New SqlCommand(sql, conn)
            cmd.Parameters.Add("@term", SqlDbType.NVarChar).Value = "%" & txtName.Text.Trim() & "%"
            Dim dt As New DataTable()
            Using da As New SqlDataAdapter(cmd)
                da.Fill(dt)
            End Using
            GridResults.DataSource = dt
            GridResults.DataBind()
        End Using
    End Using
End Sub

Notes for and others using Access/OleDb: OleDb uses positional parameters (?) rather than named @param, and Access wildcards (by default) are */? unless ANSI-92 mode is enabled. If searches return no rows, test the SQL in your DB tool, verify the connection string, and add an index on the searched column if performance is poor.

Recommended Answers

All 10 Replies

This is pretty easy to do. You can tie the DataGrid to the filter function and pass textbox as a paramter to filter records on the Grid

just paste this code:::
Protected Sub btnchk_name_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnchk_name.Click
Dim str As String = "select ID from table1 where ID = '" & txtid.Text & "'"
Dim con As String = ConfigurationManager.AppSettings("preeconn")
Dim com As New SqlCommand(str, New SqlConnection(con))
com.Connection.Open()
Dim da As New SqlDataAdapter(str, con)
Dim dr As SqlDataReader
dr = com.ExecuteReader 'where sqldatareader ain't run without using
If Not dr.Read() Then
datagrid.datasource=com.executereader()
datagrid.databind()
End If
End Sub
-----------

is this code applicable to OleDb? because i've tried it Substituting with OleDb, but it seems not to work. here is my code including myDataConnectio. plz help agentely im in a tight position

Protected Sub txtSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.Click
        Dim sql As String = "SELECT * FROM new_bug WHERE Status='" & txtStatus2.Text & "'"
        Dim cmnd As New OleDbCommand(sql, CreateOleDbConnection)
        Dim da As New OleDbDataAdapter(sql, CreateOleDbConnection)
        Dim dr As OleDbDataReader = cmnd.ExecuteReader
        If Not dr.Read() Then
            GridBugList.DataSource = cmnd.ExecuteReader()
            GridBugList.DataBind()
        End If
    End Sub

    Public Function CreateOleDbConnection() As OleDbConnection
        Dim myConnString As String = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/bug_base.mdb")
        Dim myConnection As New OleDbConnection(myConnString)
        myConnection.Open()
        Return myConnection
    End Function

hi, can you help me for our search button code. How can we tie the DataGrid to the filter function and pass textbox as a paramter to filter records on the Grid so that what we are searching will display on the data grid? Please help, we have to pass this system urgent..
Thanks and more power....

hi can anyone help me . when the user enter the name in a textbox and click the search button, the output should be display in the datagrid... Help plz have to submit my project in 8 days

Hi there
First of all you need to connect your data base to your page
Public Function CreateOleDbConnection() As OleDbConnection
Dim myConnString As String = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/Training_db.mdb")
Dim myConnection As New OleDbConnection(myConnString)
myConnection.Open()
Return myConnection
End Function

Second assign you database called values to you datagrid
Public Sub SearchCourse(ByVal CourseID As String)
Try
Dim sql As String = "SELECT CourseID, CourseName, Date_From,Date_To, SeatsAvailible FROM CourseInfo WHERE CourseName='" & coursename & "'"
Dim cmnd As New OleDbCommand(sql, CreateOleDbConnection)
Dim dataset As New DataSet()
Dim adapter As New OleDbDataAdapter(cmnd)
adapter.Fill(dataset)
adapter.Dispose()
'binding you datagrid with data from you database that which you have called
GridTrainingCourse.DataSource = dataset.Tables(0)
GridTrainingCourse.DataBind()
cmnd.Dispose()
CreateOleDbConnection.Close()
Catch ex As Exception
lblNoRecords.Text = "No records found, please redefine your search"
End Try
End Sub

Third call the function/sub to clicking event of you button.

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
'search value
Dim CourseID as string= txtSearch.Text
SearchCourse(CourseID)
End Sub

i need to know code in select(search),insert,update and delete to sql 2005 data base.plzzz help me.

You may also want to see this:

Protected Sub txtSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearch.Click
Dim sql As String = "SELECT * FROM new_bug WHERE Status='" & txtStatus2.Text & "'"
Dim cmnd As New OleDbCommand(sql, CreateOleDbConnection)
Dim da As New OleDbDataAdapter(sql, CreateOleDbConnection)
Dim dr As OleDbDataReader = cmnd.ExecuteReader
If Not dr.Read() Then
GridBugList.DataSource = cmnd.ExecuteReader()
GridBugList.DataBind()
End If
End Sub

Public Function CreateOleDbConnection() As OleDbConnection
Dim myConnString As String = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + Server.MapPath("~/App_Data/bug_base.mdb")
Dim myConnection As New OleDbConnection(myConnString)
myConnection.Open()
Return myConnection
End Function

in select(search

sCHOOL

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.