I'm currently building a small website that takes data from 6 different web forms written in ASP.NET and C#. I need all of the information to be written to a PDF at the end of Page 6. I currently have all fields mapped, but each time the page changes, the information gets wiped. i have the information set to pass the values using a query string, but it seems to keep getting lost. Any advice?
EDIT
Sorry for not posting the code the first time, this is my first question i ever posted here. Also, I know my code isn't very efficient, I'm more or less trying to get the grasp of iTextSharp and WebForms. Thanks
Page 1
AcroFields af = ps.AcroFields;
af.SetField("Name", name.Text);
af.SetField("Email", email.Text);
af.SetField("state", state.Text);
af.SetField("city", city.Text);
af.SetField("Address", address.Text);
af.SetField("Phone", phone.Text);
af.SetField("zip", zip.Text);
Response.Redirect("Default.aspx?name=" + this.name.Text + "&Email=" + this.email.Text
+ "&state=" + this.state.Text + "&city=" + this.city.Text + "&Address=" + this.address.Text +
"&Phone=" + this.phone.Text + "&zip=" + this.zip.Text);
Response.Redirect("Page2.aspx");
Page 2
AcroFields af = ps.AcroFields;
af.SetField("Degree", degree.Text);
af.SetField("Grad", gradDate.Text);
af.SetField("Obj", objective.Text);
Response.Redirect("Default.aspx?Degree=" + this.degree.Text + "&Grad=" + this.gradDate.Text
+ "&Obj=" + this.objective.Text);
Response.Redirect("Page3.aspx");
//ps.FormFlattening = true;
Page 3
AcroFields af = ps.AcroFields;
af.SetField("jobStart1", jobStart1.Text);
af.SetField("jobEnd1", jobEnd1.Text);
af.SetField("jobTitle1", jobTitle1.Text);
af.SetField("coName1", coName1.Text);
af.SetField("coAdd1", coAdd1.Text);
af.SetField("Details1", details1.Text);
Response.Redirect("Default.aspx?jobStart1" + this.jobStart1.Text + "&jobEnd1=" + this.jobEnd1.Text
+ "&jobTitle1=" + this.jobTitle1.Text + "&coName1=" + this.coName1.Text + "&coAdd1=" + this.coAdd1.Text +
"&Details1=" + this.details1.Text);
Response.Redirect("Page4.aspx");
Page 4
AcroFields af = ps.AcroFields;
af.SetField("jobStart2", jobStart2.Text);
af.SetField("jobEnd2", jobEnd2.Text);
af.SetField("jobTitle2", jobTitle2.Text);
af.SetField("coName2", coName2.Text);
af.SetField("coAdd2", coAdd2.Text);
af.SetField("Details2", details2.Text);
Response.Redirect("Default.aspx?jobStart2" + this.jobStart2.Text + "&jobEnd2=" + this.jobEnd2.Text
+ "&jobTitle2=" + this.jobTitle2.Text + "&coName2=" + this.coName2.Text + "&coAdd2=" + this.coAdd2.Text +
"&Details2=" + this.details2.Text);
Response.Redirect("Page5.aspx");
Page 5
AcroFields af = ps.AcroFields;
af.SetField("jobStart3", jobStart3.Text);
af.SetField("jobEnd3", jobEnd3.Text);
af.SetField("jobTitle3", jobTitle3.Text);
af.SetField("coName3", coName3.Text);
af.SetField("coAdd3", coAdd3.Text);
af.SetField("Details3", details3.Text);
Response.Redirect("Default.aspx?jobStart3" + this.jobStart3.Text + "&jobEnd3=" + this.jobEnd3.Text
+ "&jobTitle3=" + this.jobTitle3.Text + "&coName3=" + this.coName3.Text + "&coAdd3=" + this.coAdd3.Text +
"&Details3=" + this.details3.Text);
Response.Redirect("Page6.aspx");
Page 6
string name = Request.QueryString["Name"];
string address = Request.QueryString["Address"];
string phone = Request.QueryString["Phone"];
string email = Request.QueryString["Email"];
string city = Request.QueryString["city"];
string state = Request.QueryString["state"];
string zip = Request.QueryString["zip"];
string degree = Request.QueryString["Degree"];
string gradDate = Request.QueryString["Grad"];
string objective = Request.QueryString["Obj"];
string jobStart1 = Request.QueryString["jobStart1"];
string jobEnd1 = Request.QueryString["jobEnd1"];
string jobTitle1 = Request.QueryString["jobTitle1"];
string coName1 = Request.QueryString["coName1"];
string coAdd1 = Request.QueryString["coAdd1"];
string details1 = Request.QueryString["Details1"];
string jobStart2 = Request.QueryString["jobStart2"];
string jobEnd2 = Request.QueryString["jobEnd2"];
string jobTitle2 = Request.QueryString["jobTitle2"];
string coName2 = Request.QueryString["coName2"];
string coAdd2 = Request.QueryString["coAdd2"];
string details2 = Request.QueryString["Details2"];
string jobStart3 = Request.QueryString["jobStart3"];
string jobEnd3 = Request.QueryString["jobEnd3"];
string jobTitle3 = Request.QueryString["jobTitle3"];
string coName3 = Request.QueryString["coName3"];
string coAdd3 = Request.QueryString["coAdd3"];
string details3 = Request.QueryString["Details3"];
AcroFields af = ps.AcroFields;
af.SetField("Name", name);
af.SetField("Email", email);
af.SetField("state", state);
af.SetField("city", city);
af.SetField("Address", address);
af.SetField("Phone", phone);
af.SetField("zip", zip);
af.SetField("Degree", degree);
af.SetField("Grad", gradDate);
af.SetField("Obj", objective);
af.SetField("jobStart1", jobStart1);
af.SetField("jobEnd1", jobEnd1);
af.SetField("jobTitle1", jobTitle1);
af.SetField("coName1", coName1);
af.SetField("coAdd1", coAdd1);
af.SetField("Details1", details1);
af.SetField("jobStart2", jobStart2);
af.SetField("jobEnd2", jobEnd2);
af.SetField("jobTitle2", jobTitle2);
af.SetField("coName2", coName2);
af.SetField("coAdd2", coAdd2);
af.SetField("Details2", details2);
af.SetField("jobStart3", jobStart3);
af.SetField("jobEnd3", jobEnd3);
af.SetField("jobTitle3", jobTitle3);
af.SetField("coName3", coName3);
af.SetField("coAdd3", coAdd3);
af.SetField("Details3", details3);
af.SetField("Skills", skills.Text);
ps.FormFlattening = true;
}
Response.End();
Response.Redirect("Default.aspx?name=" + this.name.Text + "&Email=" + this.email.Text
+ "&state=" + this.state.Text + "&city=" + this.city.Text + "&Address=" + this.address.Text +
"&Phone=" + this.phone.Text + "&zip=" + this.zip.Text);
Response.Redirect("Page2.aspx"); //this will never be reached because the line before it ends the current execution context
You're redirecting back to Default.aspx, but judging by your question the intention is to go to Page2.aspx. But Response.Redirect("Page2.aspx"); is never reached because the response ends after the first redirect. When you call response redirect (the standard overload) the current request context ends, the client is sent an HTTP redirect. So no further code will be executed.
Instead, replace both of those lines with this one:
Response.Redirect("Page2.aspx?name=" + this.name.Text + "&Email=" + this.email.Text
+ "&state=" + this.state.Text + "&city=" + this.city.Text + "&Address=" + this.address.Text +
"&Phone=" + this.phone.Text + "&zip=" + this.zip.Text);
Then on Page 2, you'll need code in Page_Load to pull the values out of the request query string, and additional code so that when you leave the page you pass the values from page 1 and the new values from page 2. And so on.
You should UriEncode your values before putting them in the query string. And concatenating a bunch of strings is really ugly. Perhaps a format string would be appropriate here.
Also, if I were you, I'd abandon the query string approach entirely. That's really messy, passing them around like that because after several pages, you're going to have a ton of parameters and it's going to be a messy URL and a lot of boilerplate code. And neither of those is good. Instead, you should create a Model to represent all the information you want to gather, then store the model somewhere such as Session to pass it between pages. On the function where you build your PDF, have it accept the model as a parameter and generate the PDF according to the values in your model.
The model is simply a C# class that represents the values your PDF needs. For example, here's a starting point:
public class ApplicantInformationModel
{
public string Name {get; set;}
public string Email {get; set;}
public List<Job> Jobs {get; set;}
public string PhoneNumber {get; set;}
}
public class Job
{
public DateTime StartDate {get; set;}
public DateTime EndDate {get; set;}
public string Title {get; set;}
public string Company {get; set;}
}
Your PDF creation function will accept this:
public static Document GeneratePdfForApplicant(ApplicantInformationModel model)
{
//use iTextSharp to generate and return PDF based on the model
}
Related
I'm writing a weather app in Xamarin.Form. I am using the Yahoo API. I have no problem getting the weather by the city name parameter. However, when I change the code to use longitude and latitude, the weather does not appear.
To download the weather I use the example from the page: https://developer.yahoo.com/weather/documentation.html#oauth-csharp
I processed it in the following way:
lSign = string.Format(
"format={0}&" +
"lat={1}&" +
"lon={2}&" +
"oauth_consumer_key={3}&" +
"oauth_nonce={4}&" +
"oauth_signature_method={5}&" +
"oauth_timestamp={6}&" +
"oauth_version={7}&" +
"u={8}",
cFormat,
szerokosc,
dlugosc,
cConsumerKey,
lNonce,
cOAuthSignMethod,
lTimes,
cOAuthVersion,
jednostka.ToString().ToLower()
(...)
url = cURL + "?lat=" + szerokosc + "&lon=" + dlugosc + "&u=" + jednostka.ToString().ToLower() + "&format=" + cFormat;
According to the documentation, lSign is used for authentication. It should not be changed, remove these "lat={1}&" + "lon={2}&" from that strings.
It says Please don't simply change value of any parameter without
re-sorting.
The location information should be involved in the request url and the authorization information is added in the header.
// Add Authorization
lClt.Headers.Add ( "Authorization", _get_auth () );
// The request URL
lURL = cURL + "?" + "lat=" + szerokosc + "&lon=" + dlugosc + "&format=" + cFormat;
Unfortunately, the simple removal of " lat = {1} & " + " lon = {2} & " from variable lSign does not solve the problem.
For example, to get weather data by the city name I use:
lSign = string.Format(
"format={0}&" +
"location={1}&" +
"oauth_consumer_key={2}&" +
"oauth_nonce={3}&" +
"oauth_signature_method={4}&" +
"oauth_timestamp={5}&" +
"oauth_version={6}&" +
"u={7}",
cFormat,
miasto,
cConsumerKey,
lNonce,
cOAuthSignMethod,
lTimes,
cOAuthVersion,
jednostka.ToString().ToLower()
and
url = cURL + "?location=" + Uri.EscapeDataString(miasto) + "&u=" + jednostka.ToString().ToLower() + "&format=" + cFormat;
and
string headerString = _get_auth();
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/" + cFormat;
webClient.Headers[HttpRequestHeader.Authorization] = headerString;
webClient.Headers.Add("X-Yahoo-App-Id", cAppID);
byte[] reponse = webClient.DownloadData(url);
string lOut = Encoding.ASCII.GetString(reponse);
I am trying to add some large string into string type varriable.But it gives an error.
string SuccessUrl = "~/Customer/Success.aspx?URL=" + Server.UrlEncode("Transactionid="
+ Transactionid + "&Amount=" + Amount + "&Name=" + Name +
"&EmailOfPayer=" + EmailOfPayer + "&bussness=" + business + "&CompanyName=" + CompanyName
+ "&PaymentDate=" + paymentDateTime +"&SecuritiesandComplianceFee=" + SecuritiesandComplianceFee
+"&Status=" + Convert.ToInt32(Status) + "&BackmyUri=" + BackmyUri);
I have an error because of the last varriable i.e. BackmyUri. The varriable have the string value as given below.
string BackmyUri = "http://localhost:11181/Payment.aspx"
it gives an error .
Input string was not in a correct format.
Any kind of help will be appreciated.
Input string was not in a correct format. is most likely the default error message for int.Parse() and `Convert.ToInt32' . You should really check that. Another helpful thing for us would be to show us an example that makes it error, show the result of:
var x = "Transactionid=" + Transactionid + "&Amount=" + Amount + "&Name=" + Name +
"&EmailOfPayer=" + EmailOfPayer + "&bussness=" + business + "&CompanyName=" + CompanyName
+ "&PaymentDate=" + paymentDateTime +"&SecuritiesandComplianceFee=" + SecuritiesandComplianceFee
+"&Status=" + Convert.ToInt32(Status) + "&BackmyUri=" + BackmyUri
I am trying to replace a hash char in a string but the following is not working the
string address = "Blk 344, Jurong West, Street 11, #02-111";
address.Replace("#","%23");
Any ideas guys been driving me crazy
Query String full
http://localhost:54965/SKATEZ/thankyou.aspx?firstname=Fiora&lastname=Ray&address=Blk%20344,%20Jurong%20West,%20Street%2011,%20#02-111&total=22&nirc=S6799954H&country=Singapore&orderid=85&postalcode=746112
I construct the url as follows
string url = "thankyou.aspx?firstname=" + firstname + "&" + "lastname=" + lastname + "&" + "address=" + HttpUtility.EscapeDataString(address) + "&" + "total=" + total + "&" + "nirc=" + tbID.Text + "&" + "country=" + ddlCountry.SelectedValue + "&" + "orderid=" + orderid + "&" + "postalcode=" + tbPostalCode.Text;
Response.Redirect(url);
Try
address = address.Replace("#","%23");
Strings in C# are immutable:
Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear as if you can do this. For example, when you write this code, the compiler actually creates a new string object to hold the new sequence of characters, and that new object is assigned to b. The string "h" is then eligible for garbage collection.
Using System.Uri.EscapeDataString(string) should fix your issue:
var urlbuilder = new StringBuilder();
urlbuilder.AppendFormat("thankyou.aspx?firstname={0}", firstname);
urlbuilder.AppendFormat("&lastname={0}", lastname);
urlbuilder.AppendFormat("&address={0}", System.Uri.EscapeDataString(address));
urlbuilder.AppendFormat("&total={0}", total);
urlbuilder.AppendFormat("&nirc={0}", tbID.Text);
urlbuilder.AppendFormat("&country={0}", ddlCountry.SelectedValue);
urlbuilder.AppendFormat("&orderid={0}", orderid);
urlbuilder.AppendFormat("&postalcode={0}", tbPostalCode.Text);
Response.Redirect(urlbuilder.ToString());
(using System.Text.StringBuilder to compose your url makes the code a little more readable)
I'm new in google analytic. I go through some regarding this. I found that there is no direct method to hit a windows application in google analytic. But i found some solutions in stackoverflow. I tried that, but didn't work for me. Below is the code that I'm using.
private void analyticsmethod4(string trackingId, string pagename)
{
Random rnd = new Random();
long timestampFirstRun, timestampLastRun, timestampCurrentRun, numberOfRuns;
// Get the first run time
timestampFirstRun = DateTime.Now.Ticks;
timestampLastRun = DateTime.Now.Ticks - 5;
timestampCurrentRun = 45;
numberOfRuns = 2;
// Some values we need
string domainHash = "123456789"; // This can be calcualted for your domain online
int uniqueVisitorId = rnd.Next(100000000, 999999999); // Random
string source = "Shop";
string medium = "medium123";
string sessionNumber = "1";
string campaignNumber = "1";
string culture = Thread.CurrentThread.CurrentCulture.Name;
string screenRes = Screen.PrimaryScreen.Bounds.Width + "x" + Screen.PrimaryScreen.Bounds.Height;
string statsRequest = "http://www.google-analytics.com/__utm.gif" +
"?utmwv=4.6.5" +
"&utmn=" + rnd.Next(100000000, 999999999) +
// "&utmhn=hostname.mydomain.com" +
"&utmcs=-" +
"&utmsr=" + screenRes +
"&utmsc=-" +
"&utmul=" + culture +
"&utmje=-" +
"&utmfl=-" +
"&utmdt=" + pagename + // Here i passed my profile name "MyWindowsApp"
"&utmhid=1943799692" +
"&utmr=0" +
"&utmp=" + pagename +
"&utmac=" + trackingId + //Tracking id : ie "UA-XXXXXXXX-X"
"&utmcc=" +
"__utma%3D" + domainHash + "." + uniqueVisitorId + "." + timestampFirstRun + "." + timestampLastRun + "." + timestampCurrentRun + "." + numberOfRuns +
"%3B%2B__utmz%3D" + domainHash + "." + timestampCurrentRun + "." + sessionNumber + "." + campaignNumber + ".utmcsr%3D" + source + "%7Cutmccn%3D(" + medium + ")%7Cutmcmd%3D" + medium + "%7Cutmcct%3D%2Fd31AaOM%3B";
try
{
using (var client = new WebClient())
{
//byte[] bt = client.DownloadData(statsRequest);
Stream data = client.OpenRead(statsRequest);
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd();
MessageBox.Show(s);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This example is also got from this site itself. I don't know where was the problem. Please direct me, how can i make it. This is the output i'm getting "GIF89a".
Thanks
Bobbin Paulose
So it's working. The Google Analytics call loads a tiny GIF image, and the querystring parameters provided in the request trigger all the Google Analytics goodness. If you're getting a response back, you have registered your event successfully with Google.
I have generated the the vCard from asp.net + c# application. While ending up. browsers pops up for "open with /Save as" box. I don't want to appear this box. rather than that , I want to directly set the generated .vcf file to open with outlook 2007 or 03. what hae to do ?
My code is:
S
ystem.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//vCard Begin
stringWrite.WriteLine("BEGIN:VCARD");
stringWrite.WriteLine("VERSION:2.1");
//Name
stringWrite.WriteLine("N:" + nameLast + ";" + nameFirst +
";" + nameMiddle + ";" + nameTitle);
//Full Name
stringWrite.WriteLine("FN:" + nameFirst + " " +
nameMiddle + " " + nameLast);
//Organisation
stringWrite.WriteLine("ORG:" + company + ";" + department);
//URL
stringWrite.WriteLine("URL;WORK:" + uRL);
//Title
stringWrite.WriteLine("TITLE:" + title);
//Profession
stringWrite.WriteLine("ROLE:" + profession);
//Telephone
stringWrite.WriteLine("TEL;WORK;VOICE:" + telephone);
//Fax
stringWrite.WriteLine("TEL;WORK;FAX:" + fax);
//Mobile
stringWrite.WriteLine("TEL;CELL;VOICE:" + mobile);
//Email
stringWrite.WriteLine("EMAIL;PREF;INTERNET:" + email);
//Address
stringWrite.WriteLine("ADR;WORK;ENCODING=QUOTED-PRINTABLE:" + ";" +
office + ";" + addressTitle + "=0D" +
streetName + ";" + city + ";" +
region +
";" + postCode + ";" + country);
//Revision Date
//Not needed
//stringWrite.WriteLine("REV:" + DateTime.Today.Year.ToString() +
// DateTime.Today.Month.ToString() + DateTime.Today.Day.ToString() + "T" +
// DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() +
// DateTime.Now.Second.ToString() + "Z");
//vCard End
stringWrite.WriteLine("END:VCARD");
response.Write(stringWrite.ToString());
response.AppendHeader("Hi", "PMTS");
response.End();
If I'm understanding you correctly, the issue is that you're getting a run-or-download dialog when you mean for the vCard to simply open from the web.
Assuming that is actually what you're wanting, I believe you need only to set your response to one of the vCard MIME types (text/x-vcard, text/directory;profile=vCard, or text/directory).
Response.ContentType = "text/x-vcard";
I hope that helps.
- EDIT -
Using the following code, I am properly prompted to Open or Save (and Open opens the file in Outlook) in Internet Exploder. Unfortunately, Chrome still does not support opening files it seems, and so there is a download box somewhat permanently it seems. Try the following code out in IE and you'll see what I mean; it works. Also, on a side note - I would have been able to replicate your code somewhat easier if properly formatted. Any chance you can edit your post, highlight the code, and hit the "101010" icon? Thanks much, and good luck!
using System;
using System.IO;
using System.Web.UI;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
private string nameLast = "May";
private string nameFirst = "Lance";
private string nameMiddle = "R.";
private string nameTitle = "Mr.";
private string company = "CoreLogic";
private string department = "Development";
private string uRL = "http://www.lancemay.com";
private string title = "Application Developer Senior";
private string profession = "Developer";
private string telephone = "(123) 555-1212";
private string fax = "(321) 555-1212";
private string mobile = "(555) 555-1212";
private string email = "lancemay#gmail.com";
private string office = "Louisville";
private string addressTitle = "";
private string streetName = "123 Easy St.";
private string city = "Louisville";
private string region = "KY";
private string postCode = "40223";
private string country = "US";
protected void Page_Load(object sender, EventArgs e)
{
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//vCard Begin
stringWrite.WriteLine("BEGIN:VCARD");
stringWrite.WriteLine("VERSION:2.1");
//Name
stringWrite.WriteLine("N:" + nameLast + ";" + nameFirst + ";" + nameMiddle + ";" + nameTitle);
//Full Name
stringWrite.WriteLine("FN:" + nameFirst + " " + nameMiddle + " " + nameLast);
//Organisation
stringWrite.WriteLine("ORG:" + company + ";" + department);
//URL
stringWrite.WriteLine("URL;WORK:" + uRL);
//Title
stringWrite.WriteLine("TITLE:" + title);
//Profession
stringWrite.WriteLine("ROLE:" + profession);
//Telephone
stringWrite.WriteLine("TEL;WORK;VOICE:" + telephone);
//Fax
stringWrite.WriteLine("TEL;WORK;FAX:" + fax);
//Mobile
stringWrite.WriteLine("TEL;CELL;VOICE:" + mobile);
//Email
stringWrite.WriteLine("EMAIL;PREF;INTERNET:" + email);
//Address
stringWrite.WriteLine("ADR;WORK;ENCODING=QUOTED-PRINTABLE:" + ";" + office + ";" + addressTitle + "=0D" + streetName + ";" + city + ";" + region + ";" + postCode + ";" + country);
stringWrite.WriteLine("END:VCARD");
Response.ContentType = "text/x-vcard";
Response.Write(stringWrite.ToString());
Response.AppendHeader("Hi", "PMTS");
Response.End();
}
}
}