Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

fill web form using a windows form written in c#

Hi,

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
0
cemlouis
Asked:
cemlouis
  • 4
  • 4
1 Solution
 
vigridCommented:
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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemnetwebclientclassheaderstopic.asp

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.

HTH
0
 
Timbo87Commented:
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("http://www.site.com/post.aspx", "POST", nvc);

This will fill the values of textbox1 and textbox2 with value, then post it to site.com/post.aspx.
0
 
cemlouisAuthor Commented:
Hi Timbo87,

Thank you for your interest, anyway here is the link: http://www.stratejiparki.com/name.php[^] 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("http://www.stratejiparki.com/name.php");

HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create("http://www.stratejiparki.com/name.php");
// 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 www.stratejiparki.com/name.php 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  
 
 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
vigridCommented:
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()
            {
                  InitializeComponent();
            }

            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  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();
                  this.SuspendLayout();
                  //
                  // 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 = "http://www.stratejiparki.com/showname.php";
                  //
                  // 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.Controls.Add(this.button1);
                  this.Controls.Add(this.tbBaseUri);
                  this.Controls.Add(this.label3);
                  this.Controls.Add(this.tbValueThree);
                  this.Controls.Add(this.tbFieldThree);
                  this.Controls.Add(this.tbValueTwo);
                  this.Controls.Add(this.tbFieldTwo);
                  this.Controls.Add(this.label2);
                  this.Controls.Add(this.tbValueOne);
                  this.Controls.Add(this.label1);
                  this.Controls.Add(this.tbFieldOne);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

            }
            #endregion

            [STAThread]
            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();

                  MessageBox.Show(enc.GetString(returned));
            }
      }
}
0
 
vigridCommented:
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">
</form>

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? :)

HTH
0
 
cemlouisAuthor Commented:
Hi vigrid,

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

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

Thank you indeed,
Cem Louis
0
 
vigridCommented:
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.
0
 
cemlouisAuthor Commented:
Hi vigrid,

If you have time can you send a sample?

Thank you,
Cem Louis
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now