2012-05-14 8 views
0

로그인 할 때 다음 코드를 사용하고 있습니다. 내가 로그인하려고하면 로그인하는 동안 NullReferenceException이 발생합니다.

 public logininfo ViewLoginByUsernameAndPassword(logininfo linfo) 
     { 
     logininfo linf = null; 
     SqlDataReader dr = null; 
     try 
     { 
      con.Open(); 
      SqlCommand com = new SqlCommand("adminlogin", con); 
      com.CommandType = CommandType.StoredProcedure; 
      SqlParameter q = new SqlParameter(); 
      q = com.Parameters.AddWithValue("@username", linfo.username); 
      q = com.Parameters.AddWithValue("@password", linfo.password); 

      dr = com.ExecuteReader(); 
      linf = new logininfo(); 

      while (dr.Read()) 
      { 

       linf.username = dr["username"].ToString(); 
       linf.password = dr["password"].ToString(); 

      } 
     } 
     catch (Exception ex) 
     { 

      return new logininfo(); 
     } 
     finally 
     { 

      dr.Close(); 
      con.Close(); 
     } 
     return linf; 


     } 

, 나는 예외가

나는 또한 설정
 See the end of this message for details on invoking 
     just-in-time (JIT) debugging instead of this dialog box. 

     ************** Exception Text ************** 
     System.NullReferenceException: Object reference not set to an instance of an 
     object. 
     at 

     chitty.App_Code.DAL.SERVICES.loginservice.ViewLoginByUsernameAndPassword 
     (logininfo linfo) 
     at chitty.frmlogin.btn_login_Click(Object sender, EventArgs e) 
     at System.Windows.Forms.Control.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnClick(EventArgs e) 
     at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 
     at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 
     clicks) 
     at System.Windows.Forms.Control.WndProc(Message& m) 
     at System.Windows.Forms.ButtonBase.WndProc(Message& m) 
     at System.Windows.Forms.Button.WndProc(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
     at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
     at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr 
     wparam, IntPtr lparam) 


     ************** Loaded Assemblies ************** 
     mscorlib 
     Assembly Version: 4.0.0.0 
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100) 
     CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll 
     ---------------------------------------- 
    chitty 
    Assembly Version: 1.0.0.0 
    Win32 Version: 1.0.0.0 
    CodeBase: file:///C:/Program%20Files/itek/chittysoft/chitty.exe 
    ---------------------------------------- 
     System.Windows.Forms 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 built by: RTMRel 
     CodeBase:    

     file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms 

     /v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll 
     ---------------------------------------- 
     System.Drawing 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 built by: RTMRel 
     CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/   
     v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll 
     ---------------------------------------- 
     System 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 built by: RTMRel 
     CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/ 
     v4.0_4.0.0.0__b77a5c561934e089/System.dll 
     ---------------------------------------- 
     System.Data 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 (RTMRel.030319-0100) 
     CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Data/ 
     v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll 
     ---------------------------------------- 
    System.Core 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 built by: RTMRel 
     CodeBase:    
     file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/ 
     v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll 
     ---------------------------------------- 
     System.Configuration 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 (RTMRel.030319-0100) 
     CodeBase: 
     file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/ 
     v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll 
     ---------------------------------------- 
     System.Xml 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 built by: RTMRel 
     CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/ 
     v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll 
     ---------------------------------------- 
     System.Transactions 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 (RTMRel.030319-0100) 
     CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Transactions/ 
     v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll 
     ---------------------------------------- 
     System.EnterpriseServices 
     Assembly Version: 4.0.0.0 
     Win32 Version: 4.0.30319.1 (RTMRel.030319-0100) 
     CodeBase: 
     file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/ 
     v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll 
     ---------------------------------------- 

     ************** JIT Debugging ************** 
     To enable just-in-time (JIT) debugging, the .config file for this 
     application or computer (machine.config) must have the 
     jitDebugging value set in the system.windows.forms section. 
     The application must also be compiled with debugging 
     enabled. 

     For example: 

     <configuration> 
       <system.windows.forms jitDebugging="true" /> 
     </configuration> 

     When JIT debugging is enabled, any unhandled exception 
     will be sent to the JIT debugger registered on the computer 
     rather than be handled by this dialog box. 

인 NullReferenceException..And 발견 "jitdebugging = true"로 app.config.But에서 여전히이 오류가 발생합니다. 이를위한 해결책을 찾으십시오.

답변

0

나는 이것이 finally 블록에서 dr을 닫으려고하기 때문에 발생하는 것 같다. finally 블록은 항상 호출 할 것이므로 항상 실행하려고한다.

dr 개체를 열거 나 만들기 전에 오류가 발생할 수 있습니다.이 상태에서 dr은 할당되거나 열리지 않을 수 있으며 강제로 닫을 수 있습니다.

마지막으로 대신 catch 블록에서이 코드를 이동하거나 con와 dr을 사용할 수 있고 try 블록 밖에서 작성할 수 있습니다.

finally 
    { 
     dr.Close(); 
     con.Close(); 
    } 
관련 문제