Solved

convert integer to string in C#

Posted on 2014-09-04
3
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Expert Comment

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

Accepted Solution

by:
Sammy Ageil 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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