Solved

convert integer to string in C#

Posted on 2014-09-04
3
372 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 63

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 AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

831 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