Click here to Skip to main content
16,004,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code.
XML
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 <asp:ScriptManager ID="scm" runat="server"></asp:ScriptManager>
 <table>
    <tr>
        <td>
            <asp:DropDownList ID="ddldepartment" runat="server"
                onselectedindexchanged="ddldepartment_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
        </td>
    </tr>
    <tr>
         <td>
            <asp:GridView ID="gv" runat="server" DataKeyNames="empid"></asp:GridView>
        </td>
    </tr>
 </table>
</asp:Content>


in aspx.cs
C#
public partial class GetDetailsbydropdown : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CustomersBusiness cb = new CustomersBusiness();
            if (!IsPostBack)
            {
                ddldepartment.DataSource = cb.GetDepartment();
                ddldepartment.DataTextField = "departmentid";
                ddldepartment.DataValueField = "departmentname";
                ddldepartment.DataBind();
            }
        }

        protected void ddldepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            CustomersBusiness cb=new CustomersBusiness();
            Customers cs = new Customers();
            cs.departmentid = Convert.ToInt32(ddldepartment.SelectedItem.Text);
            gv.DataSource = cb.GetEmployeebyDepartment(cs);
            gv.DataBind();
        }
    }

in businesslogic.cs

        public List<customers> GetEmployeebyDepartment(Customers cs)
        {
            List<customers> lst = new List<customers>();
            SqlConnection con = new SqlConnection(constring);
            con.Open();
            string query = "select empid, empname, departmentid, joiningdate, locationid, salary, jobid, isworking where departmentid=@departmentid";
            cmd = new SqlCommand(query, con);
            dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    Customers cst = new Customers();
                    cst.employeeid = (int)dr[0];
                    cst.empname = (string)dr[1];
                    cst.departmentid = (int)dr[2];
                    cst.joiningdate = (DateTime)dr[3];
                    cst.locationid = (int)dr[4];
                    cst.salary = (int)dr[5];
                    cst.jobid = (int)dr[6];
                    cst.isworking = (Boolean)dr[7];
                    lst.Add(cst);
                }
            }
            return lst;
        }
i am getting error must declare scalar variable
please help me
thank you.
Posted
Updated 17-Feb-12 1:42am
v2

Your SQL is using @departmentid and SQL does not know what that is. You need to do
C#
cmd.Parameters.AddWithValue("@departmentid", deptid);
 
Share this answer
 
Change your Query to
SQL
string query = "select empid, empname, departmentid, joiningdate, locationid, salary, jobid, isworking where departmentid="+cs.departmentid;
 
Share this answer
 
v2
Comments
sreekanth12 17-Feb-12 8:25am    
sudip saha thank you its working.

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