2010-11-23 33 views
1

在這段代碼連接還沒有在catch塊初始化錯誤

private void btnConnect_Click(object sender, EventArgs e) 
    { 
     string localHost = "192.168.10.3"; 
     string logInDetails = "gp"; 
     SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); 

     try 
     { 

      //Checking for the Valid entries in textboxes. 
      if ((txtPassword.Text == logInDetails) && (txtUsername.Text == logInDetails)) 

       //Checking for the appropriate local server address. 
       if (txtHost.Text == localHost) 
       { 
        sConnection.Open(); 
        BindDBDropDown(); 
        SetOperationDropDown(); 
        PrimaryKeyTable(); 
        lblError.Text = "You are connected to the SQL Server...."; 
       } 
       else 
       { 
        lblError.Text = "Invalid Credentials"; 
       } 

     } 
     catch (Exception ex) 
     { 
      //All the exceptions are handled and written in the EventLog. 
      EventLog log = new EventLog("Application"); 
      log.Source = "MFDBAnalyser"; 
      log.WriteEntry(ex.Message); 
     } 
     finally 
     { 
      //To close the connection 
      if (sConnection != null) 
      { 
       sConnection.Close(); 
      } 
     } 
    } 

連接未初始化錯誤被捕獲...

在app.config文件我有

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="ConnectionString" value="Data Source=192.168.10.3;InitialCatalog=GoalPlanForTrainees;userid=gp;password=gp"/> 
    </appSettings> 
</configuration> 

可能是什麼問題

回答

1

目前您只打開連接(sConnection.Open();)在try之內。當然,最有可能拋出異常的地方是,這條線是。無論哪種方式,連接可能不一定在catch中打開。

而如果根本問題在調用.Open()失敗了,沒有太多的,它可以 ...