No records return when the value being pass in parameter is from Function

廉价感情. 提交于 2019-12-11 03:28:29

问题


I'm a VB guy and slowly migrating to C#. Before i go on the main problem, I would like to show you first the function which i used in manipulating the string.

class NewString
{
    public static string RemoveExtraSpaces(string xString)
    {
        string iTemp = string.Empty;
        xString = xString.Trim();
        string[] words = xString.Split(' ');
        foreach (string xWor in words)
        {
            string xxWor = xWor.Trim();
            if (xxWor.Length > 0)
            {
                iTemp += " " + xxWor;
            }

        }
        return iTemp;
    }
}

The function simply removes all the trailing and extra spaces in the string. For example:

NewString.RemoveExtraSpaces("  Stack    OverFlow  ")
==> will return "Stack OverFlow"

So my problem is, when i use that function to remove spaces within the string being pass in the parameter, there will be no records binded in the datagridview.

    private void LoadCandidateList(bool SearchAll, string iKey)
    {
        using (MySqlConnection xConn = new MySqlConnection(ConnectionClass.ConnectionString))
        {
            using (MySqlCommand xCOmm = new MySqlCommand())
            {
                xCOmm.Connection = xConn;
                xCOmm.CommandType = CommandType.StoredProcedure;
                xCOmm.CommandText = "LoadCandidateList";
                xCOmm.Parameters.AddWithValue("LoadAll", Convert.ToInt16(SearchAll));

                string fnlKey = iKey.Trim();
                // when i use the code above, the procedure performs normally
                // but if i use the code below, no records will be return
                // why is that? i prompt it in the MessageBox to check 
                // and displays the correct value.

                // string fnlKey = NewString.RemoveExtraSpaces(iKey.Trim());
                // MessageBox.Show(fnlKey); // => return correct value

                xCOmm.Parameters.AddWithValue("iKey", fnlKey);
                xCOmm.Parameters.AddWithValue("iCurrentID", _CurrentEventID);

                using (DataSet ds = new DataSet())
                {
                    using (MySqlDataAdapter xAdapter = new MySqlDataAdapter(xCOmm))
                    {
                        try 
                        {
                            xConn.Open();
                            xAdapter.Fill(ds,"CandidateList");
                            grdResult.DataSource = ds.Tables["CandidateList"];
                        }
                        catch (MySqlException ex)
                        {
                            MessageBox.Show(ex.Message.ToString(), "Function Error <LoadCandidateList>", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        finally
                        {
                            xConn.Close();
                        }    
                    }
                }
            }
        }
    }

回答1:


Your function that replaces sequences of spaces with a single space can be re-written in a single line:

string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries);

If you would like to make it an extension method, you can do it like this:

static class StringExtensions {
    public static string RemoveExtraSpaces(this string xString) {
        return string.Join(" ", xString.Split(new []{' '}, StringSplitOptions.RemoveEmptyEntries));
    }
}



回答2:


How about you use the functionality while the data is binded to the Gridview?

<asp:TextBox ID="Name" runat="server" Text='RemoveExtraSpaces(<%# Bind("Name") %>);' AutoPostBack="false"></asp:TextBox> 


来源:https://stackoverflow.com/questions/9113086/no-records-return-when-the-value-being-pass-in-parameter-is-from-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!