希望DropDownlist值按ID存储

时间:2014-11-24 11:08:03

标签: asp.net vb.net

我正在使用VB和SQL数据库在asp.net上工作 我有两张桌子mst_Emp& mst_dept

mst_dept获得了以下列(dpt_ID(PK),dpt_name,dpt_descrip)

mst_Emp获得了以下列(Emp_ID,Emp_FirstName,Emp_LastName,Emp_Address,Emp_ContactNo,Dept_ID(Foreign key),Marital_Status,Gender)

现在我有一份员工明细表,我有Department Name label(来自mst_Dept的DataBind),我有DropDownList可供选择。如果有人从DropDownList中选择值,我希望它由Dept_ID存储在数据库中。我怎样才能做到这一点 ?

2 个答案:

答案 0 :(得分:0)

尝试绑定您的部门Dropdownlist,如

Using sqlconn As New SqlConnection(ConfigurationManager.ConnectionStrings("Conn").ConnectionString)
    If sqlconn.State = ConnectionState.Closed Then
        sqlconn.Open()
    End If
    Dim ds As New DataSet()

    Dim qry As String = "Select dpt_ID,dpt_name from mst_Dept"

    Using cmd As New SqlCommand(qry, sqlconn)
        Dim sda As New SqlDataAdapter(cmd)
        sda.Fill(ds)
        D_ddlDepartment.DataSource = ds
        D_ddlDepartment.DataValueField = "dpt_ID"
        D_ddlDepartment.DataTextField = "dpt_name"
        D_ddlDepartment.DataBind()
        D_ddlDepartment.Items.Insert(0, "-- Select Department --")
 If sqlconn.State = ConnectionState.Open Then
    sqlconn.Close()
 End If
    End Using
End Using

您可以访问Dept_id之类的

Dim Deptid As Integer = Convert.ToInt32(D_ddlDepartment.SelectedValue)

答案 1 :(得分:0)

最后感谢: - )

        Dim ds As New DataSet
        Dim cmd1 As SqlCommand = New SqlCommand()
        Dim sqlconn As SqlConnection = New SqlConnection()
        sqlconn.ConnectionString = "Data Source=PRGM\SQLEXPRESS;Initial Catalog=HRMS;Integrated Security=True"
        sqlconn.Open()
        cmd1 = New SqlCommand("select Dpt_ID,Dpt_Name from mst_Dept", sqlconn)
        'Dim qry As String = "select Dpt_ID,Dpt_Name from mst_Dept"
        'cmd1 As New SqlCommand(qry, sqlconn)
        Dim sda As New SqlDataAdapter(cmd1)
        sda.Fill(ds)
        DropDownList1.DataSource = ds.Tables(0)
        DropDownList1.DataValueField = ds.Tables(0).Columns("Dpt_ID").ColumnName
        DropDownList1.DataTextField = ds.Tables(0).Columns("Dpt_Name").ColumnName
        DropDownList1.DataBind()
        DropDownList1.Items.Insert(0, "-- Select Department --")

        sqlconn.Close()
    End If