Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to select records from dataase using different search criteria.
Posted

Like this?

LINQ and Dynamic Predicate Construction at Runtime[^]

I assume you're using LINQ to SQL since you're not giving any details.
 
Share this answer
 
Comments
woutercx 22-Jul-12 11:30am    
But like barneyman below says; If you're using it on the internet, it could be susceptible to sql injection attacks. Intranet would be fine.
Try to Build Your Query Like below 


string sql = "select * from tablname where 1=1";
        if (txtSearchRiskId.Text != "")
        {
            sql += "fRiskID ='" + txtSearchRiskId.Text + "' and ";
        }
        if (txtsearchrisksequenceno.Text != "")
        {
            sql += "fSerialNo like'%" + txtsearchrisksequenceno.Text + "%'and ";
        }
        if (txtsearchriskplateno.Text != "")
        {
            sql += "fPlateNo like'%" + txtsearchriskplateno.Text + "%' or fPlateNo_bl like'%" + txtsearchriskplateno.Text + "%'and ";
        }
        if (txtsearchriskyear.Text != "")
        {
            sql += "fYear like'%" + txtsearchriskyear.Text + "%'and ";
        }
        if (txtsearchriskchassisno.Text != "")
        {
            sql += "fChassisNo like'%" + txtsearchriskchassisno.Text + "%'and ";
        }
        if (txtsearchriskcustomid.Text != "")
        {
            sql += "fCustomID like'%" + txtsearchriskcustomid.Text + "%'and ";
        }
        if (ddlsearchRiskTypeCodevalue.SelectedIndex > 0)
        {
            sql += "fVehicleTypevalue like'%" + ddlsearchRiskTypeCodevalue.SelectedItem.Value + "%'and ";
        }
        if (ddlsearchRiskmakevalue.SelectedIndex > 0)
        {
            sql += "fMakevalue like'%" + ddlsearchRiskmakevalue.SelectedItem.Value + "%'and ";
        }
        if (ddlsearchRiskmodelvalue.SelectedIndex > 0)
        {
            sql += "fModelvalue like'%" + ddlsearchRiskmodelvalue.SelectedItem.Value + "%'";
        }
 
Share this answer
 
v2
Comments
barneyman 22-Jul-12 8:49am    
no - don't do it like that at all - susceptible to SQL injection attacks - use parameters - remember Bobby Tables

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900