Marius Vasile

Marius Vasile

  • 599
  • 1.7k
  • 124.9k

Add to sql server table data from multiple TextBox-es in one query

Jul 27 2023 4:07 PM

I am trying to add data from three TextBoxes in one query but I failed to find a solution

My view

<div class="row no-gutters">
    <div class="col-4">
        <asp:TextBox ID="txtRaspuns1" class="form-control" runat="server" TextMode="MultiLine" Rows="5" style="resize:none; height:99px;" ClientIDMode="Static" />
        <asp:DropDownList ID="ddlCorect1" class="form-control colorchg" runat="server" AutoPostBack="False" ClientIDMode="Static" style="color:white" >
            <asp:ListItem Text= "NU" Value="red"></asp:ListItem>
            <asp:ListItem Text= "DA" Value="green"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="txtCorect1" runat="server" Visible="false" ClientIDMode="Static"/>
    </div>
    <div class="col-4">
        <asp:TextBox ID="txtRaspuns2" class="form-control" runat="server" TextMode="MultiLine" Rows="5" style="resize:none; height:99px;" ClientIDMode="Static" />
        <asp:DropDownList ID="ddlCorect2" class="form-control colorchg" runat="server" AutoPostBack="False" ClientIDMode="Static" style="color:white" >
            <asp:ListItem Text= "NU" Value="red"></asp:ListItem>
            <asp:ListItem Text= "DA" Value="green"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="txtCorect2" runat="server" Visible="false" ClientIDMode="Static"/>
    </div>
    <div class="col-4">
        <asp:TextBox ID="txtRaspuns3" class="form-control" runat="server" TextMode="MultiLine" Rows="5" style="resize:none; height:99px;" ClientIDMode="Static" />
        <asp:DropDownList ID="ddlCorect3" class="form-control colorchg" runat="server" AutoPostBack="False" ClientIDMode="Static" style="color:white" >
            <asp:ListItem Text= "NU" Value="red"></asp:ListItem>
            <asp:ListItem Text= "DA" Value="green"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="txtCorect3" runat="server" Visible="false" ClientIDMode="Static"/>
    </div>
</div>

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


Answers (15)