Solved

convert integer to string in C#

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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