Solved

convert integer to string in C#

Posted on 2014-09-04
3
366 Views
Last Modified: 2014-09-04
I'm using Visual Studio 2013 and working on a ASP.NET MVC application using C#.
My data source is a xml file inside my project.

model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NorthwindApp.Models
{

    public class Employee
    {
        // data store fields
        public const string ROOT = "Employees";
        public const string EMPLOYEE = "Employee";
        public const string EMPLOYEEID= "EmployeeID";
        public const string LASTNAME = "LastName";
        public const string FIRSTNAME = "FirstName";
        public const string TITLE = "Title";
        
        public Int32 EmployeeID { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Title { get; set; }


    }

}

Open in new window



Then in my repository I have the following functions:

        public EmployeesRepository()
        {            
            this.filePath = HttpContext.Current.Server.MapPath("~/App_Data/employees.xml");
        }

Open in new window



        public IEnumerable<Employee> GetAll()
        {
            if (!File.Exists(filePath))
            {
                return new List<Employee>();
            }

            var existingEmployees = XElement.Load(filePath);
            var employees = from employeeElement in existingEmployees.Elements(Employee.EMPLOYEE)
                       select new Employee()
                       {
                           EmployeeID = (Int32)employeeElement.Element(Employee.EMPLOYEEID),
                           LastName = (string)employeeElement.Element(Employee.LASTNAME),
                           FirstName = (string)employeeElement.Element(Employee.FIRSTNAME),
                           Title = (string)employeeElement.Element(Employee.TITLE)
                       };
            return employees;
        }

Open in new window


EmployeeID is Integer datatype.

What is the syntax to convert the EmployeeID column to a string in my GetAll function ?
0
Comment
Question by:maqskywalker
3 Comments
 
LVL 1

Expert Comment

by:Radha M
ID: 40304304
Use Convert.ToString() method.
0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
ID: 40304440
Your Employee class defines the EmployeeID as an int32. If you want to use a string, you need to change the property to a string property
public string EmployeeID { get; set; }

Open in new window

then in your GetAll Method
   EmployeeID = (string)employeeElement.Element(Employee.EMPLOYEEID),

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40304445
Hi maqskywalker;

If you must maintain the EmployeeID as an int you could override the ToString function of the class to return the string version of EmployeeID something like the following.

public class Employee
{
    // data store fields
    public const string ROOT = "Employees";
    public const string EMPLOYEE = "Employee";
    public const string EMPLOYEEID= "EmployeeID";
    public const string LASTNAME = "LastName";
    public const string FIRSTNAME = "FirstName";
    public const string TITLE = "Title";
    
    public Int32 EmployeeID { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Title { get; set; }

    public override String ToString()
    {
        return EmployeeID.ToString();
    }
}

Open in new window

Then by using the ClassInstanceName.ToString() will return the EmployeeID
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now