2

I have this code on my page

private object getJobID()
        {
            try
            {
                conn.Open();

                String latestJobID = @"SELECT MAX(jobId) + 1 FROM joborder";

                MySqlCommand cmd = new MySqlCommand(latestJobID, conn);
                MySqlDataReader DR = cmd.ExecuteReader();

                while (DR.Read())
                {
                    return DR[0].ToString();
                }
            }
            catch (MySqlException ex)
            {
                System.Diagnostics.Debug.WriteLine("ERROR: " + ex.ToString());
            }
            finally
            {
                conn.Close();
            }
            return "";
        }

the @"SELECT MAX(jobId) + 1 FROM joborder" do not add + 1 if my table is empty, how can i Add + 1 to my jobId if my table is null, thanks

1 Answer 1

1

You can test for null in your query

select COALESCE(MAX(jobId), 0) + 1 FROM joborder 
Sign up to request clarification or add additional context in comments.

2 Comments

the query is invalid it directs to catch
@JohnPaulCirilos Sorry, didn't notice the MySQL tag. In that case use COALESCE instead of ISNULL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.