hi

em trying to fetch Employee information from db and load it in Gridview....i select departemnt name and Grade from dropdownlist and pass it in query buh it returns Invalid column name 'InformationTechnology' and Invalid column name 'Grade17'

String strCon = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        SqlConnection objcon = new SqlConnection(strCon);
        objcon.Open();
        // String str1 = "'"+dd_AdmViewProf_dep.SelectedItem.Value.ToString()"'";
        string query = "Select * from EmpInfo where Department=" + dd_AdmViewProf_dep.SelectedItem.Value + " and Grade =" + dd_admViewProf_Grade.SelectedItem.Value;
        //Response.Write(query);
       // Response.End();
        SqlDataAdapter adp = new SqlDataAdapter(query,objcon);
        DataSet ds = new DataSet();
        adp.Fill(ds, "EmpInfo");

        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds.Tables["EmpInfo"].DefaultView;
            GridView1.DataBind();
        }
        else {

            Response.Write("unsucessful");
        
        }
    }

Have you tried inserting the variable inside single quotes?

ie:

string query = "Select * from EmpInfo where Department='" + dd_AdmViewProf_dep.SelectedItem.Value + "' and Grade ='" + dd_admViewProf_Grade.SelectedItem.Value +"'";

In sql queries, when a string is used as a where paramter, they normally need to be enclosed in ' ', this is not the case for integers.

I would suggest you step through your code at execution and see what the query string actually is. You can then copy the query and try to run it yourself and see if it works. If not, keep editing the syntax until it does, and you should have a good idea of how to correct your code.

Hope that helps.

yeah it worked:)))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.