Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

fill web form using a windows form written in c#

Posted on 2004-09-18
9
537 Views
Last Modified: 2010-04-15
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
Comment
Question by:cemlouis
  • 4
  • 4
9 Comments
 
LVL 4

Expert Comment

by:vigrid
ID: 12091128
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
 
LVL 15

Expert Comment

by:Timbo87
ID: 12092319
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
 

Author Comment

by:cemlouis
ID: 12093079
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 4

Accepted Solution

by:
vigrid earned 125 total points
ID: 12094805
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
 
LVL 4

Expert Comment

by:vigrid
ID: 12094816
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
 

Author Comment

by:cemlouis
ID: 12094896
Hi vigrid,

Thank you very much...
0
 

Author Comment

by:cemlouis
ID: 12137396
Hi vigrid,

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

Thank you indeed,
Cem Louis
0
 
LVL 4

Expert Comment

by:vigrid
ID: 12141213
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
 

Author Comment

by:cemlouis
ID: 12142120
Hi vigrid,

If you have time can you send a sample?

Thank you,
Cem Louis
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question