1

I'm working on a custom module for our site. The process flow is as follows.

  • Customer hits a specific page
  • Page detects if customer is authenticated
    • No - forces login/register
    • Yes - Continues
  • Verifies customer data
  • Retrieves token and ttl from sister site
  • Stores data in db table
  • Redirects customer to sister site using token to login

I have the following class:

[TableName("AutoRegister_Data")]
[PrimaryKey("rId", AutoIncrement = true)]
[Scope("ModuleId")]
[Cacheable("AutoRegister_Data", CacheItemPriority.Normal, 20)]
public class RegisterData
{
    public int rId { get; set; }
    public int UserID { get; set; }
    public string Email { get; set; }
    public string LastToken { get; set; }
    public DateTime LastUpdate { get; set; }
}

Using the following code to update the database

using (IDataContext db = DataContext.Instance())
{
    var repo = db.GetRepository<RegisterData>();
    var user = repo.Find($"WHERE UserID = {currentUser.UserID}");

    db.BeginTransaction();

    try
    {
        if (!user.Any())
        {
            repo.Insert(dataRecord);
            db.Commit();
        }
        else
        {
            string query = string.Empty;
            if (token != null)
            {
                query = $"SET [LastUpdateDate] = \'{DateTime.Now}\', [LastToken] = \'{token}\' WHERE [UserID] = {currentUser.UserID}";
            }
            repo.Update(query);
        }
    }
    catch (Exception ex)
    {
        db.RollbackTransaction();
        Exceptions.ProcessModuleLoadException(this, ex);
    }
}

The dataRecord is of the class RegisterData.

I get an Object reference not set to an instance of an object error.

If I take the update out of a transaction block the table gets updated with correct data but the error still occurs.

Any guidance would be most appreciated.

~Scott

VDWWD
  • 35,079
  • 22
  • 62
  • 79
Dreampoet
  • 72
  • 1
  • 7
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Lasse V. Karlsen Feb 03 '16 at 15:27
  • Where is the error being thrown, on the INSERT statement? Is the repo NULL or the dataRecord null? Is USERID populated? – Chris Hammond Feb 04 '16 at 04:07

1 Answers1

1

enter code hereThe Find method takes an id of the object in the database, or call it better the primary key value of the table in the database. Replace Where clause as in the code below

var user = repo.Where(UserID == $"{currentUser.UserID}");
VDWWD
  • 35,079
  • 22
  • 62
  • 79
Julius Depulla
  • 1,493
  • 1
  • 12
  • 27