fill web form using a windows form written in c#


As I mentioned in the subject the question is simple... I have a web form in my site and I have to periodically send data to this form by using a windows form application written in C#

Thank you,
Cem Louis
Who is Participating?

Improve company productivity with a Business Account.Sign Up

vigridConnect With a Mentor Commented:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Collections.Specialized;

namespace WindowsApplication2
      public class Form1 : System.Windows.Forms.Form
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox tbFieldOne;
            private System.Windows.Forms.TextBox tbValueOne;
            private System.Windows.Forms.TextBox tbValueTwo;
            private System.Windows.Forms.TextBox tbFieldTwo;
            private System.Windows.Forms.TextBox tbValueThree;
            private System.Windows.Forms.TextBox tbFieldThree;
            private System.Windows.Forms.TextBox tbBaseUri;

            private System.ComponentModel.Container components = null;

            public Form1()

            protected override void Dispose( bool disposing )
                  if( disposing )
                        if (components != null)
                  base.Dispose( disposing );

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
                  this.tbFieldOne = new System.Windows.Forms.TextBox();
                  this.label1 = new System.Windows.Forms.Label();
                  this.tbValueOne = new System.Windows.Forms.TextBox();
                  this.label2 = new System.Windows.Forms.Label();
                  this.tbValueTwo = new System.Windows.Forms.TextBox();
                  this.tbFieldTwo = new System.Windows.Forms.TextBox();
                  this.tbValueThree = new System.Windows.Forms.TextBox();
                  this.tbFieldThree = new System.Windows.Forms.TextBox();
                  this.label3 = new System.Windows.Forms.Label();
                  this.tbBaseUri = new System.Windows.Forms.TextBox();
                  this.button1 = new System.Windows.Forms.Button();
                  // tbFieldOne
                  this.tbFieldOne.Location = new System.Drawing.Point(8, 24);
                  this.tbFieldOne.Name = "tbFieldOne";
                  this.tbFieldOne.TabIndex = 0;
                  this.tbFieldOne.Text = "name";
                  // label1
                  this.label1.Location = new System.Drawing.Point(8, 8);
                  this.label1.Name = "label1";
                  this.label1.Size = new System.Drawing.Size(100, 16);
                  this.label1.TabIndex = 1;
                  this.label1.Text = "Field name";
                  // tbValueOne
                  this.tbValueOne.Location = new System.Drawing.Point(120, 24);
                  this.tbValueOne.Name = "tbValueOne";
                  this.tbValueOne.Size = new System.Drawing.Size(184, 20);
                  this.tbValueOne.TabIndex = 2;
                  this.tbValueOne.Text = "This is a simple text";
                  // label2
                  this.label2.Location = new System.Drawing.Point(120, 8);
                  this.label2.Name = "label2";
                  this.label2.Size = new System.Drawing.Size(184, 16);
                  this.label2.TabIndex = 3;
                  this.label2.Text = "Field value";
                  // tbValueTwo
                  this.tbValueTwo.Location = new System.Drawing.Point(120, 48);
                  this.tbValueTwo.Name = "tbValueTwo";
                  this.tbValueTwo.Size = new System.Drawing.Size(184, 20);
                  this.tbValueTwo.TabIndex = 5;
                  this.tbValueTwo.Text = "";
                  // tbFieldTwo
                  this.tbFieldTwo.Location = new System.Drawing.Point(8, 48);
                  this.tbFieldTwo.Name = "tbFieldTwo";
                  this.tbFieldTwo.TabIndex = 4;
                  this.tbFieldTwo.Text = "";
                  // tbValueThree
                  this.tbValueThree.Location = new System.Drawing.Point(120, 72);
                  this.tbValueThree.Name = "tbValueThree";
                  this.tbValueThree.Size = new System.Drawing.Size(184, 20);
                  this.tbValueThree.TabIndex = 7;
                  this.tbValueThree.Text = "";
                  // tbFieldThree
                  this.tbFieldThree.Location = new System.Drawing.Point(8, 72);
                  this.tbFieldThree.Name = "tbFieldThree";
                  this.tbFieldThree.TabIndex = 6;
                  this.tbFieldThree.Text = "";
                  // label3
                  this.label3.Location = new System.Drawing.Point(8, 104);
                  this.label3.Name = "label3";
                  this.label3.Size = new System.Drawing.Size(100, 16);
                  this.label3.TabIndex = 8;
                  this.label3.Text = "Base URI";
                  // tbBaseUri
                  this.tbBaseUri.Location = new System.Drawing.Point(8, 120);
                  this.tbBaseUri.Name = "tbBaseUri";
                  this.tbBaseUri.Size = new System.Drawing.Size(296, 20);
                  this.tbBaseUri.TabIndex = 9;
                  this.tbBaseUri.Text = "";
                  // button1
                  this.button1.Location = new System.Drawing.Point(232, 144);
                  this.button1.Name = "button1";
                  this.button1.TabIndex = 10;
                  this.button1.Text = "Submit";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  // Form1
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(312, 174);
                  this.Name = "Form1";
                  this.Text = "Form1";


            static void Main()
                  Application.Run(new Form1());

            private void button1_Click(object sender, System.EventArgs e)
                  WebClient wc = new WebClient();
                  NameValueCollection nvc = new NameValueCollection();

                  if(tbFieldOne.Text.Length != 0 && tbValueOne.Text.Length != 0)
                        nvc.Add(tbFieldOne.Text, tbValueOne.Text);
                  if(tbFieldTwo.Text.Length != 0 && tbValueTwo.Text.Length != 0)
                        nvc.Add(tbFieldTwo.Text, tbValueTwo.Text);
                  if(tbFieldThree.Text.Length != 0 && tbValueThree.Text.Length != 0)
                        nvc.Add(tbFieldThree.Text, tbValueThree.Text);

                  byte[] returned = wc.UploadValues(tbBaseUri.Text, "POST", nvc);
                  System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

You just need to post an appropiate request to the site you use.

I would design your Windows Forms application in a way that control names match the form's fields names and then extract the data using a simple enumeration in your form. You would need to enumerate all the controls that you use, check their type (if they are TextBox controls for example), then get the data they contain and build a POST data by hand.

More on the topic:

The example provided above will be useful. I would create a method that takes a NameValueCollection as an argument and converts it to byte array for POSTing.

System.Net.WebClient wc = new System.Net.WebClient();
System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
nvc.Add("textbox1", "value");
nvc.Add("textbox2", "value");
wc.UploadValues("", "POST", nvc);

This will fill the values of textbox1 and textbox2 with value, then post it to
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

cemlouisAuthor Commented:
Hi Timbo87,

Thank you for your interest, anyway here is the link:[^] When you wrote and submit something it displays what you had written... I made a windows form in C# and put a textbox, button, label on it. Then I made the button's click function like below:

private void button1_Click(object sender, System.EventArgs e)
// Download the data to a buffer.
WebClient client = new WebClient();
// Upload some form post values.
NameValueCollection form = new NameValueCollection();
form.Add("email", textBox_email.Text.ToString());
Byte[] responseData = client.UploadValues("");

HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create("");
// ask the web request for a webResponse encapsulating
// that page
HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse( );
// get the streamReader from the response
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream( ), Encoding.ASCII);
string outputString;
outputString = streamReader.ReadToEnd( );
streamReader.Close( );
label1.Text = outputString;

The thing that I want is to write a string to the textbox on windows form than press the button. The textbox's text must go the form on then the windows form takes the data on showname.php and write to the label on windows form... I mean I want to input data on windows form and get the result to the windows form also...

Thank you,
Cem Louis  
Sorry for a long post above, but it should help you. you need to point the WebClient to the link that the form uses as "action" parameter:

<form name="form1" method="post" action="showname.php">
  <input type="text" name="name">
  <input type="submit" name="Submit" value="Submit">

So, the target URI for this form is "showname.php". I provided you with a working sample that connects to the URI, posts data and then gets the source that the page returned and displays it in a MessageBox. Customize the form and URI for your needs. I hope now you get the idea? :)

cemlouisAuthor Commented:
Hi vigrid,

Thank you very much...
cemlouisAuthor Commented:
Hi vigrid,

Do you know how to retrieve the error codes? 404, 403 and the others?

Thank you indeed,
Cem Louis
You have to use the WebClient class inside of a "try" block and catch the System.Net.WebException. In this exceptiuon you will have detailed info about what happened.
cemlouisAuthor Commented:
Hi vigrid,

If you have time can you send a sample?

Thank you,
Cem Louis
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.