0

I got 8 rows with column names from stored procedure. I read this as DataTable. However, I only want three columns with reference to employeeID.

This is the table I get after I run the procedure.

stored_procedure

I read this as data table. However, I want to select three rows only and stored these three rows in datatable. The logic is if EmployeeID == "1A-111" and "1A-113" and "1A-114" then get three rows based on that and put it back to datatable. I am not sure how to do that.

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Button = System.Windows.Forms.Button;
 
namespace CrossCrackApp
{
    public partial class Form1 : Form
    {
        public object DBManager { get; private set; }
 
        public string strConn = ConfigurationManager.AppSettings["CIM.ReportDataDB"];
        
        public int thresh_val = Convert.ToInt32(ConfigurationManager.AppSettings["thresh_val"]);
       
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            display_cross_crack();             
            alarm_trigger();                  
            Show_Only_Respective_Lbl_Btn();   
            timer();                        
            lbl_timer();
            Console.WriteLine(thresh_val);
        }
 
        // This is the stored procedure 1 called load_ooc()
        private DataTable load_ooc()
        {
            // stored procedure
            SqlConnection _con = new SqlConnection(strConn);
            DataTable rdr = new DataTable();
 
            try 
            {
                _con.Open();
                lbl_error.Text = (_con is null) ? "Database not Connected" : "Database Connected";

                // create command
                SqlCommand cmd = _con.CreateCommand();
 
                // specify stored procedure to execute
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "LastHourCrossCrack";
 
                // execute command
                rdr.Load(cmd.ExecuteReader());
 
                // This is where the table is read I think so
                // Need To DO
                // Select the rows referenced to ID column
                // Then put it back to DataTable for the selected rows
                // return rdr now but need to change back to DataTable that only has selected rows
 
                // Trial & Errors
                // rdr.Select("")
                // if { rdr.Columns }
            }
            catch(Exception ex)
            {
                lbl_error.Text = ex.Message.ToString().Substring(0,100);
 
                lbl_error.ForeColor = Color.Red;
            }
            finally
            {
                if (_con == null)
                {
                    Console.WriteLine("Connection To Database is not succesful.");
                }
                
                _con.Dispose();
            }

            return rdr;
        }
    }
}

Please suggest what I could do. Thank you.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
MyatKaung
  • 9
  • 2

1 Answers1

0

You can use DataView.

DataView dv = new DataView(yourDatatable);
dv.RowFilter = "query"; // query example: "EmployeeID = '1A-111' or EmployeeID = '1A-113' or EmployeeID = '1A-114'"

How to apply filter to DataView with Multiple "AND" conditions

MikeR
  • 51
  • 3