Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
But they are working when only one is selected but when i select the data between two i.e one is dept and other date then it display the all data of that user and between the dates which means 2nd and 4th if statement is not working

here is my code

C#
protected void btnreprt_Click(object sender, EventArgs e)
    {
        string date1 = Request.Form[txtdate1.UniqueID];
        string.Format("dd/MM/yyyy", date1);
        string date2 = Request.Form[txtdate2.UniqueID];
        string.Format("dd/MM/yyyy", date1);
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\site\SamsungClassifieds\App_Data\Dorknozzle.mdf;Integrated Security=True;User Instance=True");
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        string search = "SELECT AD_TITLE, AD_DESC, AD_IMG, AD_PRICE, AD_NO, SUB_CAT_DESC, CAT_DESC, SELLER_NAME, SELLER_MOB_NO, SELLER_EMAIL_ID, SELLER_DEP, SELLER_FLOOR,DATE FROM AD_DETAIL_V WHERE BUY_SELL_ID='1' AND DELETED_AD='0'";
        if (txtemp.Text != "")
        {
            search = search + " and EMP_ID = '" + txtemp.Text + "'";
        }
        else if (txtemp.Text != "" & date1 != "" & date2 != "")
        {
            search = search + " and EMP_ID = '" + txtemp.Text + "'And Date between '" + date1 + "'and '" + date2 + "'";
        }
        else if (txtdept.Text != "")
        {
            search = search + " and SELLER_DEP = '" + txtdept.Text + "'";
        }
        else if (txtdept.Text != "" & date1 != "" & date2 != "")
        {
            search = search + " and SELLER_DEP = " + txtdept.Text + "And Date between '" + date1 + "'and '" + date2 + "'";
        }
        else if (date1 != "" & date2 != "")
        {
            search = search + "And Date between '" + date1 + "'and '" + date2 + "'";
        }
        cmd.CommandText = search;
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(ds);
        result.DataSource = ds;
        result.DataBind();      
    }
Posted
Updated 11-Aug-13 21:36pm
v2
Comments
Herman<T>.Instance 12-Aug-13 3:57am    
you do not format date2 in your code, only date1 twice
format yyyy/MM/dd is Always internal in a db understood as a datevalue pattern

Change if condition in following way.

C#
if (txtemp.Text != "")
{
    search = search + " and EMP_ID = '" + txtemp.Text + "'";
}

if (date1 != "" & date2 != "")
{
    search = search + " and Date between '" + date1 + "' and '" + date2 + "'";
}

if (txtdept.Text != "")
{
    search = search + " and SELLER_DEP = '" + txtdept.Text + "'";
}
 
Share this answer
 
Comments
Harshit Wadhera 12-Aug-13 4:41am    
thanx for the answer.....
assign this can also give the answer

in 1st if statement

if (txtemp.Text != "" && txtdate1.Text == "" && txtdate2.Text == "")

in 3 if statement

else if (txtdept.Text != "" && txtdate1.Text == "" && txtdate2.Text == "")
 
Share this answer
 

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