Skip to content

How to change statment to accept (= and like) in same time

ahmed sa

Asked Aug 26, 2014, 3:37 AM EDT2 answers
Share
LinkedInX
Hi guys i make dynamic filter in datagridview but i face proplem
when i using to search for number as example
555 and inside id three values as 555,5555,55555 it give me three value
but actually there are one value found so that i need to use equal =  with like statment in same time
are this possible my code as below:
 



private void searchgrid_TextChanged(List<field> fields)

 System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource); //
 _textFilters = ""; //reset filters











bool first = true; //to handle the " and "








foreach (field f in fields)




{




if (f.Value.Length > 0) //only if there is a value to filter for




{




if (!first) _textFilters += " and ";




_textFilters += f.Field + " like '%" + f.Value + "%'";








 first = false;




}
}
dv.RowFilter = _textFilters;




this.label1.Text = _textFilters;




}




2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    can any one know how to use = or between with like statment
    if possible help me
    0
  • 0
    the logic what i need is
    my logic is as following bur from c# not sql
    Create Procedure sp_EmployeeSelect
    /* Input Parameters */
    @EmployeeName NVarchar(100),
    @Department NVarchar(50),
    @Designation NVarchar(50),
    @StartDate DateTime,
    @EndDate DateTime,
    @Salary Decimal(10,2)

    AS
    Set NoCount ON
    /* Variable Declaration */
    Declare @SQLQuery AS NVarchar(4000)
    Declare @ParamDefinition AS NVarchar(2000)
    /* Build the Transact-SQL String with the input parameters */
    Set @SQLQuery = 'Select * From tblEmployees where (1=1) '
    /* check for the condition and build the WHERE clause accordingly */
    If @EmployeeName Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (EmployeeName = @EmployeeName)'

    If @Department Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (Department = @Department)'

    If @Designation Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (Designation = @Designation)'

    If @Salary Is Not Null
    Set @SQLQuery = @SQLQuery + ' And (Salary >= @Salary)'

    If (@StartDate Is Not Null) AND (@EndDate Is Not Null)
    Set @SQLQuery = @SQLQuery + ' And (JoiningDate
    BETWEEN @StartDate AND @EndDate)'
    /* Specify Parameter Format for all input parameters included
    in the stmt */
    Set @ParamDefinition = ' @EmployeeName NVarchar(100),
    @Department NVarchar(50),
    @Designation NVarchar(50),
    @StartDate DateTime,
    @EndDate DateTime,
    @Salary Decimal(10,2)'
    /* Execute the Transact-SQL String with all parameter value's
    Using sp_executesql Command */
    Execute sp_Executesql @SQLQuery,
    @ParamDefinition,
    @EmployeeName,
    @Department,
    @Designation,
    @StartDate,
    @EndDate,
    @Salary

    If @@ERROR <> 0 GoTo ErrorHandler
    Set NoCount OFF
    Return(0)

    ErrorHandler:
    Return(@@ERROR)
    GOThis sample stored
    i need to do same logic but from interface c#
    0