Basically, I am wanting to get the last userID which is submitted when the user enters their details and clicks the submit button. I have searched the internet and have made changes but still don't understand where i'm going wrong. I used the MAX statement to show the last user ID in the textbox from the user table.my code is shown below:

Protected Sub txtuserID_TextChanged(sender As Object, e As System.EventArgs) Handles txtuserID.TextChanged

    Dim strConnection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\LostPropertyProject\App_Data\LostPropertyDatabase.mdf;Integrated Security=True;User Instance=True"

Dim strConnection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\LostPropertyProject\App_Data\LostPropertyDatabase.mdf;Integrated Security=True;User Instance=True"
'Establish SQL Connection
Dim con As New SqlConnection(strConnection)
'Open database connection to connect to SQL Server
con.Open()
'Data table is used to bind the resultant data
Dim dtusers As New DataTable()
'Create a new data adapter based on the specified query.
Dim comm As New SqlCommand
comm.CommandText = "SELECT MAX(UserID) FROM tblUser"
comm.Connection = con

Dim MaxUserID As Object = comm.ExecuteScalar()
con.Close()


End Sub

Also, I want the userID to be displayed in the textbox as soon as the page loads. How do I go about doing that?

Would appreciate help. Thank you!

So exactly what error message are you getting when trying to retreive this info from the DB? I assume that the user ID field is a number?

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.