25-07-24, 11:20 AM
جرب هذه الطريقة
PHP كود :
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim connectionString As String = "connection"
Using connection As New SqlConnection(connectionString)
Dim query As String = "SELECT TOP 10 * FROM Invoices WHERE 1=1"
If Not String.IsNullOrEmpty(txtDate.Text) Then
query &= " AND InvoiceDate = @InvoiceDate"
End If
If Not String.IsNullOrEmpty(txtInvoiceNumber.Text) Then
query &= " AND InvoiceNumber = @InvoiceNumber"
End If
If Not String.IsNullOrEmpty(txtCustomerName.Text) Then
query &= " AND CustomerName LIKE @CustomerName"
End If
query &= " ORDER BY CreatedDate DESC"
Using command As New SqlCommand(query, connection)
If Not String.IsNullOrEmpty(txtDate.Text) Then
command.Parameters.AddWithValue("@InvoiceDate", txtDate.Text)
End If
If Not String.IsNullOrEmpty(txtInvoiceNumber.Text) Then
command.Parameters.AddWithValue("@InvoiceNumber", txtInvoiceNumber.Text)
End If
If Not String.IsNullOrEmpty(txtCustomerName.Text) Then
command.Parameters.AddWithValue("@CustomerName", "%" & txtCustomerName.Text & "%")
End If
Dim adapter As New SqlDataAdapter(command)
Dim table As New DataTable()
adapter.Fill(table)
dataGridViewResults.DataSource = table
End Using
End Using
End Sub

