Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey everyone.

I have a simple Silverlight web app form I created to test a web service I have set up for a bigger project. This form is basically textboxes that take First Name, Last Name, and Middle Name, and on button click, the code gets passed on to the web service on the server, which prints them in a pdf form, saves it to disk, then emails it.

I’ve written all the code, I don’t get any errors, but it’s not working. No file gets saved and no email gets sent.

Here’s the code from the MainPage.xaml.cs:
C#
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using ExampleSilverlightApp.ServiceReference1;

namespace ExampleSilverlightApp
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.newpdfRequestBody proxy = new ServiceReference1.newpdfRequestBody();
            proxy._1_FirstName = textBox1.Text;
            proxy._1_lastName = textBox2.Text;
            proxy._1_middlename = textBox3.Text;

        }
    }
}



And here’s the code from my web service:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.IO;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace ExampleSilverlightApp.Web
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MyWebService : System.Web.Services.WebService
    {
                
        [WebMethod]
        public void newpdf(string _1_FirstName, string _1_lastName, string _1_middlename)
        {

            string filename = @"C:\Temp\" + _1_FirstName + _1_lastName + ".pdf";
            iTextSharp.text.Document d = new iTextSharp.text.Document(PageSize.A4, 72, 72, 172, 72);
            PdfWriter.GetInstance(d, new FileStream(filename, FileMode.Create));

            d.Open();
            d.Add(new Paragraph(string.Format("First Name:", _1_FirstName)));
            d.Add(new Paragraph(string.Format("Last Name:", _1_lastName)));
            d.Add(new Paragraph(string.Format("Middle Name:", _1_middlename)));
            d.Close();
            try
            {
                MailAddress SendFrom = new MailAddress("t@hotmail.com");
                MailAddress SendTo = new MailAddress("tg@gmail.com");

                MailMessage MyMessage = new MailMessage(SendFrom, SendTo);
                MyMessage.Subject = "new test";
                MyMessage.Body = "heres a new test!";

                Attachment attachfile = new Attachment(filename);
                MyMessage.Attachments.Add(attachfile);

                SmtpClient emailClient = new SmtpClient("smtp.live.com");
                emailClient.Send(MyMessage);
            }

            catch (FileNotFoundException)
            {
                Console.WriteLine("File Lost!");
            }

        }
    }
}


I can’t tell what is wrong with what I have set up.
Posted

To debug the application, you can write try.. catch block in MainPage.xaml.cs

C#
private void button1_Click(object sender, RoutedEventArgs e)
       {
         try
         {
           ServiceReference1.newpdfRequestBody proxy = new ServiceReference1.newpdfRequestBody();
           proxy._1_FirstName = textBox1.Text;
           proxy._1_lastName = textBox2.Text;
           proxy._1_middlename = textBox3.Text;
         }
         catch (Exception ex)
         {....}
       }
 
Share this answer
 
I don't see anywhere that you actually call the webservice.
 
Share this answer
 
Comments
Taysseer Kadri 24-Oct-11 14:47pm    
i don't see how its not invoked? do i need to put a return statement or something?
Mark Salsbery 25-Oct-11 10:58am    
Just setting the properties of a ServiceReference1.newpdfRequestBody object invokes it? Unlikely. You should have a different proxy class that has a newpdf() method that can be called...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900