Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a drop-down control to a data source in ASP.NET

I am new to C#.

I have a project to create an HR system and I created a page to add employees but the supervisor asked me to create a drop down list that displays the department when adding a new employee.

I don't know how to start and what I should do first. I already added a drop down list from the tools but I don't know how to choose the data source and the name and value of it. Should I choose the department table or the employee table?

public partial class _Default : Page
{

    private String strcon = ConfigurationManager.ConnectionStrings["hr"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)

            bindgrideview();
    }
    protected void bindgrideview()
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT F_name , L_name , salary FROM Employee ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();


        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

        adapter.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string F_name = TextBox1.Text;
        string L_name = TextBox2.Text;
        int status = 1;
        string salarystr = TextBox3.Text.ToString();
        int salary = Int32.Parse(salarystr);
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "ADDEMP";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        ADDCmd.CommandType = CommandType.StoredProcedure;
        ADDCmd.Parameters.AddWithValue("@F_name", F_name);
        ADDCmd.Parameters.AddWithValue("@L_name", L_name);
        ADDCmd.Parameters.AddWithValue("@status", status);
        ADDCmd.Parameters.AddWithValue("@salary", salary);

        ADDCmd.ExecuteNonQuery();
        bindgrideview();
        TextBox1.Text = "";
        TextBox2.Text = "";
    }

And this is a screen shot of my page: http://store2.up-00.com/Nov12/YXb11858.png

this is the final code there is no error but the dropdown list have no item :(

public partial class _Default : Page
{

    private String strcon = ConfigurationManager.ConnectionStrings["hr"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)

            bindgrideview();
    }
    protected void bindgrideview()
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT F_name , L_name , salary FROM Employee ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();


        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

        adapter.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string F_name = TextBox1.Text;
        string L_name = TextBox2.Text;
        int status = 1;
        string salarystr = TextBox3.Text.ToString();
        int salary = Int32.Parse(salarystr);
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "ADDEMP";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        ADDCmd.CommandType = CommandType.StoredProcedure;
        ADDCmd.Parameters.AddWithValue("@F_name", F_name);
        ADDCmd.Parameters.AddWithValue("@L_name", L_name);
        ADDCmd.Parameters.AddWithValue("@status", status);
        ADDCmd.Parameters.AddWithValue("@salary", salary);

        ADDCmd.ExecuteNonQuery();
        bindgrideview();
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
    }
    protected void bindDepartments()
    {
        SqlConnection strcon1 = new SqlConnection(strcon);
        strcon1.Open();
        string ADDStr = "SELECT ID,department_name FROM Department ";
        SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
        DataTable table = new DataTable();


        SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

        adapter.Fill(table);

        DropDownList1.DataSource = table;
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataTextField = "department_name";
        DropDownList1.DataBind();

    }

}
like image 552
nourah Avatar asked Mar 04 '13 15:03

nourah


People also ask

How do you bind multiple values in a DropDownList from the database?

SqlCommand cmd = new SqlCommand("select * from UserDetail where id = '" + DropDownList1. SelectedValue + "'", con); SqlDataAdapter Adpt = new SqlDataAdapter(cmd); DataTable dt = new DataTable();

What is the property used to set the SqlDataSource to a DropDownList?

DataValueField. The DataValueField property binds the values of the datasheet or database to the drop-down list which is then visible in the dropdown list.


1 Answers

As your code works for retrieving Employees Info. from database, you will retrieve the Departments info. from your Departments table.

protected void bindDepartments()
{
    SqlConnection strcon1 = new SqlConnection(strcon);
    strcon1.Open();
    string ADDStr = "SELECT DepartmentId,DepartmentName FROM Departments ";
    SqlCommand ADDCmd = new SqlCommand(ADDStr, strcon1);
    DataTable table = new DataTable();


    SqlDataAdapter adapter = new SqlDataAdapter(ADDCmd);

    adapter.Fill(table);

    ddlDepartments.DataSource = table;
    ddlDepartments.DataValueField = "DepartmentId"; //The Value of the DropDownList, to get it you should call ddlDepartments.SelectedValue;
    ddlDepartments.DataTextField = "DepartmentName"; //The Name shown of the DropDownList.
    ddlDepartments.DataBind();

}
like image 155
Muhammad Hani Avatar answered Nov 02 '22 23:11

Muhammad Hani