I am trying to add data from three TextBoxes in one query but I failed to find a solution
My view
Code
My code behind
C#
protected void Adauga_Raspunsuri(object sender, EventArgs e)
{
int id = Convert.ToInt32(txtParam.Text);
DataTable Dt = new DataTable();
Dt.Columns.Add("Raspuns", typeof(string));
Dt.Columns.Add("Corect", typeof(string));
Dt.Columns.Add("IdIntrebare", typeof(int));
for (int i = 1; i <= 3; i++)
{
string Raspuns = Request.Form["txtRaspuns" + i].ToString();
string Corect = Request.Form["txtCorect" + i].ToString();
int IdIntrebare = id;
Dt.Rows.Add(Raspuns, Corect, IdIntrebare);
}
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand("AddRaspunsuri", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@DetailInsertion", Dt);
try
{
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Inserted Successfully')", true);
}
catch (Exception es)
{
throw es;
}
}
}
Response.Redirect("/AdminPages/Raspunsuri.aspx");
}The error I have is that string Raspuns = Request.Form["txtRaspuns" + i].ToString(); is empty object


Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 2:03 PM EDT
Ok, I tried something else
I sent values from txtRaspuns1, 2 and 3 as session parameter and I got this value System.Web.UI.WebControls.TextBox
protected void Page_Load(object sender, EventArgs e) { if (Session["p1"] != null) { txtError1.Text = Session["p1"].ToString(); } if (Session["p2"] != null) { txtError2.Text = Session["p2"].ToString(); } if (Session["p3"] != null) { txtError3.Text = Session["p3"].ToString(); } }and
protected void Adauga_Raspunsuri(object sender, EventArgs e) { Session["p1"] = txtRaspuns1.ToString(); Session["p2"] = txtRaspuns2.ToString(); Session["p3"] = txtRaspuns3.ToString(); Response.Redirect("/AdminPages/Raspunsuri.aspx"); }why is that happening?
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:03 PM EDT
Thank you Tahir. I already know there is no data, but why? Error here
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:34 PM EDT
the button is enabled once all three boxes have some data
$(document).ready(function () { $("#RasDiv").on("change", function () { var l1 = $("#txtRaspuns1").val().length; var l2 = $("#txtRaspuns2").val().length; var l3 = $("#txtRaspuns3").val().length; if (l1 == 0 || l2 == 0 || l3 == 0) { $("#btnSave").attr('disabled', 'disabled'); } else { $("#btnSave").removeAttr('disabled'); } }); });Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 1:53 PM EDT
for (int i = 1; i <= 3; i++)
{
string Raspuns = Request.Params["txtRaspuns" + i];
string Corect = Request.Params["txtCorect" + i];
int IdIntrebare = id;
Dt.Rows.Add(Raspuns, Corect, IdIntrebare);
}
Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 7:17 PM EDT
By accessing the Text property of the TextBox controls (txtRaspuns1.Text, txtRaspuns2.Text, and txtRaspuns3.Text), you are now storing their actual values in the Session, and the data should be displayed correctly in the txtError1, txtError2, and txtError3 TextBoxes when you access the page again.
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 2:11 PM EDT
So, I got this
protected void Adauga_Raspunsuri(object sender, EventArgs e) { Session["p1"] = txtRaspuns1.ToString(); Session["p2"] = txtRaspuns2.ToString(); Session["p3"] = txtRaspuns3.ToString(); Response.Redirect("/AdminPages/Raspunsuri.aspx"); }has to be
protected void Adauga_Raspunsuri(object sender, EventArgs e) { Session["p1"] = txtRaspuns1.Text.ToString(); Session["p2"] = txtRaspuns2.Text.ToString(); Session["p3"] = txtRaspuns3.Text.ToString(); Response.Redirect("/AdminPages/Raspunsuri.aspx"); }so maybe there is something similar in ?
string Raspuns = Request.Form["txtRaspuns" + i].ToString();Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 12:52 PM EDT
To fix this issue, you can use the
Enabledproperty instead ofVisible. TheEnabledproperty controls whether the control is rendered as part of the HTML form and whether its value is included in theRequest.Formcollection. SettingEnabled="false"will make the control non-editable, but its value will still be available in theRequest.Formcollection.ASPX
By setting Enabled="true" for the TextBoxes that should contain user input and Enabled="false" for the TextBoxes that should be read-only, you ensure that their values will be accessible in the code-behind.
Add null checking condition in your code to handle issue
If the line string Raspuns = Request.Form["txtRaspuns" + i].ToString(); is resulting in an empty object, it means that the form field "txtRaspuns" + i does not exist in the Request.Form collection, or it does exist, but its value is empty or null.
To handle this situation, you can check if the form field exists in the
Request.Formcollection and if it has a non-empty value before attempting to use it.C#
// Check if the form field exists and has a non-empty value if (!string.IsNullOrEmpty(Raspuns) && !string.IsNullOrEmpty(Corect)) { Dt.Rows.Add(Raspuns, Corect, IdIntrebare); } else { // Handle the case where the form field is missing or has an empty value // For example, you can skip this iteration or log an error. }Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:05 PM EDT
But I am writting text to those textboxes before save. At least for txtRaspuns1, 2 and 3.txtCorect is a jquery from ddl
Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 1:13 PM EDT
Before saving record you putting value in textboxea Okay,
How do you post the value to the server side using jquery?
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:15 PM EDT
I'm not posting directly but just populating textboxes with
$(document).ready(function () { $("#ddlCorect1").on("change", function () { var l1 = $("#ddlCorect1 option:selected").text(); $("#txtCorect1").val(l1); }); $("#ddlCorect2").on("change", function () { var l2 = $("#ddlCorect2 option:selected").text(); $("#txtCorect2").val(l2); }); $("#ddlCorect3").on("change", function () { var l3 = $("#ddlCorect3 option:selected").text(); $("#txtCorect3").val(l3); }); });default values are NU for all
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:16 PM EDT
And I am posting with
Tahir AnsariRank #366 · 6.8K reputationPosted Jul 27, 2023, 1:32 PM EDT
When a button is disabled (Enabled="false"), it won't trigger any server-side events, including the Adauga_Raspunsuri method. Therefore, the button click event is not firing, and the data is not being processed on the server-side.
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:40 PM EDT
But I have no issues with the button, why bother?
Marius VasileRank #785 · 2.9K reputationPosted Jul 27, 2023, 1:57 PM EDT
I get the same error as Request.Form
Marius VasileRank #785 · 2.9K reputationPosted Jul 28, 2023, 9:40 AM EDT
Yes, so when I ask for
Session["p1"] = txtRaspuns1.Text.ToString();I get the result I need but when I run
for (int i = 1; i <= 3; i++) { string Raspuns = Request.Form["txtRaspuns" + i].ToString(); string Corect = Request.Form["txtCorect" + i].ToString(); int IdIntrebare = id; Dt.Rows.Add(Raspuns, Corect, IdIntrebare); }I got an error. And still cannot figure it out :(