不要将空文本框添加到数据库

时间:2018-04-20 14:26:02

标签: asp.net

我有30个可用于用户输入的文本框和一个需要仅插入带有值的文本框的“提交”按钮。目前,我有空文本框接收“null”值,但这会将单词“null”插入数据库而不是将其留空。

   protected void btnSubmit_Click(object sender, EventArgs e)
{

    String conString = @"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8";
    SqlConnection con = new SqlConnection(conString);

    //create a command behavior object
    String cmdString = "INSERT INTO [Positions]( Sport_ID, Position)" +
    "VALUES (@Sport_ID, @Position1), (@Sport_ID, @Position2), (@Sport_ID, @Position3), (@Sport_ID, @Position4), (@Sport_ID, @Position5), (@Sport_ID, @Position6), (@Sport_ID, @Position7), (@Sport_ID, @Position8), (@Sport_ID, @Position9), (@Sport_ID, @Position10), (@Sport_ID, @Position11), (@Sport_ID, @Position12), (@Sport_ID, @Position13), (@Sport_ID, @Position14), (@Sport_ID, @Position15), (@Sport_ID, @Position16), (@Sport_ID, @Position17), (@Sport_ID, @Position18), (@Sport_ID, @Position19), (@Sport_ID, @Position20), (@Sport_ID, @Position21), (@Sport_ID, @Position22), (@Sport_ID, @Position23), (@Sport_ID, @Position24), (@Sport_ID, @Position25), (@Sport_ID, @Position26), (@Sport_ID, @Position27), (@Sport_ID, @Position28), (@Sport_ID, @Position29), (@Sport_ID, @Position30)";
    SqlCommand cmd = new SqlCommand(cmdString, con);

//这只是一个使用5个位置的例子。

    SqlParameter param0 = new SqlParameter();
    param0.ParameterName = "@Sport_Id";
    param0.Value = Session["SportID"];
    cmd.Parameters.Add(param0);


    SqlParameter param1 = new SqlParameter();
    param1.ParameterName = "@Position1";
    param1.Value = TextBoxOpt1.Text;
    cmd.Parameters.Add(param1);


    SqlParameter param2 = new SqlParameter();
    param2.ParameterName = "@Position2";

    if (TextBoxOpt2.Text != String.Empty)
    {

        param2.Value = TextBoxOpt2.Text;

    }

    if (TextBoxOpt2.Text == String.Empty)
    {

        param2.Value = "null";

        //cmd.Parameters.AddWithValue("@Position2", param2==null ? (object)DBNull.Value : param2);
    }
    cmd.Parameters.Add(param2);


    SqlParameter param3 = new SqlParameter();
    param3.ParameterName = "@Position3";
    if (TextBoxOpt3.Text != String.Empty)
    {
        param3.Value = TextBoxOpt3.Text;
    }

    if (TextBoxOpt3.Text == String.Empty)
    {
        param3.Value = "null";
    }
     cmd.Parameters.Add(param3);

    SqlParameter param4 = new SqlParameter();
    param4.ParameterName = "@Position4";
    param4.Value = TextBoxOpt4.Text;

    if (TextBoxOpt4.Text != String.Empty)
    {
        param4.Value = TextBoxOpt4.Text;
    }
    if (TextBoxOpt4.Text == String.Empty)
    {
        param4.Value = "null";
    }
    cmd.Parameters.Add(param4);

    SqlParameter param5 = new SqlParameter();
    param5.ParameterName = "@Position5";
    param5.Value = TextBoxOpt5.Text;
    if (TextBoxOpt5.Text != String.Empty)
    {
        param5.Value = TextBoxOpt5.Text;
    }
    if (TextBoxOpt5.Text == String.Empty)
    {
        param5.Value = "null";
    }
    cmd.Parameters.Add(param5);

}

2 个答案:

答案 0 :(得分:0)

你在每个参数上定义

if (TextBoxOpt2.Text == String.Empty)
{
    param2.Value = "null";
}` 

您将null作为字符串传递,因此它也会将其作为null写入您的数据库。

你在评论中几乎有正确的方法。

cmd.Parameters.AddWithValue("@Position2", String.IsNullOrWhiteSpace(TextBoxOpt2.Text) ? (object)DBNull.Value:(object)TextBoxOpt2.Text;
然后,您可以移除所有这些ifs,然后将其转换为上面的一个衬垫。

如果你真的想把它留在其他地方

if(String.IsNullOrWhiteSpace(TextBoxOpt2.Text)){
   cmd.Parameters.AddWithValue("@Position2", DBNull.Value);
}else{
   cmd.Parameters.AddWithValue("@Position2", TextBoxOpt2.Text;
}

编辑工作示例

protected void btnSubmit_Click(object sender, EventArgs e)
{
    if (Session["SportID"] != null)
        if (int.TryParse(Session["SportID"].ToString(), out int SportId))
        {
            string conString = @"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8";
            using (SqlConnection con = new SqlConnection(conString))
            {
                string cmdString = "INSERT INTO [Positions](Sport_ID, Position) " +
                "VALUES (@Sport_ID, @Position1), (@Sport_ID, @Position2), (@Sport_ID, @Position3), (@Sport_ID, @Position4), (@Sport_ID, @Position5), (@Sport_ID, @Position6), (@Sport_ID, @Position7), (@Sport_ID, @Position8), (@Sport_ID, @Position9), (@Sport_ID, @Position10), (@Sport_ID, @Position11), (@Sport_ID, @Position12), (@Sport_ID, @Position13), (@Sport_ID, @Position14), (@Sport_ID, @Position15), (@Sport_ID, @Position16), (@Sport_ID, @Position17), (@Sport_ID, @Position18), (@Sport_ID, @Position19), (@Sport_ID, @Position20), (@Sport_ID, @Position21), (@Sport_ID, @Position22), (@Sport_ID, @Position23), (@Sport_ID, @Position24), (@Sport_ID, @Position25), (@Sport_ID, @Position26), (@Sport_ID, @Position27), (@Sport_ID, @Position28), (@Sport_ID, @Position29), (@Sport_ID, @Position30)";
                using (SqlCommand cmd = new SqlCommand(cmdString, con))
                {

                    cmd.Parameters.AddWithValue("@Sport_ID", SportId);
                    cmd.Parameters.AddWithValue("@Position1", String.IsNullOrWhiteSpace(TextBoxOpt1.Text) ? (object)DBNull.Value : (object)TextBoxOpt1.Text);
                    cmd.Parameters.AddWithValue("@Position2", String.IsNullOrWhiteSpace(TextBoxOpt2.Text) ? (object)DBNull.Value : (object)TextBoxOpt2.Text);
                    cmd.Parameters.AddWithValue("@Position3", String.IsNullOrWhiteSpace(TextBoxOpt3.Text) ? (object)DBNull.Value : (object)TextBoxOpt3.Text);
                    cmd.Parameters.AddWithValue("@Position4", String.IsNullOrWhiteSpace(TextBoxOpt4.Text) ? (object)DBNull.Value : (object)TextBoxOpt4.Text);
                    cmd.Parameters.AddWithValue("@Position5", String.IsNullOrWhiteSpace(TextBoxOpt5.Text) ? (object)DBNull.Value : (object)TextBoxOpt5.Text);
                    cmd.Parameters.AddWithValue("@Position6", String.IsNullOrWhiteSpace(TextBoxOpt6.Text) ? (object)DBNull.Value : (object)TextBoxOpt6.Text);
                    cmd.Parameters.AddWithValue("@Position7", String.IsNullOrWhiteSpace(TextBoxOpt7.Text) ? (object)DBNull.Value : (object)TextBoxOpt7.Text);
                    cmd.Parameters.AddWithValue("@Position8", String.IsNullOrWhiteSpace(TextBoxOpt8.Text) ? (object)DBNull.Value : (object)TextBoxOpt8.Text);
                    cmd.Parameters.AddWithValue("@Position9", String.IsNullOrWhiteSpace(TextBoxOpt9.Text) ? (object)DBNull.Value : (object)TextBoxOpt9.Text);
                    cmd.Parameters.AddWithValue("@Position10", String.IsNullOrWhiteSpace(TextBoxOpt10.Text) ? (object)DBNull.Value : (object)TextBoxOpt10.Text);
                    cmd.Parameters.AddWithValue("@Position11", String.IsNullOrWhiteSpace(TextBoxOpt11.Text) ? (object)DBNull.Value : (object)TextBoxOpt11.Text);
                    cmd.Parameters.AddWithValue("@Position12", String.IsNullOrWhiteSpace(TextBoxOpt12.Text) ? (object)DBNull.Value : (object)TextBoxOpt12.Text);
                    cmd.Parameters.AddWithValue("@Position13", String.IsNullOrWhiteSpace(TextBoxOpt13.Text) ? (object)DBNull.Value : (object)TextBoxOpt13.Text);
                    cmd.Parameters.AddWithValue("@Position14", String.IsNullOrWhiteSpace(TextBoxOpt14.Text) ? (object)DBNull.Value : (object)TextBoxOpt14.Text);
                    cmd.Parameters.AddWithValue("@Position15", String.IsNullOrWhiteSpace(TextBoxOpt15.Text) ? (object)DBNull.Value : (object)TextBoxOpt15.Text);
                    cmd.Parameters.AddWithValue("@Position16", String.IsNullOrWhiteSpace(TextBoxOpt16.Text) ? (object)DBNull.Value : (object)TextBoxOpt16.Text);
                    cmd.Parameters.AddWithValue("@Position17", String.IsNullOrWhiteSpace(TextBoxOpt17.Text) ? (object)DBNull.Value : (object)TextBoxOpt17.Text);
                    cmd.Parameters.AddWithValue("@Position18", String.IsNullOrWhiteSpace(TextBoxOpt18.Text) ? (object)DBNull.Value : (object)TextBoxOpt18.Text);
                    cmd.Parameters.AddWithValue("@Position19", String.IsNullOrWhiteSpace(TextBoxOpt19.Text) ? (object)DBNull.Value : (object)TextBoxOpt19.Text);
                    cmd.Parameters.AddWithValue("@Position20", String.IsNullOrWhiteSpace(TextBoxOpt20.Text) ? (object)DBNull.Value : (object)TextBoxOpt20.Text);
                    cmd.Parameters.AddWithValue("@Position21", String.IsNullOrWhiteSpace(TextBoxOpt21.Text) ? (object)DBNull.Value : (object)TextBoxOpt21.Text);
                    cmd.Parameters.AddWithValue("@Position22", String.IsNullOrWhiteSpace(TextBoxOpt22.Text) ? (object)DBNull.Value : (object)TextBoxOpt22.Text);
                    cmd.Parameters.AddWithValue("@Position23", String.IsNullOrWhiteSpace(TextBoxOpt23.Text) ? (object)DBNull.Value : (object)TextBoxOpt23.Text);
                    cmd.Parameters.AddWithValue("@Position24", String.IsNullOrWhiteSpace(TextBoxOpt24.Text) ? (object)DBNull.Value : (object)TextBoxOpt24.Text);
                    cmd.Parameters.AddWithValue("@Position25", String.IsNullOrWhiteSpace(TextBoxOpt25.Text) ? (object)DBNull.Value : (object)TextBoxOpt25.Text);
                    cmd.Parameters.AddWithValue("@Position26", String.IsNullOrWhiteSpace(TextBoxOpt26.Text) ? (object)DBNull.Value : (object)TextBoxOpt26.Text);
                    cmd.Parameters.AddWithValue("@Position27", String.IsNullOrWhiteSpace(TextBoxOpt27.Text) ? (object)DBNull.Value : (object)TextBoxOpt27.Text);
                    cmd.Parameters.AddWithValue("@Position28", String.IsNullOrWhiteSpace(TextBoxOpt28.Text) ? (object)DBNull.Value : (object)TextBoxOpt28.Text);
                    cmd.Parameters.AddWithValue("@Position29", String.IsNullOrWhiteSpace(TextBoxOpt29.Text) ? (object)DBNull.Value : (object)TextBoxOpt29.Text);
                    cmd.Parameters.AddWithValue("@Position30", String.IsNullOrWhiteSpace(TextBoxOpt30.Text) ? (object)DBNull.Value : (object)TextBoxOpt30.Text);

                    int added = 0;
                    try
                    {
                        con.Open();
                        added = cmd.ExecuteNonQuery();

                    }
                    catch (Exception err)
                    {
                        // Output.Text = err.Message;
                    }
                }
            }
            Response.Redirect("Questionnaire.aspx");
        }
        else
        {
            // your object does not containt a int. Parse failed
        }
    else
    {
        //your session object is empty
    }
}

答案 1 :(得分:0)

   protected void btnSubmit_Click(object sender, EventArgs e)
{

    String conString = @"Data Source=184.168.47.21;Initial Catalog=RecruitPursuit;Persist Security Info=True;User ID=RecruitPursuit;Password=Recruit20!8";
    SqlConnection con = new SqlConnection(conString);

    //create a command behavior object
    String cmdString = "INSERT INTO [Positions]( Sport_ID, Position)" +
    "VALUES (@Sport_ID, @Position1), (@Sport_ID, @Position2), (@Sport_ID, @Position3), (@Sport_ID, @Position4), (@Sport_ID, @Position5), (@Sport_ID, @Position6), (@Sport_ID, @Position7), (@Sport_ID, @Position8), (@Sport_ID, @Position9), (@Sport_ID, @Position10), (@Sport_ID, @Position11), (@Sport_ID, @Position12), (@Sport_ID, @Position13), (@Sport_ID, @Position14), (@Sport_ID, @Position15), (@Sport_ID, @Position16), (@Sport_ID, @Position17), (@Sport_ID, @Position18), (@Sport_ID, @Position19), (@Sport_ID, @Position20), (@Sport_ID, @Position21), (@Sport_ID, @Position22), (@Sport_ID, @Position23), (@Sport_ID, @Position24), (@Sport_ID, @Position25), (@Sport_ID, @Position26), (@Sport_ID, @Position27), (@Sport_ID, @Position28), (@Sport_ID, @Position29), (@Sport_ID, @Position30)";
    SqlCommand cmd = new SqlCommand(cmdString, con);


    SqlParameter param0 = new SqlParameter();
    param0.ParameterName = "@Sport_Id";
    param0.Value = Session["SportID"];
    cmd.Parameters.Add(param0);

    cmd.Parameters.AddWithValue("@Position1", String.IsNullOrWhiteSpace(TextBoxOpt1.Text) ? (object)DBNull.Value : (object)TextBoxOpt1.Text);

    cmd.Parameters.AddWithValue("@Position2", String.IsNullOrWhiteSpace(TextBoxOpt2.Text) ? (object)DBNull.Value : (object)TextBoxOpt2.Text);

    cmd.Parameters.AddWithValue("@Position3", String.IsNullOrWhiteSpace(TextBoxOpt3.Text) ? (object)DBNull.Value : (object)TextBoxOpt3.Text);
    cmd.Parameters.AddWithValue("@Position4", String.IsNullOrWhiteSpace(TextBoxOpt4.Text) ? (object)DBNull.Value : (object)TextBoxOpt4.Text);
    cmd.Parameters.AddWithValue("@Position5", String.IsNullOrWhiteSpace(TextBoxOpt5.Text) ? (object)DBNull.Value : (object)TextBoxOpt5.Text);
    cmd.Parameters.AddWithValue("@Position6", String.IsNullOrWhiteSpace(TextBoxOpt6.Text) ? (object)DBNull.Value : (object)TextBoxOpt6.Text);
    cmd.Parameters.AddWithValue("@Position7", String.IsNullOrWhiteSpace(TextBoxOpt7.Text) ? (object)DBNull.Value : (object)TextBoxOpt7.Text);
    cmd.Parameters.AddWithValue("@Position8", String.IsNullOrWhiteSpace(TextBoxOpt8.Text) ? (object)DBNull.Value : (object)TextBoxOpt8.Text);
    cmd.Parameters.AddWithValue("@Position9", String.IsNullOrWhiteSpace(TextBoxOpt9.Text) ? (object)DBNull.Value : (object)TextBoxOpt9.Text);
    cmd.Parameters.AddWithValue("@Position10", String.IsNullOrWhiteSpace(TextBoxOpt10.Text) ? (object)DBNull.Value : (object)TextBoxOpt10.Text);
    cmd.Parameters.AddWithValue("@Position11", String.IsNullOrWhiteSpace(TextBoxOpt11.Text) ? (object)DBNull.Value : (object)TextBoxOpt11.Text);
    cmd.Parameters.AddWithValue("@Position12", String.IsNullOrWhiteSpace(TextBoxOpt12.Text) ? (object)DBNull.Value : (object)TextBoxOpt12.Text);
    cmd.Parameters.AddWithValue("@Position13", String.IsNullOrWhiteSpace(TextBoxOpt13.Text) ? (object)DBNull.Value : (object)TextBoxOpt13.Text);
    cmd.Parameters.AddWithValue("@Position14", String.IsNullOrWhiteSpace(TextBoxOpt14.Text) ? (object)DBNull.Value : (object)TextBoxOpt14.Text);
    cmd.Parameters.AddWithValue("@Position15", String.IsNullOrWhiteSpace(TextBoxOpt15.Text) ? (object)DBNull.Value : (object)TextBoxOpt15.Text);
    cmd.Parameters.AddWithValue("@Position16", String.IsNullOrWhiteSpace(TextBoxOpt16.Text) ? (object)DBNull.Value : (object)TextBoxOpt16.Text);
    cmd.Parameters.AddWithValue("@Position17", String.IsNullOrWhiteSpace(TextBoxOpt17.Text) ? (object)DBNull.Value : (object)TextBoxOpt17.Text);
    cmd.Parameters.AddWithValue("@Position18", String.IsNullOrWhiteSpace(TextBoxOpt18.Text) ? (object)DBNull.Value : (object)TextBoxOpt18.Text);
    cmd.Parameters.AddWithValue("@Position19", String.IsNullOrWhiteSpace(TextBoxOpt19.Text) ? (object)DBNull.Value : (object)TextBoxOpt19.Text);
    cmd.Parameters.AddWithValue("@Position20", String.IsNullOrWhiteSpace(TextBoxOpt20.Text) ? (object)DBNull.Value : (object)TextBoxOpt20.Text);
    cmd.Parameters.AddWithValue("@Position21", String.IsNullOrWhiteSpace(TextBoxOpt21.Text) ? (object)DBNull.Value : (object)TextBoxOpt21.Text);
    cmd.Parameters.AddWithValue("@Position22", String.IsNullOrWhiteSpace(TextBoxOpt22.Text) ? (object)DBNull.Value : (object)TextBoxOpt22.Text);
    cmd.Parameters.AddWithValue("@Position23", String.IsNullOrWhiteSpace(TextBoxOpt23.Text) ? (object)DBNull.Value : (object)TextBoxOpt23.Text);
    cmd.Parameters.AddWithValue("@Position24", String.IsNullOrWhiteSpace(TextBoxOpt24.Text) ? (object)DBNull.Value : (object)TextBoxOpt24.Text);
    cmd.Parameters.AddWithValue("@Position25", String.IsNullOrWhiteSpace(TextBoxOpt25.Text) ? (object)DBNull.Value : (object)TextBoxOpt25.Text);
    cmd.Parameters.AddWithValue("@Position26", String.IsNullOrWhiteSpace(TextBoxOpt26.Text) ? (object)DBNull.Value : (object)TextBoxOpt26.Text);
    cmd.Parameters.AddWithValue("@Position27", String.IsNullOrWhiteSpace(TextBoxOpt27.Text) ? (object)DBNull.Value : (object)TextBoxOpt27.Text);
    cmd.Parameters.AddWithValue("@Position28", String.IsNullOrWhiteSpace(TextBoxOpt28.Text) ? (object)DBNull.Value : (object)TextBoxOpt28.Text);
    cmd.Parameters.AddWithValue("@Position29", String.IsNullOrWhiteSpace(TextBoxOpt29.Text) ? (object)DBNull.Value : (object)TextBoxOpt29.Text);
    cmd.Parameters.AddWithValue("@Position30", String.IsNullOrWhiteSpace(TextBoxOpt30.Text) ? (object)DBNull.Value : (object)TextBoxOpt30.Text);

    int added = 0;
    try
    {
        con.Open();
        added = cmd.ExecuteNonQuery();

    }

    catch (Exception err)
    {
       // Output.Text = err.Message;
    }

    finally
    {
        con.Close();
    }

    Response.Redirect("Questionnaire.aspx");
}