静态类方法不返回值

时间:2012-04-01 19:54:25

标签: c# asp.net arrays

我有以下代码(这只是为了演示,我不会粘贴所有行)

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 user_code=@user_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;
    }
}

然后我在Code behind file(* 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];
    }
}

但是,代码不返回任何内容(标签名称为空),甚至不会抛出异常。任何想法捕获的地方?谢谢。

3 个答案:

答案 0 :(得分: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 user_code=@user_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;

答案 1 :(得分:0)

好了,现在您已向我们展示了更多代码:如果SQL查询返回0行,则返回的数组将为空,如您所见。因此,您传递给user_code的值似乎不在数据库中 - 或者它们可能是错误的数据类型。

答案 2 :(得分:0)

您应该尝试使用调试器逐步完成此操作。有一种情况,field数组将返回一个包含六个空字符串元素的数组:如果code不是空字符串,则else子句将执行,如果您的SQL查询返回没有行,那么field将永远不会收到任何字符串值。我的猜测是这就是正在发生的事情。如果由于某种原因(?)无法使用调试器,则可以随时修改代码:

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