How do you grab the Max Value?

Thedragon

New Member
Ok I need to grab the max value in a database. I know the SQL for this but how do I assign that to a variable in ASP.NET? Here is what I am trying:<BR><BR>Dim strGetMax as String = "SELECT MAX(id_value) FROM table"<BR>Dim objCmdGetMax as New SQLCommand(strGetMax, ObjConn)<BR>Dim strMaxValue as Object = objCmdGetMax.ExecuteReader<BR>response.write (strMaxValue)<BR><BR>But I'm getting this output:<BR>System.Data.SqlClient.SqlDataReaderreplace this line:<BR><BR>Dim strMaxValue as Object = objCmdGetMax.ExecuteReader<BR><BR>with this:<BR><BR>Dim strMaxValRD AS SqlDataReader = objCmdGetMax.ExecuteReader<BR>strMaxValRD.Read()<BR>Dim strMaxValue AS integer(or string) = strMaxValRD(0)<BR>strMaxValRD .Close()<BR><BR>That should do it. You have to create and execute the reader before you can grab values out of it. Then, you have to .Read() the contents. <BR><BR>])ryThanks, I also just found another way to do it<BR><BR>Dim strGetMax as String="SELECT MAX(id) FROM table"<BR>Dim objCmdGetMax as New SQLCommand(strGetMax, ObjConn)<BR>Dim strMaxValue as Object = objCmdGetMax.ExecuteScalar<BR>response.write (strMaxValue)
 
Back
Top