I am trying to determine the next available order id using the method below. It works provided the table has a record in it. If it doesn't I get the error "Input string was not in a correct format." I am certain that it is because the query is returning a value of NULL. How can get around that or check for the NULL value?
' Establish data connection...
Dim sqlConnAsNew SqlConnection(ConfigurationSettings.AppSettings("connectionstring"))
'Determine order id number...
Dim order_idAsInteger
Dim strSQLAsString
strSQL = "Select MAX(order_id) from mkt_order"
Dim sqlCmdAsNew SqlCommand(strSQL, sqlConn)
Dim sqlDAAsNew SqlDataAdapter(sqlCmd)
Dim sqlDSAsNew DataSet
sqlDA.Fill(sqlDS, "item")
If sqlDS.Tables(0).Rows.Count <> 0Then
order_id = Convert.ToInt32(sqlDS.Tables(0).Rows(0)(0).ToString()) + 1
Else
order_id = 1
EndIf
This is because a DBNull will be returned if you use MAX() function and there is no (qualified) record in the table. Let's try TOP...ORDER BY... to avoid using MAX():
strSQL = "Select TOP 1 order_id from mkt_order ORDER BY order_id DESC"
No comments:
Post a Comment