Search code examples
c#asp.netsqldatasourcesqldatareader

Reference data in SQLDataReader by column and rows or any alternative


I am running a stored procedure and the result is this format

+------+--------+-----+-------+
|  ID  | Resign | Sum | Count |
+------+--------+-----+-------+
| 1234 |      0 | 400 |     3 |
| 1234 |      1 | 800 |     4 |
+------+--------+-----+-------+

I tried this code to reference the values returned by the query but, it seem not working the way I want it

if (conn.State != ConnectionState.Open)
    conn.Open();
    SqlCommand sc = new SqlCommand();
    sc.CommandText = "usp_GetResignPool";
    sc.CommandType = CommandType.StoredProcedure;
    sc.Connection = conn;
    sc.Parameters.Add(AddParam(EndDate, "@EndDate"));
    sc.Parameters.Add(AddParam(am_id, "@id"));

    SqlDataReader reader;
    reader = sc.ExecuteReader();

 while (reader.Read())
            {
                if reader. // lost here
            }

How can I do something like this. ↓

int resigned = 0, resign_count = 0, not_resigned = 0, notresign_count =0;

if (read["Resigned"] == 1)
{
    resigned = read["sum"];
    resign_count = read["count"];
}
else
{
    not_resigned = read["sum"];
    notresign_count = read["count"]; 
}           

It is not important that I used SQLDataReader.

Edit: Real column names

ID        Resigned    sum                    count
--------- ----------- ---------------------- -----------

Solution

  • Would this work?

    int resign = 0;
    int not_resign = 0;
    int resign_count = 0;
    int not_resign_count = 0;
    
    while (reader.Read())
    {   
        if (Convert.ToInt32(reader["Resigned"]) == 1)
        {
            resign = Convert.ToInt32(reader["Sum"]);        
            resign_count = Convert.ToInt32(reader["Count"]);        
        }
        else
        {
            not_resign = Convert.ToInt32(reader["Sum"]);        
            not_resign_count = Convert.ToInt32(reader["Count"]);        
        } 
    }