VB.NET - How To Search Data In SqlServer DataBase Using Visual Basic .Net
In This VB.NET Tutorial We Will See How To Search / Find Records In Microsoft SQL DataBase And Display It In TextBoxes Using Visual Basic .NET Programming Language.
Project Source Code:
Imports System.Data.SqlClient
Public Class VBNET_SQL_SEARCH
Private Sub BTN_SEARCH_Click(sender As Object, e As EventArgs) Handles BTN_SEARCH.Click
Dim connection As New SqlConnection("Server= SAMSNG-PC; Database = TestDB; Integrated Security = true")
Dim command As New SqlCommand("select * from Users where Id = @id", connection)
command.Parameters.Add("@id", SqlDbType.Int).Value = TextBoxID.Text
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
TextBoxFN.Text = ""
TextBoxLN.Text = ""
TextBoxAGE.Text = ""
If table.Rows.Count() > 0 Then
' return only 1 row
TextBoxFN.Text = table.Rows(0)(1).ToString()
TextBoxLN.Text = table.Rows(0)(2).ToString()
TextBoxAGE.Text = table.Rows(0)(3).ToString()
Else
MessageBox.Show("NO Data Found")
End If
End Sub
End Class
///////////////OUTPUT:
Download Projects Source Code
1 comments:
commentsI have used the code above
ReplyHowever i only get 1 result the first row
when i type in a different code in search box i get result of first row
please find my modified code
i am new to programming appologies in advance
Imports MySql.Data.MySqlClient
Public Class Insert_Update_Delete_Search
Dim connection As New MySqlConnection("datasource=localhost;port=3306;username=root;password=;database=")
' button find
Private Sub ButtonSearch_Click(sender As Object, e As EventArgs) Handles ButtonSearch.Click
Dim search_command As New MySqlCommand("SELECT * FROM `products` WHERE `CODE` = "CODE", connection)
search_command.Parameters.Add("CODE", MySqlDbType.Int64).Value = TextBox1.Text
Dim adapter As New MySqlDataAdapter(search_command)
Dim table As New DataTable()
Try
adapter.Fill(table)
TextBox2.Text = table(0)(3)
Else
TextBox2.Text = ""
MessageBox.Show("No Data Found")
End If
Catch ex As Exception
MessageBox.Show("ERROR")
End Try
End Sub