简体   繁体   English

如何从非NULL的文本框中包含值?

[英]How can I include values from text boxes that are not NULL?

I have a method that gets the value from text boxes and inserts them into the database. 我有一个从文本框中获取值并将其插入数据库的方法。 How can I parse the values from only text boxes which have had text typed into them? 如何仅从已输入文本的文本框中解析值?

For example if textbox 4, 5 6 are empty then I do not want them to be parsed to the insert method. 例如,如果文本框4、5 6为空,那么我不希望将它们解析为insert方法。

This is what I have written to insert ONE answer into the database. 这就是我将一个答案插入数据库的内容。 How can I modify it to include only those text boxes that are NOT null? 如何修改它以仅包含那些不为null的文本框?

<div id="answer_wrapper">
                <div class="answer">
                    <h3 class="new">Answer 1</h3>
                    <asp:TextBox ID="AnswerBox1" runat="server" CssClass="form" Width="300px"></asp:TextBox>
                </div> <!-- end answer -->
                <div class="answer">
                    <h3 class="new">Answer 2</h3>
                    <asp:TextBox ID="AnswerBox2" runat="server" CssClass="form" Width="300px"></asp:TextBox>
                </div> <!-- end answer -->
                <div class="answer">
                    <h3 class="new">Answer 3</h3>
                    <asp:TextBox ID="AnswerBox3" runat="server" CssClass="form" Width="300px"></asp:TextBox>
                </div> <!-- end answer -->
                <div class="answer">
                    <h3 class="new">Answer 4</h3>
                    <asp:TextBox ID="AnswerBox4" runat="server" CssClass="form" Width="300px"></asp:TextBox>
                </div> <!-- end answer -->
                <div class="answer">
                    <h3 class="new">Answer 5</h3>
                    <asp:TextBox ID="AnswerBox5" runat="server" CssClass="form" Width="300px"></asp:TextBox>
                </div> <!-- end answer -->
                <div class="answer">
                    <h3 class="new">Answer 6</h3>
                    <asp:TextBox ID="AnswerBox6" runat="server" CssClass="form" Width="300px"></asp:TextBox>
                </div> <!-- end answer -->

protected void FinishQnrButton_Click(object sender, EventArgs e)
        {
            int QuestionnaireId = (int)Session["QuestionnaireID"];  
            SendData = new OsqarSQL();
            int questionId = SendData.InsertQuestions(QuestionName.Text, Int32.Parse(QuestnType.SelectedValue), QuestionnaireId);
            int answerId = SendData.InsertAnswer(AnswerBox1.Text, questionId, QuestionnaireId);
            Response.Redirect("~/buildq/Confirm_Questionnaire.aspx");

        } // End NewQNRButton_Click

        public int InsertAnswer(string AnswerTitle, int QuestionID, int QuestionnaireID)
        {
            int returnValue = 0;
            SqlCommand myCommand = new SqlCommand("NewAnswer", _productConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add(new SqlParameter("@ANSWERTITLE", SqlDbType.NVarChar));
            myCommand.Parameters.Add(new SqlParameter("@QUESTION_ID", SqlDbType.Int));
            myCommand.Parameters.Add(new SqlParameter("@QUEST_ID", SqlDbType.Int));
            myCommand.Parameters.Add("@ANSWER_ID", SqlDbType.Int, 0, "ANSWER_ID");
            myCommand.Parameters["@ANSWER_ID"].Direction = ParameterDirection.Output;
            myCommand.Parameters[0].Value = AnswerTitle;
            myCommand.Parameters[1].Value = QuestionID;
            myCommand.Parameters[2].Value = QuestionnaireID;
            _productConn.Open();
            myCommand.ExecuteNonQuery();
            returnValue = (int)myCommand.Parameters["@ANSWER_ID"].Value;
            _productConn.Close();
            return returnValue;
        }

Technically, the TextBox.Text property will never be null . 从技术上讲, TextBox.Text属性永远不会为null If no value is present in the form, it will be string.Empty . 如果表单中没有值,则为string.Empty

You can test for empty strings with something like this: 您可以使用以下内容测试空字符串:

if (!string.IsNullOrEmpty(AnswerBox1.Text))
    int answerId = SendData.InsertAnswer(AnswerBox1.Text, questionId, QuestionnaireId);

Note that I'm not entirely sure what your overall goal here is. 请注意,我不是很确定你这里的总体目标是什么。 Maybe I'm missing something. 也许我想念一些东西。 It sounds like you're asking how to check each TextBox for empty values and perform an insert on the ones which have non-empty values. 听起来您正在询问如何检查每个TextBox的空值,以及如何对具有非空值的TextBox执行插入操作。 So I guess you'd do something similar to the above several times. 因此,我想您会多次执行与上述类似的操作。 There are other ways to do it, you can wrap it all in helper methods, etc. But in general you're just testing for a "non-insertable according to the business rules" value. 还有其他方法可以将其包装在辅助方法中,等等。但是通常,您只是在测试“根据业务规则不可插入”的值。

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

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