Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
Request.Form on input tag is empty

I am using Request.Form to get the text in the input field, however the value is always empty. I am trying to get the text value in the input tage and query my database. I am using a datepicker which writes the value to the input tag. I have tried writing to an asp Textbox, however the problem is after querying the database with the text in TextBox. When I choose a new date the Textbox does not update with new date value from datepicker. That is why I am using input tag. The problem here is that Request.Form["input"] always gets an empty string.

.aspx file

<form runat="server" method="post">      
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="insert into db" OnClick="Button1_Click" />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </ContentTemplate>
</asp:UpdatePanel>
</form>

script.js

var picker = new Pikaday(
    {
        field: document.getElementById('input'),
        trigger: document.getElementById('scheduleDate'),
        minDate: new Date(Date.now()),
        disableDayFn: function (date) {
            // Disable Monday
            var allDates = date.getDate();
            return date.getDay() === 6 || date.getDay() === 0;
                //block out dates that are fully booked.
        },
        toString(date, format) { // using moment.js
            return moment(date).format('YYYY-MM-DD');
        },

    });

cs file

protected void Button1_Click(object sender, EventArgs e)
    {
        string date = Request.Form["input"];
        
        using (SqlConnection connection = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; Integrated Security = True"))
        {
            //string selectquery = "SELECT * FROM Events";
            string sqlquery = "INSERT INTO [Events] (start_date, end_date) VALUES ('" + date + "', '" + date + "')";
            connection.Open();
            SqlCommand command = new SqlCommand(sqlquery, connection);
            command.ExecuteNonQuery();
            connection.Close();

        }

    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You don't need (or want) to use request.form.

Just drop your controls into the page. And then they can be freely used throuhout your code. So, say this markup:

<ContentTemplate>
    <asp:Button ID="Button1" runat="server" Text="insert into db" OnClick="Button1_Click" />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</ContentTemplate>

Now, in your code?

DateTime MyDate = DateTime.Parse(TextBox2.Text);

string MyTextBox1Text = TextBox1.Text;

So, in your case, you looking to do this:

using (SqlConnection conn = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; Integrated Security = True"))
{
    string sqlquery = "INSERT INTO [Events] (start_date, end_date) VALUES (@MyDate,@MyDate)";
    using (SqlCommand command = new SqlCommand(sqlquery, conn))
    {
        command.Parameters.Add("@MyDate", SqlDbType.Date).Value = TextBox1.Text;
        conn.Open();
        command.ExecuteNonQuery();
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda