Guid GetCartID(string userName) { Guid result = Guid.NewGuid(); var existsSql = OrdersTable.Select(OrdersTable.Columns.OrderID) .Where(OrdersTable.Columns.UserName, userName) .And(OrdersTable.Columns.OrderStatusID, 99); object existingID = existsSql.BuildCommand().ExecuteScalar(); if (existingID == null) { //create one! var sql = OrdersTable.Insert(new Dictionary <string, object>() { { OrdersTable.Columns.OrderID, result }, { OrdersTable.Columns.UserName, userName }, { OrdersTable.Columns.OrderStatusID, 99 }, { OrdersTable.Columns.UserLanguageCode, System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName }, { OrdersTable.Columns.CreatedOn, DateTime.Now } }); //save it sql.BuildCommand().ExecuteNonQuery(); } else { result = (Guid)existingID; } return(result); }