0

Hi , i hv created 2 tables : a Department table with fields Deptname , Deptid and Employee table with Empid , Empname, Salary .
I have created a datagrid with fields empname , address , Salary , Deptname and edit and delete command columns . By clicking on edit , it redirects to another form with 3 textboxes ( name , address , salary) and a dropdownlist with department names and update button .So data will get edited and updated after clicking on update button .
I have binded the dropdownlist with values from database . I have used a query string using which data from datagrid is populated into the textboxes on the second page .
The problem that I am facing is that the deptnames for each row in datagrid r not getting selected in the dropdownlist while trying to edit.
The code used for binding the dropdownlist with database is as follows :

    protected void FillDeptDropDownList()
    {
        String strConnString = ConfigurationManager.ConnectionStrings["Company"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand("Select * from Department", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "DeptName";
        DropDownList1.DataValueField = "DeptID";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, "Select department name:");



    }

Please help with dis issue .

2
Contributors
1
Reply
14
Views
2 Years
Discussion Span
Last Post by tarunap
0

The above code for filling the dropdownlist is correct. Are you binding the dropdownlist in the OnRowDataBound of Gridview? In OnRowDataBound before

if (e.Row.RowType == DataControlRowType.DataRow)

        //Find the DropDownList in the Row
        DropDownList ddlCountries = (e.Row.FindControl("ddldepartment") as DropDownList);
        //continued by your code

    }

Check this out for further details.

This topic has been dead for over six months. 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.