Searching Data Based on Date Using VB.Net and SQL Server 2018
Submitted by janobe on Wednesday, October 30, 2019 - 16:00.
In this tutorial, I will teach you how to search data based on the date using vb.net and SQL server 2018. This method has the ability to find the data in the datagridview based on the selected date in the DateTimePicker. This will help you find the specific transaction within the selected date that you choose in the DateTimePicker.
Output
The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Database
1. Install the MSSMS 2018 on your machine. 2. Open the MSSMS 2018 . After that, right click the database, the select “New Database” and name it “dbpos” 3. Do the following query to create a table in the database that you have created- USE [dbpos]
- GO
- /****** Object: Table [dbo].[tbltransaction] Script Date: 28/10/2019 2:05:52 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[tbltransaction](
- [TransactionId] [INT] IDENTITY(1,1) NOT NULL,
- [InvoiceNo] [INT] NULL,
- [Barcode] [VARCHAR](MAX) NULL,
- [TransactionDate] [datetime] NULL,
- [Price] [money] NULL,
- [TransVat] [nvarchar](50) NULL,
- [TransDiscount] [money] NULL,
- [TransactionQty] [INT] NULL,
- [SubTotal] [money] NULL,
- [UserId] [INT] NULL,
- [OrderId] [INT] NULL,
- CONSTRAINT [PK_tbltransaction] PRIMARY KEY CLUSTERED
- (
- [TransactionId] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
- GO
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for visual basic.Step 2
Add a DateTimePicker, and a DataGridview inside the Form. Then do the Form just like shown below.Step 3
Press F7 to open the code editor. In the code editor, add a namespace to accessSQL Server
libraries.
- Imports System.Data.SqlClient
Step 4
Create a connection between Visual Basic 2015 and SQL Server database. After that, declare and initialize all the classes and variables that are needed.- Dim con As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;Database=dbpos;trusted_connection=true;")
- Dim cmd As SqlCommand
- Dim da As SqlDataAdapter
- Dim dt As DataTable
- Dim sql As String
Step 5
Create a method for retrieving the data in the database.- Private Sub load_data(sql As String, dtg As DataGridView)
- Try
- con.Open()
- cmd = New SqlCommand
- da = New SqlDataAdapter
- dt = New DataTable
- With cmd
- .Connection = con
- .CommandText = sql
- End With
- With da
- .SelectCommand = cmd
- .Fill(dt)
- End With
- dtg.DataSource = dt
- Catch ex As Exception
- MsgBox(ex.Message)
- Finally
- con.Close()
- da.Dispose()
- End Try
- End Sub
Step 6
Go back to the design view, double click the DateTimePicker to open theValueChanged event
handler on it. After that, add this code inside the “DateTimePicker1_ValueChanged” event to find the specific transaction in the database.
- Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker1.ValueChanged
- Dim d As String = Format(DateTimePicker1.Value, "MM/d/yyyy")
- sql = "SELECT * FROM tbltransaction WHERE FORMAT (TransactionDate, 'd', 'en-US') = '" & d & "'"
- load_data(sql, DataGridView1)
- End Sub
Step 7
Write the following codes for retrieving data in the database in the first load of the form.- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- sql = "SELECT * FROM tbltransaction"
- load_data(sql, DataGridView1)
- End Sub
Add new comment
- 2220 views