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.
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?
<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.Textproperty will never benull. If no value is present in the form, it will bestring.Empty.You can test for empty strings with something like this:
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
TextBoxfor empty values and perform an insert on the ones which have non-empty values. 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.