Retrieving data from a POST method in ASP.NET

104,189

Solution 1

You can get a form value posted to a page using code similiar to this (C#) -

string formValue;
if (!string.IsNullOrEmpty(Request.Form["txtFormValue"]))
{
  formValue= Request.Form["txtFormValue"];
}

or this (VB)

Dim formValue As String
If Not String.IsNullOrEmpty(Request.Form("txtFormValue")) Then
    formValue = Request.Form("txtFormValue")
End If

Once you have the values you need you can then construct a SQL statement and and write the data to a database.

Solution 2

The data from the request (content, inputs, files, querystring values) is all on this object HttpContext.Current.Request
To read the posted content

StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream);
string requestFromPost = reader.ReadToEnd();

To navigate through the all inputs

foreach (string key in HttpContext.Current.Request.Form.AllKeys)
{
   string value = HttpContext.Current.Request.Form[key];
}
Share:
104,189
Etienne
Author by

Etienne

Updated on December 13, 2020

Comments

  • Etienne
    Etienne over 3 years

    I am using ASP.NET.

    There is a system that needs to POST data to my site and all they asked for is for me to provide them with a URL. So I gave them my URL http://www.example.com/Test.aspx.

    Now I do not know exactly how they POST it but now on my Test.aspx page I need to write code that will save that data to a database.

    But how would this work and what must I do on my Test.aspx page?

    I wrote some code in my Page Load Event that sends me an email on Page Load to see if they actually hit the page and it does not seem like they are even?

  • Etienne
    Etienne over 12 years
    So txtFormValue will be one of the parameters that gets passed to my URL?
  • ipr101
    ipr101 over 12 years
    Yes it will - you'd need to adjust the names of the of the values you were checking for in the Request.Form collection depending on what was being posted to you.
  • Etienne
    Etienne over 12 years
    But must I place this code in my PAGE LOAD event? Does it even load the page when a POST to that URL occurs?
  • ipr101
    ipr101 over 12 years
    PAGE LOAD would be a good place to put the code as that event should fire when data is posted to the page. You could test a post to your page using a tool such as Fiddler.
  • Kasaku
    Kasaku over 12 years
    @Etienne, all steps of the page lifecycle (Of which Load is one) are called regardless of GET or POST. Some additional ones are also invoked on POST. Would strongly suggest you read the ASP.NET Page Life Cycle Overview to understand how ASP.NET works before progressing much further.
  • Etienne
    Etienne over 12 years
    @PirateKitten, is there a server or web.config setting I need to set to allow POST to my website/page?
  • Kasaku
    Kasaku over 12 years
    There isn't. I would echo @TJHeuvel above and strongly suggest you buy an ASP.NET book to read up on the subject, because by the nature of your questions there will be too much to cover in comments.
  • Sunil Kumar
    Sunil Kumar almost 6 years
    Nice one, can we map it with an entity(table) in entity framework??
  • Adrian Iftode
    Adrian Iftode almost 6 years
    Maybe try MVC or Web Api to do the job? :D
  • Sunil Kumar
    Sunil Kumar almost 6 years
    I am using Web API but don't know how to map entity?