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();
}
}
SqlCommand cmd = new SqlCommand("select * from UserDetail where id = '" + DropDownList1. SelectedValue + "'", con); SqlDataAdapter Adpt = new SqlDataAdapter(cmd); DataTable dt = new DataTable();
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With