2013-03-04 5 views
5

Sono nuovo al C#.Come associare un controllo a discesa a un'origine dati in ASP.NET

Ho un progetto per creare un sistema di gestione delle risorse umane e ho creato una pagina per aggiungere dipendenti ma il supervisore mi ha chiesto di creare un elenco a discesa che visualizza il reparto quando si aggiunge un nuovo dipendente.

Non so come iniziare e cosa dovrei fare prima. Ho già aggiunto un elenco a discesa dagli strumenti ma non so come scegliere l'origine dati e il nome e il valore di esso. Dovrei scegliere il tavolo dipartimento o il tavolo dei dipendenti?

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

E questo è uno screenshot della mia pagina: http://store2.up-00.com/Nov12/YXb11858.png

questo è il codice finale non v'è alcun errore, ma l'elenco a discesa non hanno voce :(

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

    } 

} 

risposta

17

Come il vostro codice funziona per recuperare informazioni Employees dal database, si recupereranno le informazioni Departments dalla tabella dei dipartimenti

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

grazie mille non posso dirti quanto sono contento quando vedo la tua risposta ma c'è una cosa quando aggiungo un elenco a discesa come questa immagine http://store2.up-00.com/Nov12/JrJ12794 .png scelgo la tabella reparto ma il valore e il nome cosa sarà? – nourah

+0

@nourah - la proprietà Value è il testo che verrà mostrato nell'elenco a discesa. La proprietà Name ha indicato il valore che è possibile utilizzare per inserirlo nel database. L'approccio più comune è mostrare DepartmentName, rendere DepartmentId come valore, Supponendo che la tabella contenga i due campi. – MuhammadHani

+0

l'ID del compartimento nella tabella dei dipendenti, quindi come posso usarlo? :( – nourah

Problemi correlati