below is my code, i want to get a record base on a specif invoice number,please help me out cos this thing has giving me sleepless nite.thanks in advance.

Imports System.Data.Sql
Imports System.Data.SqlClient
Public Class Invoice

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

Call connectDatabase()
Call getLastID()
Call getTotals()
Call loadOrder()
Me.DataGridView1.Refresh()
If txtPerson.Text = "" Then
btnAdd.Enabled = False
btnDelete.Enabled = False
Button1.Enabled = False
Else
btnAdd.Enabled = True
btnDelete.Enabled = True
Button1.Enabled = True
End If

txtPerson.Enabled = False
txtAdd.Enabled = False
txtPerson.Enabled = False
MaskedTextBox1.Enabled = False
End Sub

Private Sub getTotals()
Try
Dim I As Integer
For I = 0 To DataGridView1.RowCount - 1
txtSubTotal.Text = Val(Format(txtSubTotal.Text, "General Number")) + Val(DataGridView1.Rows.Item(I).Cells(4).Value)
txtSubTotal.Text = Format(txtSubTotal.Text, "Standard")
Next
Catch
End Try
End Sub

Private Sub getLastID()
Dim oleDBDR As SqlDataReader
Dim oleDBCommand As New SqlCommand

With oleDBCommand
.Connection = con
.CommandText = "SELECT * FROM tblAddOrder ORDER BY InvoiceNo DESC"
End With
oleDBDR = oleDBCommand.ExecuteReader
If oleDBDR.Read Then
lblOrNumber.Text = Val(oleDBDR.Item(0)) + 1
End If
End Sub

Private Sub loadOrder()
Dim oleDBC As New SqlCommand
Dim oleDBDR As SqlDataReader
Dim c As Integer
c = 0
With oleDBC
.Connection = con
.CommandText = "SELECT * FROM tblAddOrder Where InvoiceNo='" & lblOrNumber.Text & "'"
End With
oleDBDR = oleDBC.ExecuteReader

If oleDBDR.HasRows Then

While oleDBDR.Read
DataGridView1.Rows.Add()
DataGridView1.Item(0, c).Value = oleDBDR.Item(3)
DataGridView1.Item(1, c).Value = oleDBDR.Item(4)
DataGridView1.Item(2, c).Value = oleDBDR.Item(5)
DataGridView1.Item(3, c).Value = oleDBDR.Item(6)
DataGridView1.Item(4, c).Value = oleDBDR.Item(7)
c = c + 1

End While

oleDBDR.Close()
Else
MsgBox("No Record to Display!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "WARNING")
End If
End Sub

Just some hints.

On the getLastId sub, you miss to close the datareader. It is required for the loadOrder to work.
Maybe, the getTotals sub must be called after the loadOrder to have all the info.

If you need to load the info from only one order number, i will clear the grid view between

If oleDBDR.HasRows Then
DataGridView1.RowCount = 0
While oleDBDR.Read

Hope this helps

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.