0

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");
        
        }
    }
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ryathegr8
0

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.

Edited by croker10: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.