hello this code for add the auto number form the access table to the form vb but But it only works when the table contains data but when the table is empty does not work and the bill number appears a blank
Sub New()

InitializeComponent()
Try
    Dim BillNo As New DataTable
    BillNo = New DAL().selectdatatable(" Select * FROM client ")
    For i As Integer = 0 To BillNo.Rows.Count
        txtBillNo.Text = BillNo.Rows(i)(0).ToString + 1
    Next
Catch ex As Exception            
End Try

Recommended Answers

All 7 Replies

Of course it won't work. An empty table will return an empty result set and so your line
txtBillNo.Text = BillNo.Rows(i)(0).ToString + 1
can't supply a value because BillNo.Rows(i)(0) doesn't exist.
If no results are returned just set the txtBillNo to whatever the first number should be. e.g. 1.

Thank you, but I'm still Beginner So please give example of what you want

any help

You haven't explained exactly what this bill number is but, assuming you've got your table starting at 1 for the first entry, if the returned rows is zero (no records in the table) you know your first bill number is 1. Other increment by 1 as you are doing.

Dim BillNo As New DataTable BillNo = New DAL().selectdatatable(" Select * FROM client ") If (BillNo.Rows.Count < 1) txtBillNo.Text = "1" // or whatever is relevant Else For i As Integer = 0 To BillNo.Rows.Count
        txtBillNo.Text = BillNo.Rows(i)(0).ToString + 1 Next End IF 

My VB syntax is rusty so my If loop maybe incorrect.

commented: Loop should be [ For i As Integer = 0 To (BillNo.Rows.Count-1) ] +7

the bill no is a primary key all i want to dispaly the new number of this primary key in the form
when i make a new bill
Sub New()

 InitializeComponent() Try
        Dim BillNo As New DataTable
        BillNo = New DAL().selectdatatable(" Select * FROM bak_buy1 ")
        If (BillNo.Rows.Count < 1) Then
        txtBillNo.Text = "1"
        Else
        For i As Integer = 0 To BillNo.Rows.Count
        txtBillNo.Text = BillNo.Rows(i)(0).ToString + 1 
        Next
        End If Catch ex As Exception
        ' MsgBox(ex.Message) End Try this code work but when i delete the last bill say no 5 and open the from to make a new bill the number in the form is 5 but the number in the table is 6 and he can not save the bill into the table 

Thank you all for your cooperation
My mistake was I when using the insert into phrase did not put billno field where the fields are to be positioned so that the table was forced to accept the number on the form
Special thanks to Mr. hericles

'Set the Identity_Insert to ON first before attempting to insert new row. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
        cmd.Connection = cnn 
        cmd.CommandText = "Set Identity_Insert Tmp_Agents ON"
        cmd.ExecuteNonQuery() 
        cmd.CommandText = "INSERT INTO Tmp_Agents (AgentIdent, AgentCode) " &
        " Values (5,'JMB')"
        cmd.ExecuteNonQuery() End Sub 
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.