sql - Return maximum ID number from a database -


this easy thing do, i'm doing first vb project i'm not sure how 100%, apologies in advanced if following problem simple.

basically, need is, when loading database table ultragrid, need retrieve maximum integer stored in field.

to explain more clearly, each record in database has it's own id number, need iterate each record , find 1 highest id number, , return id, can used in other calculations.

i know can use sql = select max(supportid) tblincidents example retrieve highest id number stored in table.

so, how go declaring result of (so, highest id number) variable can firstly display in messagebox prove me query has worked, , secondly can use variable means of using id throughout code?

an example; code save new record tblincidents table.

 private sub btnsave_click(sender object, e eventargs) handles btnsave.click      dim incidentsolved boolean = false     if cboxsolved.checked         incidentsolved = true     end if      if txtclientsave.text = ""         msgbox("client name cannot blank")      elseif rtbproblem.text = ""         msgbox("problem cannot blank")      elseif cboxsolved.checked = true , rtbsolution.text = ""         msgbox("please enter solution")      else         database.savenewincident(txtclientsave.text, dtpstart.value, dtpend.value, rtbproblem.text, dtpstarttime.value, dtpendtime.value, cboxsolved.checked, rtbsolution.text, _con)          txtclientsave.text = ""         rtbproblem.text = ""         rtbsolution.text = ""         dtpstart.value = date.today         dtpend.value = date.today         dtpstarttime.value = datetime.now         dtpendtime.value = datetime.now         cboxsolved.checked = false      end if end sub 

database function called

public shared function savenewincident(byval clientname string, datestart date, dateend date, byval incidentproblem string, byval timestart string, byval timeend string,                                        byval incidentsolved boolean, byval incidentsolution string, _con oledbconnection)      dim tr oledbtransaction = nothing      try         tr = _con.begintransaction()          dim dc new oledbcommand         dc.connection = _con          dc.commandtype = commandtype.text         dc.commandtext = "insert dbo.tblincidents values(?, ?, ?, ?, ?, ?, ?, ?, ?)"         dc.transaction = tr         dc.parameters.add("@clientname", oledbtype.varchar).value = clientname         dc.parameters.add("@datestart", oledbtype.date).value = datestart         dc.parameters.add("@dateend", oledbtype.date).value = dateend         dc.parameters.add("@incidentproblem", oledbtype.longvarchar).value = incidentproblem         dc.parameters.add("@timestart", oledbtype.varchar).value = timestart         dc.parameters.add("@timeend", oledbtype.varchar).value = timeend         dc.parameters.add("@incidentsolved", oledbtype.boolean).value = incidentsolved         dc.parameters.add("@incidentsolution", oledbtype.longvarchar).value = incidentsolution          dc.executenonquery()          tr.commit()          msgbox("save successful")      catch ex exception          mdinit.errorlog(ex.message, ex.stacktrace)         msgbox("failed save data, refer error log")         tr.rollback()      end try  end function 

what can in vb.net define sqlcommand require sql connection attached - since query returns single value able execute executescalar return single value eg

dim cmd new sqlclient.sqlcommand("select max(billid) tblrecurring",connectionstring)  

then

cmd.connection.open  

and

dim x maxid = cmd.executescalar  

and finish off

cmd.connection.dispose  

of course


Comments