0

I have a function here that gets the last value of an order_id and increments it by 1 if it's not already 1. Problem is, the initial value is null and I couldn't get it to add by 1.

Code:

private void getOrderId()
    {
        var orders_dt = conn.Select("orders", "MAX(order_id)").GetQueryData();

        if (orders_dt == null || orders_dt.Rows.Count == 0 || orders_dt.Rows[0][0] == null)
        {
            //Should not preemptively insert into the database
            order_no.Text = "1";
        }
        else
        {
            int order_id = orders_dt.Rows[0][0] + 1; //ERROR HERE
            order_no.Text = order_id.ToString();
        }
    }

An error occurs on int order_id = orders_dt.Rows[0][0] + 1; Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Operator '+' cannot be applied to operands of type 'System.DBNull' and 'int''

Any ideas?

Edit: I've already confirmed the query in MySql, it returns null.

Jammeh
  • 63
  • 2
  • 16
  • Exactly the error message. When it is DBNull you cannot treat it as an int. Assign a value when it is DBNull. – PepitoSh Jun 25 '18 at 02:16
  • I cannot preemptively assign 1 as the value, the only thing I could think of here is assigning 0 as the first initial value... I'm not sure if that's correct? – Jammeh Jun 25 '18 at 02:18
  • You need to check `DBNull.Equals(orders_dt.Rows[0][0])` as DBNull != null. – ProgrammingLlama Jun 25 '18 at 02:20
  • In general, incrementing a variable is an assignment. – PepitoSh Jun 25 '18 at 02:20
  • 1
    Please note, your code pattern is prone to an error in a concurrent environment. You may query the same max id value and increment in separate threads just to realize it when you save the second one. Unique ID generation should be left to the database engine, or you have to have a stronger facility to implement the same. – PepitoSh Jun 25 '18 at 02:32
  • https://stackoverflow.com/a/40477560/3110834 – Reza Aghaei Jun 25 '18 at 08:49

1 Answers1

2

Your initial check is checking for null instead of DBNull.Value.

private void getOrderId()
{
    var orders_dt = conn.Select("orders", "MAX(order_id)").GetQueryData();

    if (orders_dt == null || orders_dt.Rows.Count == 0 || orders_dt.Rows[0][0] == DBNull.Value)
    {
        //Should not preemptively insert into the database
        order_no.Text = "1";
    }
    else
    {
        int order_id = orders_dt.Rows[0].Field<int>(0) + 1;
        order_no.Text = order_id.ToString();
    }
}

Note:

//I'm using `Field<T>` to ensure correct datatype.  Change `Field<int>` to reflect whatever type it really is.
int order_id = orders_dt.Rows[0].Field<int>(0) + 1;
Zer0
  • 7,191
  • 1
  • 20
  • 34
  • I will try this later when I get to edit my code, thanks in advance, I'll let you know if it works. – Jammeh Jun 25 '18 at 05:36
  • May I ask if it's supposed to be Field(0) parenthesis? or [0]? – Jammeh Jun 25 '18 at 06:42
  • Thank you for this answer, the function now works. I never knew you could compare to a DBNull.value. The more you learn, thanks again! – Jammeh Jun 25 '18 at 12:13