如何使用asp.net将下拉列表中的空值添加到sql server

时间:2016-03-22 04:52:24

标签: asp.net database

我在SQL服务器中创建了两个表:

TblStudent:

TblStudent

TblProvince:

TblProvince

在Asp.net设计表格中

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="StudentName"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Province"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddlProvince" runat="server"></asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

在Asp.net后端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //bound data from TblProvince to ddlProvince 
            using (SqlConnection con  = DBManager.getConnection())
            {
                string sql = @"Select ProvinceId, Province From TblProvince";
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "dtProvince");
                ddlProvince.DataSource = ds.Tables["dtProvince"];
                ddlProvince.DataValueField = "ProvinceId";
                ddlProvince.DataTextField = "Province";
                ddlProvince.DataBind();
                ddlProvince.Items.Insert(0, new ListItem("Please Select", "NULL"));
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = DBManager.getConnection())
        {
            string sql = @"Insert Into TblStudent(Name, ProvinceId) Values(@Name, @ProvinceId)";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.Add("@Name", SqlDbType.NChar).Value = txtStudentName.Text;
            cmd.Parameters.Add("@ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
}

问题:
当我没有在ddlProvince中选择为什么当你将它保存到数据库时它显示错误Input string was not in a correct format.

1 个答案:

答案 0 :(得分:1)

它抛出&#34;输入字符串的格式不正确&#34;错误,因为您尝试将null转换为Int。

cmd.Parameters.Add("@ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);

您可以检查SelectedValue是否为null,然后将其转换为Int。

cmd.Parameters.Add("@ProvinceId", SqlDbType.Int).Value = (ddlProvince.SelectedValue == null) ? null :  Convert.ToInt32(ddlProvince.SelectedValue)