Question

I been trying many ways but it keeps on giving me this error for the below code

using (custTrans db = new custTrans())
{
    Guid AgentGUID = Guid.Parse(AgentUID);
    var AgentQuery = from a in db.Agents
                     join u in db.UserLogins on a.UserUID equals u.UserUID into j
                     from u in j.DefaultIfEmpty()
                     where a.AgentUID == AgentGUID
                     select new { a.AgentUID  , a.Name, u.Login, u.UserUID, u.Type };

    foreach (var result in AgentQuery)
    {
        txtName.Text = result.Name;
        txtUsername.Text = result.Name
    }
}

I keep on getting this error when I try to load the records

The cast to value type 'Guid' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.

Anyone have any idea on how to solve this?

Était-ce utile?

La solution

After some suggestion I have made some changes on my code

 Guid AgentGUID = Guid.Parse(AgentUID);

 var AgentQuery = from a in db.Agents
                  join u in db.UserLogins on a.AgentUID equals u.UserUID into j
                  from u in j.DefaultIfEmpty()
                  where a.AgentUID == AgentGUID
                  select new { a.AgentUID, a.Name, Login = (u == null ? String.Empty : u.Login), UserUID = ( u == null ? Guid.Empty : u.UserUID), Type = (u == null ? -1 : u.Type) };

foreach (var result in AgentQuery)
{
    txtName.Text = result.Name;
    txtUsername.Text = result.Name;

    if (!string.IsNullOrEmpty(result.Login.ToString()))
    {
        chkEnableLogin.Checked = true;
        txtUsername.Text = result.Login;
        txtUsername.ReadOnly = true;
        ddlType.SelectedValue = result.Type.ToString();
        ViewState.Add("UserUID", result.UserUID);
    }
}

The select statement to check Null actually does the trick. Thanks everyone that gives me the hint

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top