Hi every1 i have a form with a textboxid, buttonAdd, ButtonSave on it , when i click on the btnAdd it must show the id and when i click update it must really Save . I want to auto generate the id through codes ... can any1 help...
I don't want to do it through access.. am using Visual Basic 2005

Note: i have a table with only one column that is the ID column..

Do you want to generate sequential IDs--1,2,3, etc.

Yes dear... Can u plz help me

see this following code :

Private Sub AutoNumberNo()
        Dim myReader As SqlDataReader
        conn = GetConnect()
        conn.Open()
        Dim temp As String
        Try
            Dim sql As String = "SELECT MAX(NO) 'IDNumber' FROM Student "
            Dim comm As SqlCommand = New SqlCommand(sql, conn)
            myReader = comm.ExecuteReader
            If myReader.HasRows Then
                While myReader.Read()
                    temp = myReader.Item("IDNumber") + 1
                End While
            End If
            myReader.Close()
        Catch ex As Exception

        End Try
        conn.Close()
        txtId.Text = String.Concat(temp) ' result will appear in textbox txtId
    End Sub

call that procedure in button click event.
on button click event...
AutoNumberNo()
end sub

Do you want to generate sequential IDs--1,2,3, etc.

yes

see this following code :

Private Sub AutoNumberNo()
        Dim myReader As SqlDataReader
        conn = GetConnect()
        conn.Open()
        Dim temp As String
        Try
            Dim sql As String = "SELECT MAX(NO) 'IDNumber' FROM Student "
            Dim comm As SqlCommand = New SqlCommand(sql, conn)
            myReader = comm.ExecuteReader
            If myReader.HasRows Then
                While myReader.Read()
                    temp = myReader.Item("IDNumber") + 1
                End While
            End If
            myReader.Close()
        Catch ex As Exception

        End Try
        conn.Close()
        txtId.Text = String.Concat(temp) ' result will appear in textbox txtId
    End Sub

call that procedure in button click event.
on button click event...
AutoNumberNo()
end sub

Thanks for code
but i get error when my database field is blank.plz help

Add this line after End While--
Else
temp = 1

but i get error when my database field is blank.plz help

hi,
the problem is occur when database is empty.plz mail me proper code

try this line before myreader.hasrows --

myReader.Read ()

and the Else statement as suggested before.

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 error when database is empty.

prasad satam

mumbai

hi,
i also want to display this id in textbox

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

u didn't try my code?

hi,
i get error when my database is empty.So, plz help me

1.My code working , but i want to autogenerate number which is also display in textbox & then save in database. Number also increase when it store.
2. When my database field is empty that time query cannot retrive max(Serial_No) & i get error as follow:
Cast from "DBNull" To "String" is Not Valid
Please help me for this error.

strGenerate = CreatestrGenerate 'Auto generate

strGenerate //' goes where ever you want to put..

Function CreatePaymentKey
Randomize
strCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
intUpperLimit = 36
intLowerLimit = 1

For S = 1 To 12 //' change this to the number of code you want
strTempKey = Mid(strCharacters, Int((intUpperLimit - intLowerLimit) * Rnd() + intLowerLimit), 1)
CreatestrGenerate= CreatestrGenerate & strTempKey
Next
End Function

Good luck..

Hi every1 i have a form with a textboxid, buttonAdd, ButtonSave on it , when i click on the btnAdd it must show the id and when i click update it must really Save . I want to auto generate the id through codes ... can any1 help...
I don't want to do it through access.. am using Visual Basic 2005

Note: i have a table with only one column that is the ID column..

Want to ask what if the id automatically produce but taking the final value has been input and reduced if the data has been deleted, I am using Visual Basic 2005 with SQL database server 2005 with ODBC connections ... Thank you

hey, i've made a textbox and want the asp.net to search max id from database(basically from sql2005) and generate the max id +1 into the textbox .PLEASE HELP.

I can do the first thing by simply typing
convert.tostring(1);
but how to increase that one to two,three and further is wat i wanna ask!

please help with this problem

I can do the first thing by simply typing
convert.tostring(1);
but how to increase that one to two,three and further is wat i wanna ask!

you can email me:-dipsy_sharma89@yahoo.co.in

There's a much easier way to do that.

Save the last id number in a textfile. When you click the btnAdd the code should read the textfile and add one to the value in the textfile then show it in the textbox. When you click btnSave the last id is saved in the database and in the textfile.

[U]btnAdd_Click[/U]
'Reads from text file
Dim TextReader As New System.IO.StreamReader(TextFilePath)
Dim NewID As Integer
NewID = Val(TextReader.ReadToEnd) + 1
txt.Text = NewID
TextReader.Close()

[U]btnSave_Click[/U]

Dim nr As DataRow = ds.Tables(Table).NewRow
Dim cb As New oledb.oledbCommandBuilder(da)

'Reads from textfile
Dim TextReader As New System.IO.StreamReader(TextFilePath)
Dim NewID As Integer
NewID = Val(TextReader.ReadToEnd) + 1
TextReader.Close()

'Saves to textfile
Dim TextWriter As New System.IO.StreamWriter(TextFilePath)
TextWriter.Write(NewID)
TextWriter.Close()
 
'Saves to database
nr(0) = NewID
da.Update(ds,"Table")

Hi every1 i have a form with a textboxid, buttonAdd, ButtonSave on it , when i click on the btnAdd it must show the id and when i click update it must really Save . I want to auto generate the id through codes ... can any1 help...
I don't want to do it through access.. am using Visual Basic 2005

Note: i have a table with only one column that is the ID column..

This is the simplest way to duo it.Copy and paste on your page load event.

string sqlText = "SELECT MAX(product_id) FROM Product ";
        SqlConnection con3 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\QXScustomerportalDB.mdf;Integrated Security=True;User Instance=True");
        SqlCommand command = new SqlCommand(sqlText, con3);

    try
    {
        con3.Open();
        string pcount = Convert.ToString( command.ExecuteScalar());
        if (pcount.Length == 0)
        {
            TextBox1.Text = "1";

        }
        else 
        {
            int pcount1 = Convert.ToInt32(pcount);
            int pcountAdd = pcount1 + 1;
            TextBox1.Text = pcountAdd.ToString();
        }

    }
    catch (Exception ex)
    {
        Alert.Show(ex.Message);
    }
    finally
    {
        con3.Close();
    }

hey, i've made a textbox and want the asp.net to search max id from database(basically from sql2005) and generate the max id +1 into the textbox .

'Jx Man' already gave showed you that in his code as written below

Try
Dim sql As String = "SELECT MAX(NO) 'IDNumber' FROM Student "
Dim comm As SqlCommand = New SqlCommand(sql, conn)
myReader = comm.ExecuteReader
If myReader.HasRows Then
While myReader.Read()
temp = myReader.Item("IDNumber") + 1
End While
End If
myReader.Close()
Catch ex As Exception
 
End Try
conn.Close()
txtId.Text = String.Concat(temp) ' result will appear in textbox txtId

I want to generate serial number (i.e,generated by system only ) but it is sequence one

@rajeshmca03 Do you mean a Global Unique Identifier? If so, you may get a new Guid and just append a counter, for example like this:

Module Module1

    Sub Main()
        Try
            Dim sGuid As String = String.Empty
            Dim nOption As Int32 = 0
            Dim sequenceCount As Int32 = 0
            Dim curGlobalIdent As String = ""
            Dim formatLn As Int32 = 6
            Do
                Console.WriteLine("1. Get a new Guid.")
                Console.WriteLine("2. Increment by one.")
                Console.WriteLine("3. Exit.")
                Console.WriteLine("Enter the desired option number and press 'Enter':")
                Dim e1 As String = Console.ReadLine
                If Int32.TryParse(e1, nOption) Then
                    Select Case nOption
                        Case 0, 3 : Exit Do
                        Case 1
                            Dim g As New Guid
                            sGuid = g.ToString
                            curGlobalIdent = sGuid + "-" + StrDup(formatLn - 1, "0") + "1"
                            sequenceCount = 1
                            Console.WriteLine(curGlobalIdent)
                        Case 2
                            sequenceCount += 1
                            Dim sHex As String = Hex(sequenceCount)
                            If Len(sHex) < formatLn Then
                                sHex = StrDup(formatLn - Len(sHex), "0") + sHex
                            End If
                            curGlobalIdent = sGuid + "-" + sHex
                            Console.WriteLine(curGlobalIdent)
                    End Select
                End If
            Loop
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
            Console.WriteLine("Press 'Enter' to exit")
            Console.ReadLine()
        End Try

    End Sub
End Module

How to make unique customer id using name in the textbox like Rahul Balasaheb Dube so the code RBD + 001.... use single textbox or all prefix word in vb.net with msaccess

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.