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