简体   繁体   English

DropDownList 中的值不会保存到 SQL Server 表中

[英]Value from DropDownList will not save into SQL Server table

I have a DropDownList that gets populated by a SQL Server table called tblVisa .我有一个 DropDownList,它由名为tblVisa的 SQL Server 表tblVisa My issue is that the values that are being populated from the SQL Server table are not being saved.我的问题是未保存从 SQL Server 表填充的值。 Everything else gets saved except for my DropDownLists.除了我的 DropDownLists 之外,其他所有内容都会被保存。 I've tried using .SelectedValue and .Text , but it still does not work.我试过使用.SelectedValue.Text ,但它仍然不起作用。

Here is my code这是我的代码

protected void PopulateVisaType()
{
        List<ListItem> result = new List<ListItem> { new ListItem("", "") };

        SqlCommand cmd = new SqlCommand() { Connection = sqlConn, CommandText = "SELECT VisaType FROM tblVisa ORDER BY VisaType ASC" };

        if (sqlConn.State == ConnectionState.Closed) 
        {
            sqlConn.Open(); 
        }

        SqlDataReader read = cmd.ExecuteReader();

        while (read.Read())
        {
            result.Add(new ListItem(read["VisaType"].ToString(), read["VisaType"].ToString()));
        }

        read.Close();
        sqlConn.Close();
        cmd.Dispose();

        DDLVisa.DataSource = result;
        DDLVisa.DataValueField = "value";
        DDLVisa.DataTextField = "text";
        DDLVisa.DataBind();
}

Here's my code for saving the information into the database:这是我将信息保存到数据库中的代码:

protected void LbSaveProfile_Click(object sender, EventArgs e)
{
    SqlCommand cmd = new SqlCommand() { Connection = sqlConn, CommandText = "spSaveNewProviderInformation", CommandType = CommandType.StoredProcedure };

    if (sqlConn.State == ConnectionState.Closed)
    { 
        sqlConn.Open(); 
    }

    cmd.Parameters.AddWithValue("@EmployeeNumber", TbEmployeeNumber.Text.Trim());
    cmd.Parameters.AddWithValue("@SSN", TbSSN.Text.Trim());
    cmd.Parameters.AddWithValue("@ContractType", DDLContractType.SelectedItem.Value);
    cmd.Parameters.AddWithValue("@Firstname", TbFirstname.Text.Trim());
    cmd.Parameters.AddWithValue("@Lastname", TbLastname.Text.Trim());
    cmd.Parameters.AddWithValue("@MiddleInitial", TbMiddleInitial.Text.Trim());
    cmd.Parameters.AddWithValue("@ContractRenewalDate", TbContractRenewalDate.Text.Trim());
    cmd.Parameters.AddWithValue("@Position", DDLPosition.Text.Trim());
    cmd.Parameters.AddWithValue("@Specialty", DDLSpecialty.Text.Trim());
    cmd.Parameters.AddWithValue("@PrimaryDepartment", DDLPrimaryDepartment.Text.Trim());
    cmd.Parameters.AddWithValue("@SecondaryDepartment", DDLSecondaryDepartment.Text.Trim());
    cmd.Parameters.AddWithValue("@Gender", DDLGender.Text.Trim());
    cmd.Parameters.AddWithValue("@Birthdate", TbBirthdate.Text.Trim());
    cmd.Parameters.AddWithValue("@EmailAddress", TbEmailAddress.Text.Trim());
    cmd.Parameters.AddWithValue("@PhoneNumber", TbPhoneNumber.Text.Trim());
    cmd.Parameters.AddWithValue("@Address", TbAddress.Text.Trim());
    cmd.Parameters.AddWithValue("@PassportNumber", TbPassportNumber.Text.Trim());
    cmd.Parameters.AddWithValue("@Citizenship", DDLCitizenship.Text.Trim());
    cmd.Parameters.AddWithValue("@Visa", DDLVisa.Text.Trim());
    cmd.Parameters.AddWithValue("@Status", 1);

    cmd.ExecuteNonQuery();

    sqlConn.Close();

    Alert("Provider Information saved!");

    ClearControls();
}

You much better to provide the drop down list with column names.您最好提供带有列名的下拉列表。

So, say this:所以,这样说:

    protected void PopulateVisaType()
    {
        SqlConnection sqlConn = new SqlConnection("");

        using (SqlCommand cmd = new SqlCommand("SELECT VisaType FROM tblVisa ORDER BY VisaType ASC", sqlConn))
        {
            if (sqlConn.State == ConnectionState.Closed)
            {
                sqlConn.Open();
            }

            DDLVisa.DataSource = cmd.ExecuteReader();
            DDLVisa.DataValueField = "VisaType";
            DDLVisa.DataTextField = "VisaType";

            DDLVisa.DataBind();
            //DDLVisa.Items.Insert(0, new ListItem(""));   // optional blank row choice

            sqlConn.Close();
        }
    }

So the TextField, and the DataText field need to be a named column from the data source.因此 TextField 和 DataText 字段需要是来自数据源的命名列。

I also included an optional first blank option if you need/want/expect to have no choice.如果您需要/想要/期望别无选择,我还包含了一个可选的第一个空白选项。

However, keep in mind that this empty string should be translated into a null in your database if you don't allow (or want) empty strings, and want a null for that value.但是,请记住,如果您不允许(或想要)空字符串,并且希望该值的空值,则应将此空字符串转换为数据库中的空值。 This applies to all of your values.这适用于您的所有价值观。 (perhaps the stored procedure does this?). (也许存储过程会这样做?)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM