2012-04-01 133 views
-1

我有下面的代碼(這只是爲了演示,我不會糊所有線路)靜態類方法沒有返回值

public static class SearchAndEdit 
{ 
    public static string[] SearchAndDisplay(string code) 
    { 
     string SQLconnection = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; 
     string[] field; 
     field = new string[6]; 
     if (code == "") 
     { 
      field[0] = "Nothing to search"; 
     } 
     else 
     { 
      string SQLSelect = "SELECT user_name, user_surname, user_code, user_group, user_password FROM Users WHERE [email protected]_code"; 
      SqlConnection connect = new SqlConnection(SQLconnection); 
      SqlCommand search = new SqlCommand(SQLSelect, connect); 
      search.Parameters.Clear(); 
      search.Parameters.AddWithValue("@user_code", code); 
      try 
      {      
       connect.Open(); 
       SqlDataReader info = search.ExecuteReader(); 
       if (info.HasRows) 
       { 
        field[0] = "Data loaded"; 
       } 

      } 
      finally 
      { 
       connect.Close(); 
      } 
     } 
     return field; 
    } 
} 

然後,我用它的代碼隱藏文件(* aspx.cs )。

protected void Search_click(object sender, EventArgs e) 
{ 
    string[] information = SearchAndEdit.SearchAndDisplay(searchBox.Text); 
    for (int i = 0; i < information.Length; i++) 
    { 
     name.Text += information[i]; 
    } 
} 

但是,代碼沒有返回值(標籤是空的),甚至沒有拋出異常。任何想法在哪裏捕捉?謝謝。

+10

說「這僅用於演示,我不會粘貼所有行」的問題是你總會忽略導致問題的部分。這裏沒有什麼不對,所以你所遺漏的部分是不對的。 – 2012-04-01 19:57:38

+1

你應該考慮使用'string.IsNullOrWhitespace()'而不是'==「」'。 – 2012-04-01 19:57:52

+1

在你的SearchAndDisplay中放置一個斷點並進入它。可能會告訴你發生了什麼事。 – user710502 2012-04-01 19:58:26

回答

1

試試這個

if(string.IsNullOrEmpty(code)) 
{ 
    field[0] = "Empty box"; 
} 

更改了帖子。你應該有這個代替

string SQLconnection = WebConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString; 
    string[] field; 
    field = new string[6]; 
    field[0] = "Nothing to search"; // add here in case string is null 
    if (!string.IsNullOrEmpty(code)) 
    { 
     string SQLSelect = "SELECT user_name, user_surname, user_code, user_group, user_password FROM Users WHERE [email protected]_code"; 
     SqlConnection connect = new SqlConnection(SQLconnection); 
     SqlCommand search = new SqlCommand(SQLSelect, connect); 
     search.Parameters.Clear(); 
     search.Parameters.AddWithValue("@user_code", code); 
     try 
     {      
      connect.Open(); 
      SqlDataReader info = search.ExecuteReader(); 
      if (info.HasRows) 
      { 
       field[0] = "Data loaded"; 
      } 

     } 
     catch 
     { 
      // error handle here problem with connection 
     } 
     finally 
     { 
      connect.Close(); 
     } 
    } 
    return field; 
0

好了,現在你已經向我們展示更多的代碼:如果SQL查詢返回0行,則返回的數組將是空的,因爲你觀察。因此,您傳遞給user_code的值似乎不在數據庫中 - 或者它們可能是錯誤的數據類型。

0

你應該嘗試用調試器來完成這一步。有一種情況,field數組將返回一個由六個空字符串元素組成的數組:如果code不是空字符串,則會執行else子句,如果您的SQL查詢不返回任何行,則field將永遠不會收到任何字符串值。我的猜測是,這是發生了什麼事。如果由於某種原因(?)而無法使用調試器,則可以隨時修改代碼:

if (info.HasRows) { 
    field[0] = "Data loaded"; 
} else { 
    field[0] = "No data found"; 
}