Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:01:22+00:00 2026-06-10T14:01:22+00:00

i have a table empinf fields Empid int EmpName Nvarchar Salary Numeric i have

  • 0

i have a table empinf
fields

Empid int
EmpName Nvarchar
Salary Numeric

i have a EmployeeUtility class which contains a Method Getdetails

public EmployeeDetails Getdetails(int employeeId)
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SelectEmployee", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@Id",SqlDbType.Int,4));
        cmd.Parameters["@Id"].Value=employeeId;


            try
               {
        con.Open();

        SqlDataReader reader = cmd.ExecuteReader();
        if (!reader.HasRows) return null;
        reader.Read();
        EmployeeDetails emp = new EmployeeDetails((int)reader["EmpId"], (string)reader["EmpName"], (int)reader["Salary"]);
        reader.Close();
        return emp;
    }
    catch (SqlException e)
    {
        throw new ApplicationException("Data error.");

    }
    finally
    {
        con.Close();
    }


    } 


EmployeeDetails emp=new EmployeeDetails((int)reader["EmpId"],(string)reader["EmpName"],(int)reader["Salary"]);

it shows an error in this line indicating Specified cast is not valid. please need assistance in it.

my employeeDetails class is as follows:

public class EmployeeDetails
{
    private int employeeID;
    public int EmployeeID
    {
        get { return employeeID; }
        set { employeeID = value; }
    }

    private string employeeName;
    public string EmployeeName
    {
        get { return employeeName; }
        set { employeeName = value; }
    }

    private int salary;
    public int Salary
    {
        get { return salary; }
        set { salary = value; }
    }

    public EmployeeDetails(int employeeId, string employeeName, int salary)
    {
        EmployeeID = employeeID;
        EmployeeName = employeeName;
        Salary = salary;
    }

    public EmployeeDetails()
    {
        // TODO: Complete member initialization
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:01:24+00:00Added an answer on June 10, 2026 at 2:01 pm

    Try this:

    EmployeeDetails emp=new EmployeeDetails(Convert.ToInt32(reader["EmpId"]),Convert.ToString(reader["EmpName"]),Convert.ToInt32(reader["Salary"]));
    
    //this assumes that you don't have NULL values in any of those columns, because if you do, it will fail.  It would be more safe to check for DBNull.Value before trying to Cast these values.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

two tables are there EmpInf EmpId, EmpName, Salary, DepartNumber. Dept DeptNo, Deptname, i have
I have two tables EmpInf EmpId, EmpName, Salary, DepartNumber. Dept DeptNo, Deptname, I also
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have table in which I am inserting rows for employee but next time
I have table A with fields user1 and user2 and table B with user3
I have table view which is populated from an array grabbed from a url.
I have table of products, and there are 2 fields: price, currency. Possible values
I have table that contains more than 12 millions of rows. I need to
I have table view which is showing list of files to download. When I
I have table with 2 columns: id and name. need trigger which at change

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.