Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here in form load event i m trying to take value form sql server database ID column and want to
display max(ID)+1 in textbox control

here in visual studio when i start debug its start compiling without any error but after few seconds it automatically stop debugging
help me whats the issue in my code


Public Class FrmAdddonor
Dim con As New SqlConnection(Data Source=Tarun-PC;Initial Catalog=bdsdb;Integrated Security=True)
Private Sub FrmAdddonor_Load(sender As Object, e As EventArgs) Handles MyBase.Load

txtdonorID.Enabled = False
Dim number As Integer
con.Open()
Dim Str As String =select max(ID)+1 from tbldonor;
Dim com As New SqlCommand(Str, con)
Dim reader As SqlDataReader = com.ExecuteReader()
If reader.Read() Then
number = reader("ID").ToString()
txtdonorID.Text = number
reader.Close()
con.Close()
End If

End Sub
Posted

You have some errors in your code:
-the select statement should be: "select max(ID) as MAX from tbldonor"
-then Dim temp as integer; temp = DirectCast(reader("MAX"), Integer) + 1;
-end finally: txtdonorID.Text = temp.ToString();
 
Share this answer
 
v2
VB
Public Function maxuse() as integer
        con.Open()
        sql = "select IIF(ISNULL(MAX(sms_count)), 0, MAX(sms_count))  from sms_master"
        Com = New OleDb.OleDbCommand(sql, con)
        maxuse = Com.ExecuteScalar()
        'If _maxno = "&H0" Then
        '    _maxno = 0
        'End If
        Com.ExecuteNonQuery()
        Com.Dispose()
        con.Close()
    End Sub</pre>


VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       textbox1.text = maxuse() + 1
   End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900