Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys!

I have an image button for searching and a grid view for displaying records.If the record/s does not exist, the page will display the number of items found. And when it found a record that matches, it will display in my grid view. Now, my problem here is my search function does not work. Can anyone help me?

Here is my .cs file for Searching:

C#
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        repgen_connection();
        DataTable dt = new DataTable();
        if (sbu.SelectedItem.Text == null && company_code.Text == null && buyer_name.Text == null && proj_name.Text == null && unit_desc.Text == null && milestone.SelectedItem.Text == null && checklist.SelectedItem.Text == null && activity.SelectedItem.Text == null)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('No records found!');", true);
        }
        else if (sbu.SelectedValue != null || company_code.Text != null || buyer_name.Text != null || proj_name.Text != null || unit_desc.Text != null || milestone.SelectedValue != null || checklist.SelectedValue != null || activity.SelectedValue != null)
        {
            string strQuery = "Select DISTINCT brand, company_code,Contract_num,buyer_name, proj_name ,unit_description, milestone, checklist,activity, status,logged_by,date_created,completed_by,date_completed FROM  rawdata WHERE ";
            if (sbu.SelectedValue != "")
            { strQuery += " brand LIKE '%" + sbu.SelectedValue + "%' AND"; }
            if (company_code.Text != "")
            { strQuery += " company_code LIKE '%" + company_code.Text + "%' AND"; }
            if (buyer_name.Text != "")
            { strQuery += " buyer_name LIKE '%" + buyer_name.Text + "%' AND"; }
            if (proj_name.Text != "")
            { strQuery += " proj_name LIKE '%" + proj_name.Text + "%' AND"; }
            if (unit_desc.Text != "")
            { strQuery += " unit_description LIKE '%" + unit_desc.Text + "%' AND"; }
            if (milestone.SelectedValue != "")
            { strQuery += " milestone LIKE '%" + milestone.SelectedValue + "%' AND"; }
            if (checklist.SelectedValue != "")
            { strQuery += " checklist LIKE '%" + checklist.SelectedValue + "%' AND"; }
            if (activity.SelectedValue != "")
            { strQuery += " activity LIKE '%" + activity.SelectedValue + "%' AND"; }
            strQuery += " proj_name and unit_description AND company_code  AND status='not completed' ORDER BY Contract_num DESC";

            SqlCommand query = new SqlCommand(strQuery);
            SqlDataAdapter sda = new SqlDataAdapter();
            query.CommandType = CommandType.Text;
            query.Connection = amicassaCon_repgen;

            try
            {
                amicassaCon_repgen.Open();
                sda.SelectCommand = query;
                sda.Fill(dt);
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                // Response.Write(ex.Message);
            }
            finally
            {
                amicassaCon_repgen.Close();
                sda.Dispose();
                amicassaCon_repgen.Dispose();
                //Response.Write(strQuery);
            }
        }
       

        int tot = 0;
        foreach (DataRow dr in dt.Rows)
        {
            tot = tot + 1;
        }
        itms.Text = "Item's found: " + tot + " items";
    }
Posted
Updated 17-Aug-14 20:05pm
v2
Comments
ClimerChinna 18-Aug-14 2:38am    
strQuery += " proj_name and unit_description AND company_code AND status='not completed' ORDER BY Contract_num DESC";

what is the purpose of above line
Prasad Avunoori 18-Aug-14 3:00am    
First of all move this inline query to a stored procedure for better performance, readability and non-vulnerable to sql injections.

change
C#
strQuery += " proj_name and unit_description AND company_code  AND status='not completed' ORDER BY Contract_num DESC";

to
C#
strQuery += " proj_name = 'not completed' and unit_description ='not completed' AND company_code ='not completed' AND status='not completed' ORDER BY Contract_num DESC";
 
Share this answer
 
I think you will get the below error because of
C#
strQuery += " proj_name and unit_description AND company_code  AND status='not completed' ORDER BY Contract_num DESC"; 
line

error:
An expression of non-boolean type specified in a context where a condition is expected, near 'and'.

tel what you want to do with the above line so that we can tell you what to do
 
Share this answer
 
Comments
DarkDreamer08 18-Aug-14 19:29pm    
I got no error in terms of that. I just put it like that on the last part of my if statement so that, whatever the user puts in the proj_name, unit description, company code text boxes and the sattus is not completed, then the record/s that matches with the condition displays.
ClimerChinna 18-Aug-14 23:50pm    
Then use like below

strQuery += " proj_name = 'not completed' and unit_description ='not completed' AND company_code ='not completed' AND status='not completed' ORDER BY Contract_num DESC";
See a Full Example in below link. This will help you to fill your requirement.

http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/[^]
 
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