locked
String Concat with inbetween operator RRS feed

  • Question

  • User-1826049516 posted

    Hi,

    I'm having a complete mind blank for this.

    I have 2 strings, one is formed from user entry in a Textbox control, the other is pulled from the database. Only if both are not blank, I need to insert "and" between them. The combined string is the basis of a DataView RowFilter.

    private string SearchValue
    {
    	get
    	{
    		string _entry = Search_Text.Text; 'user input from textbox, can be empty
    		string _default = UserDefaultSearch; 'property that pulls string from db based on current user, can be empty
    		return String.Concat(_user, " and ", _default);
    	}
    }
    ...
    ...
    ...
    DataView view;
    view = <datasource>;
    view.RowFilter = SearchValue;
    

    As is, this will not work because if user input is empty the concat will have an invalid leading " and " and if _default is empty the concat will have an invalid trailing " and "

    Thanks!

    Wednesday, September 26, 2018 10:40 AM

Answers

  • User-821857111 posted

    You've probably solved it by now but:

    return string.Concat(_user, string.IsNullOrEmpty(_user) || string.IsNullOrEmpty(_default) ? "" : " and ", _default);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2018 9:12 AM
  • User839733648 posted

    Hi ldoodle,

    To form a RowFilter value, specify the name of a column followed by an operator and a value to filter on. The value must be in quotation marks.

    So you should write the SearchValue like "LastName = 'Smith'".

    For more information about RowFilter, you could refer to: https://docs.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter?view=netframework-4.7.2 

    Please pay attention to the Remarks.

    Besides, I've made a sample on my side, for more you could refer to the following code.

    .aspx

        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>

    code behind.

    protected void Button1_Click(object sender, EventArgs e)
            {
                string constr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tb_info",con);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    string _default = dt.Rows[0][1].ToString();
                    string _entry = TextBox1.Text.ToString();
                    string connectstring= String.Concat(_entry, " and ", _default);
                    Response.Write(connectstring);
    
                    DataView view = new DataView();
                    view.Table = ds.Tables["tb_info"];
                    view.RowFilter = "a='"+ connectstring + "'";
                }
            }

    result:

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2018 9:19 AM

All replies

  • User-821857111 posted

    You've probably solved it by now but:

    return string.Concat(_user, string.IsNullOrEmpty(_user) || string.IsNullOrEmpty(_default) ? "" : " and ", _default);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2018 9:12 AM
  • User839733648 posted

    Hi ldoodle,

    To form a RowFilter value, specify the name of a column followed by an operator and a value to filter on. The value must be in quotation marks.

    So you should write the SearchValue like "LastName = 'Smith'".

    For more information about RowFilter, you could refer to: https://docs.microsoft.com/en-us/dotnet/api/system.data.dataview.rowfilter?view=netframework-4.7.2 

    Please pay attention to the Remarks.

    Besides, I've made a sample on my side, for more you could refer to the following code.

    .aspx

        <form id="form1" runat="server">
            <div>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>

    code behind.

    protected void Button1_Click(object sender, EventArgs e)
            {
                string constr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tb_info",con);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    string _default = dt.Rows[0][1].ToString();
                    string _entry = TextBox1.Text.ToString();
                    string connectstring= String.Concat(_entry, " and ", _default);
                    Response.Write(connectstring);
    
                    DataView view = new DataView();
                    view.Table = ds.Tables["tb_info"];
                    view.RowFilter = "a='"+ connectstring + "'";
                }
            }

    result:

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 27, 2018 9:19 AM