我有一个下拉列表...我必须在其中选择员工的姓名....但是在后端 [英] i have a dropdownlist... where i have to select the name of the employee.... but at the back end

查看:66
本文介绍了我有一个下拉列表...我必须在其中选择员工的姓名....但是在后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个下拉列表...我必须在其中选择员工的姓名....但是在后端,它必须保存EMPLOYEE ID而不是员工的姓名.....



i have a dropdownlist... where i have to select the name of the employee.... but at the back end it must save EMPLOYEE ID not the name of the employee.....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Drawing;
using System.Net.Mail;

public partial class emp_details : System.Web.UI.Page
{
   SqlConnection cn = new SqlConnection("server=''fre-ggn-fstest\\sharepoint''; database = ''DMS1''; integrated security = ''true'';");
   SqlCommand cmd;
  // int id;
       
    protected void Page_Load(object sender, EventArgs e)
    {
        cn.Open();
        getCompName();
    }
    protected void btnSubmitdetails_Click(object sender, EventArgs e)
    {
        LBLErrorMsg.Text = " ";
        LBLSuccessfullySaved.Text = " ";

        SaveDetails();
        Response.Redirect("emp_details.aspx");   // this is done to avoid the duplicate insertion of values in database on refreshing 
    }

    public void SaveDetails()
    {
        string str = "insert into temp (emp_code,emp_name,paswrd,domain_name,dpt_code,dpt_name,email,role,CompCode) values (@emp_code,@emp_name,@paswrd,@domain_name,@dpt_code,@dpt_name,@email,@role,@CompCode)";

            cmd = new SqlCommand(str, cn);
            cmd.Parameters.Add("@emp_code", SqlDbType.Int).Value = Convert.ToInt32(txtEmpCode.Text);
           // cmd.Parameters.Add("@emp_id", SqlDbType.Int).Value = 1;
            cmd.Parameters.Add("@emp_name", SqlDbType.VarChar).Value = txtEmpName.Text;
            cmd.Parameters.Add("@paswrd", SqlDbType.VarChar).Value = txtPwd.Text;
            cmd.Parameters.Add("@domain_name", SqlDbType.VarChar).Value = txtDomainName.Text;
            cmd.Parameters.Add("@dpt_code", SqlDbType.VarChar).Value = DDLDeptName.SelectedValue;
            cmd.Parameters.Add("@dpt_name", SqlDbType.VarChar).Value = DDLDeptName.Text;
            cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtEmail.Text;
            cmd.Parameters.Add("@role", SqlDbType.Int).Value = Convert.ToInt32(DDLRole.SelectedIndex);

            cmd.Parameters.Add("@CompCode", SqlDbType.Int).Value = Convert.ToInt32(DDL_CompName.SelectedValue); // here i m faceing the problem... it is saveing first index value for all the records i want to enter.....
please help with the code..      
            cmd.ExecuteNonQuery();

            LBLSuccessfullySaved.Text = "Data Saved Successfully";

            // calling the "blankall" method to remove all the entrys
            blankall();
    }

    public void blankall()
    {
        txtEmpCode.Text = "";
        txtEmpName.Text = "";
        txtPwd.Text = "";
        txtRePwd.Text = "";
        txtDomainName.Text = "";
        txtEmail.Text = "";
    }

    public void getCompName()
    {
        string str = "select * from mCompnay";

        SqlDataAdapter sda = new SqlDataAdapter(str, cn);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        DDL_CompName.DataSource = ds.Tables[0];
        DDL_CompName.DataTextField = "comp_name";
        DDL_CompName.DataValueField = "comp_code";
        DDL_CompName.DataBind();

        //string str1 = "select comp_code from mCompnay where comp_name=''" + DDL_CompName.Text + "''";
        //SqlDataAdapter sda1 = new SqlDataAdapter(str1, cn);
        //DataSet ds1 = new DataSet();
        //sda1.Fill(ds1);
        //id = ;
    }

// pls read the above comment and help
// thanks
}

推荐答案

用户界面代码将为,
User Interface code will be,
<td align="left" style="width: 10%">
                <asp:dropdownlist id="EmployeeList" runat="server" autopostback="True" onload="EmployeeList_Load" xmlns:asp="#unknown">
                </asp:dropdownlist>
            </td>


后端代码将是


Back end code will be,

protected void DepartmentList_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
            cmd = new SqlCommand(str, cn);

            var emp = cmd.ExecuteQuery<employee>(@"SELECT EmpID
            FROM   EmpTable
            WHERE  EmpName = " + EmployeeList.SelectedItem);

            Session["SelectedEmp"] = emp.EmpID;
      }
  }


</employee>


这篇关于我有一个下拉列表...我必须在其中选择员工的姓名....但是在后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆