Solved

convert integer to string in C#

Posted on 2014-09-04
3
360 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
Comment Utility
Use Convert.ToString() method.
0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
crm development 2 34
creating a flowchart from an algorithm 5 16
Image(7) 1 32
Long URL in SMS 6 21
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

6 Experts available now in Live!

Get 1:1 Help Now