1

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();

        }

    }
1
  • 1
    There is no control with the "input" id/name inside your form tag. Commented Jan 28, 2022 at 20:21

1 Answer 1

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.