2013-03-04 6 views
5

私はC#を初めて使用しています。ドロップダウンコントロールをASP.NETのデータソースにバインドする方法

HRシステムを作成するプロジェクトがあり、従業員を追加するためのページを作成しましたが、スーパーバイザは新しい従業員を追加するときに部門を表示するドロップダウンリストを作成するように求めました。

私は最初のやり方を始めて何をすべきかわかりません。私はすでにツールからドロップダウンリストを追加しましたが、データソースとその名前と値を選択する方法はわかりません。私は部門テーブルまたは従業員テーブルを選択する必要がありますか?

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 = ""; 
    } 

そして、これは私のページのスクリーンショットです: http://store2.up-00.com/Nov12/YXb11858.png

これは何のエラーが、ドロップダウンリストがありません最終的なコードがあるにはアイテム:(として

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(); 

    } 

} 

答えて

17

を持っていないあなたコードはEmployees情報をデータベースから取得するために働きます。Departments情報を部門テーブルから取得します。

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(); 

} 
+0

ありがとうございました。私は答えてくればどれくらい幸せなのかは分かりませんが、この写真のようにドロップダウンリストを追加すると事があります。http://store2.up-00.com/Nov12/JrJ12794 .png私は部門テーブルを選択しますが、値と名前は何になりますか? – nourah

+0

@nourah - Valueプロパティは、ドロップダウンリストに表示されるテキストです。 Nameプロパティは、データベースに挿入するために使用できる値を示しました。最も一般的なアプローチは、DepartmentNameを表示し、DepartmentIdを値として、テーブルに2つのフィールドがあると仮定します。 – MuhammadHani

+0

従業員テーブルのdpartment IDはどのように使用できますか? :( – nourah

関連する問題