Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:03:31+00:00 2026-06-07T02:03:31+00:00

I have a wpf staff creation window in which I can create basic information

  • 0

I have a wpf staff creation window in which I can create basic information like first name, last name etc this creates the staff in my REST web service. An example:

Client side:

    private void CreateStaffMember_Click(object sender, RoutedEventArgs e)
    {
        string uri = "http://localhost:8001/Service/Staff";
        StringBuilder sb = new StringBuilder();
        sb.Append("<Staff>");
        sb.AppendLine("<FirstName>" + this.textBox1.Text + "</FirstName>");
        sb.AppendLine("<LastName>" + this.textBox2.Text + "</LastName>");
        sb.AppendLine("<Password>" + this.passwordBox1.Password + "</Password>");
        sb.AppendLine("</Staff>");
        string NewStudent = sb.ToString();
        byte[] arr = Encoding.UTF8.GetBytes(NewStudent);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        MessageBox.Show("Staff Creation: Status " + resp.StatusDescription);
        reqStrm.Close();
        resp.Close();
    }

Web Service side:

    #region POST

    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Staff")]
    void AddStaff(Staff staff);

    #endregion

    public void AddStaff(Staff staff)
    {
        staff.StaffID = (++eCount).ToString();
        staff.Salt = GenerateSalt();
        byte[] passwordHash = Hash(staff.Password, staff.Salt);
        staff.Password = Convert.ToBase64String(passwordHash);
        staffmembers.Add(staff);
    }

All fine on that side, but Im looking to “import” the staff details from an excel spreadsheet, not sure if import is the correct word but I want to take the first names and last names contained in such n such spreadsheet and add them to the web service from the client side wpf application.

How would I go about it? I have my open file dialog:

    private void Import_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Show open file dialog box
        Nullable<bool> result = dlg.ShowDialog();

        // Process open file dialog box results
        if (result == true)
        {
            // Open document
            string filename = dlg.FileName;
        }
    }

So I open my excel spread sheet then how would I go about taking the inner contents and sending it to the web service? Really stuck on the code or how to go about it :/

Just looking for an automated way of adding staff members rather than manually typing the names, but seeing as the staff excel doc could be named anything I wanted the open file dialog box. The structure inside will always be the same first name then last name.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:03:33+00:00Added an answer on June 7, 2026 at 2:03 am

    First, here is my test Excel file that contains the Staff you want to import:
    enter image description here

    (Column ‘A’ if first name, column ‘B’ is last name and column ‘C’ is the password…)

    Ok, so assuming that your code calling your web service works, here is my version of the Import_Click method (and a generic method to save new staff):

        private void Import_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
    
            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();
    
            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
    
                Microsoft.Office.Interop.Excel.Application vExcelObj = new Microsoft.Office.Interop.Excel.Application();
                try
                {
                    Workbook theWorkbook = vExcelObj.Workbooks.Open(filename, Type.Missing, true);
    
                    Worksheet sheet = theWorkbook.Worksheets[1];  // This is assuming that the list of staff is in the first worksheet
    
                    string vFirstName = "temp";
                    string vLastName = "temp";
                    string vPassword = "temp";
                    int vIndex = 1;
    
                    while (vFirstName != "")
                    {
                        // Change the letters of the appropriate columns here!  
                        // In my example, 'A' is first name, 'B' is last name and 'C' is the password
                        vFirstName = sheet.get_Range("A" + vIndex.ToString()).Value.ToString();
                        vLastName = sheet.get_Range("B" + vIndex.ToString()).Value.ToString();
                        vPassword = sheet.get_Range("C" + vIndex.ToString()).Value.ToString();
    
                        this.SaveNewStaff(vFirstName, vLastName, vPassword);
    
                        vIndex++;
    
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error processing excel file : " + ex.Message);
                }
                finally {
                    vExcelObj.Quit();
                }
            }
        }
    
        private void SaveNewStaff(string firstName, string lastName, string password) {
            string uri = "http://localhost:8001/Service/Staff";
            StringBuilder sb = new StringBuilder();
            sb.Append("<Staff>");
            sb.AppendLine("<FirstName>" + firstName + "</FirstName>");
            sb.AppendLine("<LastName>" + lastName + "</LastName>");
            sb.AppendLine("<Password>" + password + "</Password>");
            sb.AppendLine("</Staff>");
            string NewStudent = sb.ToString();
            byte[] arr = Encoding.UTF8.GetBytes(NewStudent);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            req.Method = "POST";
            req.ContentType = "application/xml";
            req.ContentLength = arr.Length;
            Stream reqStrm = req.GetRequestStream();
            reqStrm.Write(arr, 0, arr.Length);
            reqStrm.Close();
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            //MessageBox.Show("Staff Creation: Status " + resp.StatusDescription);
            reqStrm.Close();
            resp.Close();
        }
    

    Note: I have REMed out the MessageBox in the call to the web service to make sure you are not annoyed by it if the list is long, but you are free to “unREM” it if you need confirmation for every staff creation. In the same line of taught, there is not validation that the creation has occurred successfully. I would need more details to create a decent validation process.
    Also VERY important, this does not validate if the staff you are saving already exists in the list. If you re-run this import procedure multiple times, it may (and probably will) create duplicate entries.

    Cheers

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a datagrid in WPF in which I can drag and drop stuff
I have WPF Form which has many buttons with the same code. Appearance of
I have a WPF application which connects to a remote database over internet and
I have a WPF TabControl which displays UserControl s. Some UserControl s are larger
I have an action Edit in my WPF application, which is bound to items
I have two windows in my WPF app: a login window and an options
I have a WPF UserControl that I'd like to inject dependencies into. What's the
In my WPF app I have many images of books which i want to
Background: We have a ClickOnce-deployed WPF app, that talks to WCF Services, which in
I am using WPF with model-view-viewmodel pattern. I have a ResultsView and ResultsViewModel which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.