0

fees_structure has 24 "A" Category rows. But when I try to fetch them and print, it only displays 20 or (21 rows sometimes)

Here is my code:

string catA = "SELECT id,fees_name,amount,fee_category FROM fees_structure where fee_category='A' ORDER BY id";

using (SqlCommand scom = new SqlCommand(catA, con))
{
    using (SqlDataReader read = scom.ExecuteReader())
    {
        read.Read();

        if (read.HasRows)
        {
            while (read.Read())
            {
                feestableA.Append("<tr>");
                feestableA.Append("<td>" + read["fees_name"] + "</td>");
                feestableA.Append("<td>" + read["amount"] + "</td>");
                feestableA.Append("</tr>");
            }

            plcfeesA.Controls.Add(new Literal { Text = feestableA.ToString() });
            plcfeesA.Dispose();
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

2

The read() before the while is suspicious. I suspect that is eating one row.

Another possibility for losing rows would be case-sensitive collations -- if the category could be 'a' (or a variant in another encoding). However, this depends on the default or explicit collations used for the column, database, and server.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

I dont think this is a C# issue.

Try this

string catA = "SELECT id,fees_name,amount,fee_category FROM fees_structure where fee_category like '%A%' ORDER BY id";

Just see if your results change

0

Try this. Watch closely, I removed lines of code that were doing stuff you did not want to be done.

string catA = "SELECT id,fees_name,amount,fee_category FROM fees_structure where fee_category='A' ORDER BY id";

using (SqlCommand scom = new SqlCommand(catA, con))
{
    using (SqlDataReader read = scom.ExecuteReader())
    {
        while (read.Read())
        {
            feestableA.Append("<tr>");
            feestableA.Append("<td>" + read["fees_name"] + "</td>");
            feestableA.Append("<td>" + read["amount"] + "</td>");
            feestableA.Append("</tr>");
        }

        plcfeesA.Controls.Add(new Literal { Text = feestableA.ToString() });
    }
}

}

nvoigt
  • 75,013
  • 26
  • 93
  • 142