John Paul Cirilos
John Paul Cirilos

Reputation: 137

Mysql command in asp.net c#

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

Upvotes: 2

Views: 70

Answers (1)

juharr
juharr

Reputation: 32266

You can test for null in your query

select COALESCE(MAX(jobId), 0) + 1 FROM joborder 

Upvotes: 1

Related Questions