I am trying to add data from three TextBoxes in one query but I failed to find a solution
My view
My code behind
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 VasilePosted Jul 28, 2023, 1:40 PM
Yes, so when I ask for
I get the result I need but when I run
I got an error. And still cannot figure it out :(
Tahir AnsariPosted Jul 27, 2023, 11:17 PM
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 VasilePosted Jul 27, 2023, 6:11 PM
So, I got this
has to be
so maybe there is something similar in ?
Marius VasilePosted Jul 27, 2023, 6:03 PM
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
and
why is that happening?
Marius VasilePosted Jul 27, 2023, 5:57 PM
I get the same error as Request.Form
Tahir AnsariPosted Jul 27, 2023, 5:53 PM
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);
}
Marius VasilePosted Jul 27, 2023, 5:40 PM
But I have no issues with the button, why bother?
Marius VasilePosted Jul 27, 2023, 5:34 PM
the button is enabled once all three boxes have some data
Tahir AnsariPosted Jul 27, 2023, 5:32 PM
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 VasilePosted Jul 27, 2023, 5:16 PM
And I am posting with
Marius VasilePosted Jul 27, 2023, 5:15 PM
I'm not posting directly but just populating textboxes with
default values are NU for all
Tahir AnsariPosted Jul 27, 2023, 5:13 PM
Before saving record you putting value in textboxea Okay,
How do you post the value to the server side using jquery?
Marius VasilePosted Jul 27, 2023, 5:05 PM
But I am writting text to those textboxes before save. At least for txtRaspuns1, 2 and 3.txtCorect is a jquery from ddl
Marius VasilePosted Jul 27, 2023, 5:03 PM
Thank you Tahir. I already know there is no data, but why? Error here
Tahir AnsariPosted Jul 27, 2023, 4:52 PM
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#