Internet is growing Every Seconds :: Industry is growing Every Year

Saturday, June 19, 2004

Dot net : get_max_id

Function get_max_id :

This method is used to get the maximum id existing in the table.
It accepts 2 arguments, namely tbl_name and fld_name
Tbl_Name – Name of the Table.
Fld_Name – Name of the Id Field
It returns the Maximum Id as Integer.


Public Function get_max_id(ByVal tbl_name As String, ByVal fld_name As String) As Integer
Dim rd As Object
str_qry = "select max(" & fld_name & ") as Cnt from " & tbl_name
cmd = New SqlCommand(str_qry, con)
Try
con.Open()
rd = cmd.ExecuteScalar()
If rd.GetType Is GetType(DBNull) Then
result = 1
Else
result = CInt(rd) + 1
End If
Return result
Catch ex As Exception
MsgBox(ex.Message.ToString)
Finally
con.Close()
End Try
End Function