2010-11-26 5 views
0

J'ai une fonction comme celui-ciexception référence nulle

public bool CheckMentorAccess() 
{ 
    bool blnAllow = false; 
    try 
    { 
     string strSelectMentorQuery = "SELECT COUNT(DISTINCT MLL.LED_ID) FROM M_USER_DETAILS MUD INNER JOIN M_LEADERLED MLL " + "ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString() + "' AND MUD.ACTIVE = 1 AND MLL.START_DATE <= Getdate() AND" + " MLL.END_DATE > Getdate()"; 

     int intNoOfMembers = Convert.ToInt32(cSQLHelper.myExecuteScalar(strSelectMentorQuery)); 

     if (intNoOfMembers > 0) 
     { 
      blnAllow = true; 
     } 
    } 
    catch (Exception ex) 
    { 
     ExceptionLogger.LogException(ex); 
     blnAllow = false; 
    } 

    // Return the value 
    return blnAllow; 
} 

Et puis-je utiliser si comme ce

if ((Int32.Parse(Session["ROLE_ID"].ToString()) == 3) && (CheckMentorAccess() == true)) 
{ 
    cmbempList.Visible = true; 
} 

mais il jette une exception de référence null sur la première ligne de l'exemple ci-dessus

Quelqu'un peut-il m'aider ..

+0

La clé « ROLE_ID » existent réellement en session? – bblack

Répondre

0

Eh bien, si la trace de la pile seulement points à cette ligne, je suppose que c'est le problème:

Int32.Parse(Session["ROLE_ID"].ToString()) 

Cela va lancer une NullReferenceException si Session["ROLE_ID"] renvoie NULL.

(Il lancera FormatException si la valeur existe dans la session, mais n'est pas un entier valide.)

Il est possible que c'est l'appel à CheckMentor() qui échoue, bien sûr - mais votre trace de pile ne doit dis comme ça. Cela pourrait échouer si cSQLHelper est nul, par exemple.

1

Vous pouvez

if (Session["ROLE_ID"] != null && (Int32.Parse(Session["ROLE_ID"].ToString()) == 3) && (CheckMentorAccess() == true)) 
{ 
    cmbempList.Visible = true; 
} 

Commencerpars'assurerqu'iln'yaplus Session["ROLE_ID"] existe avant d'utiliser ToString.

Il est toujours plus sûr d'utiliser Convert.ToString.

0

Essayez ceci - vérifier les valeurs que vous utilisez lorsque vous appelez la méthode:

if (Session["ROLE_ID"] != null) 
{ 
    if ((Int32.Parse(Session["ROLE_ID"]) == 3) && (CheckMentorAccess())) 
    { 
     cmbempList.Visible = true; 
    } 
} 

et de vérifier dans la méthode

public bool CheckMentorAccess() 
{ 
    if (Session["UserID"] == null) 
    { 
     throw new NullReferenceException("UserID is null"); 
    } 

    bool blnAllow = false; 
    try 
    { 
     string strSelectMentorQuery = "SELECT COUNT(DISTINCT MLL.LED_ID) FROM M_USER_DETAILS MUD INNER JOIN M_LEADERLED MLL " + "ON MLL.LED_ID = MUD.PK_ID WHERE MLL.LEADER_ID = '" + Session["UserID"].ToString() + "' AND MUD.ACTIVE = 1 AND MLL.START_DATE <= Getdate() AND" + " MLL.END_DATE > Getdate()"; 

     int intNoOfMembers = Convert.ToInt32(cSQLHelper.myExecuteScalar(strSelectMentorQuery)); 

     blnAllow = intNoOfMembers > 0; 
    } 
    catch (Exception ex) 
    { 
     ExceptionLogger.LogException(ex); 
     blnAllow = false; 
    } 

    // Return the value 
    return blnAllow; 
} 
Questions connexes