Solved

fill web form using a windows form written in c#

Posted on 2004-09-18
9
542 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

691 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