hi,

Private Sub AutoNumberNo()
try
Dim strsql As String
strsql = "SELECT MAX(Serial_No)+1 FROM Cutting"
Dim com As New SqlCommand(strsql, objconnection)
txtsrno.Text = com.ExecuteScalar() ' result will appear in textbox txtsrno
Catch sqlex As SqlException
MessageBox.Show(sqlex.Message)
End Try
End sub


i try this code but i get following error
1) casting from DBnull to string not possible , when database is empty.
2) serial number increasing from zero, but i want serial number from one
3) serial number not shown in text box.

i am waiting for ur reply........................

prasad satam

mumbai

First of all you put your code into a code block so that we can read it easier, like so:

Private Sub AutoNumberNo()
    try
        Dim strsql As String
        strsql = "SELECT MAX(Serial_No)+1 FROM Cutting"
        Dim com As New SqlCommand(strsql, objconnection)
        txtsrno.Text = com.ExecuteScalar() ' result will appear in textbox txtsrno
    Catch sqlex As SqlException
        MessageBox.Show(sqlex.Message)
    End Try
End sub

Part of your problem is that you don't allow for a null return, so allow for one:

Private Sub AutoNumberNo()
    try
        Dim strsql As String
        strsql = "SELECT IsNull(MAX(Serial_No),0)+1 FROM Cutting"
        Dim com As New SqlCommand(strsql, objconnection)
        txtsrno.Text = com.ExecuteScalar() ' result will appear in textbox txtsrno
    Catch sqlex As SqlException
        MessageBox.Show(sqlex.Message)
    End Try
End sub
This article has been dead for over six months. Start a new discussion instead.