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 961871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:24:53+00:00 2026-05-16T01:24:53+00:00

I have a method that calls a stored procedure. It uses the employee number

  • 0

I have a method that calls a stored procedure. It uses the employee number as a parameter to retrieve the data of a particular employee and then fills the data table with the result.

protected DataTable CreateDT(string empNo)
        {
            DataTable dataTable = null;
            try
            {
                SqlCommand cmd = new SqlCommand("FIND_EMPLOYEE_BY_EMPNO", pl.ConnOpen());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@EMPNO", (object)empNo));
                SqlDataAdapter da = new SqlDataAdapter(pl.cmd);
                dataTable = new DataTable("dt");
                da.Fill(dt);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                pl.MySQLConn.Close();
            }
            return dt;
        }

What I’m trying to accomplish is convert this code to LINQ, but I don’t know how get the result and fill it to my data table. See below:

alt text http://a.imageshack.us/img706/9017/testki.jpg

protected DataTable CreateDT(string empNo)
        {
            DataTable dataTable = null;
            try
            {
                DataClasses1DataContext dataClass1 = new DataClasses1DataContext();
                // I tried to cast it to DataTable, but it doesn't work...
                dataTable = (DataTable)dataClass1.findEmployeeByID(empNo);
            }
            catch (Exception x)
            {
                MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                pl.MySQLConn.Close();
            }
            return dt;
        }

Please guide me how to properly do this… Thanks in advance..

  • 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-05-16T01:24:54+00:00Added an answer on May 16, 2026 at 1:24 am

    findEmployeeByID will most likely return IEnumerable<Employee>. Considering that you are switching to using LINQ, you should actually take advantage of strongly typed data and use it across you application. So, change the return type of CreateDT function and adjust the rest of the code accordingly(I assume that the stored procedure returns at most one result):

    protected Employee CreateDT(string empNo)
        {
    
            try
            {
                DataClasses1DataContext dataClass1 = new DataClasses1DataContext();
                // I tried to cast it to DataTable, but it doesn't work...
                return dataClass1.findEmployeeByID(empNo).FirstOrDefault();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
               //might need to dispose the context here
            }
            return null;
        }
    

    Usage:

     var employee = CreateDT(1234);
     //You can now access members of employee in a typesafe manner
     string name = employee.Name;
    

    EDIT Updated code – this is how you can rewrite the old DataTable code:

     protected void RetrieveEmployee(string empNo) { 
         Employee emp = CreateDT(empNo);// <---- Here 
         txtEmployeeNo.Text = emp.EmployeeNo;
         txtLastName.Text = emp.LastName;
         //....
     }
    

    Note the absence of array indices and late bound column specifiers – ie dt[0]["EmployeeNo"] became emp.EmployeeNo – must safer, faster and easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# application that calls a stored procedure that produces an xml
I have created a console application that calls a method on a webservice. I
I have a method that uses Entity Framework to do some changes/inserts in different
I have several listboxes that get each of their data from a separate stored
I have a method that varies by a single method call inside, and I'd
I'd like to be able to call getProgram on objects which have that method,
I have class method that returns a list of employees that I can iterate
I have a method that where I want to redirect the user back to
I have a method that can return either a single object or a collection
I have a method that takes an IQueryable. Is there a LINQ query that

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.