Click here to Skip to main content
15,896,467 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My table have 10 récords and when i use my method only have a "0" that means only read one record because only the first row field is null another fields is not nulls

Query

SQL
string query = "SELECT * FROM Movies";


What I have tried:

using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {

                        if (string.IsNullOrEmpty(sdr["Code"].ToString()))
                        {
                            C_E = "0";
                            this.Update(C_E);
                            //ViewBag.Message = "cero";
                        }
                        else
                        {
                            C_E = sdr["Code"].ToString();
                            this.Update(C_E);
                            // ViewBag.Message = C_E;
                        }


                    }
Posted
Updated 29-Oct-18 20:34pm
Comments
F-ES Sitecore 30-Oct-18 5:41am    
The first thing you need to do is not rely on assumptions. Your assumption that you are only processing one row is probably wrong, and stopping you solving the actual problem. Use the debugger to step through the code line by line to see what it is doing. The problem is more likely to be inside the Update method.
Richard Deeming 30-Oct-18 8:07am    
REPOST
You have already posted this in the ASP.NET forum:
https://www.codeproject.com/Messages/5566568/Why-my-code-only-read-one-row.aspx[^]

1 solution

The problem is not the number of rows that are read - though without your data we can't check that, there may only be one row in the "Movies" table - but that C_E will always have the same value when you exit the loop - the last value read from the data.
I have no idea what your Update method does, but if you want to assemble all the values from your DB you need to do something like this:
C#
using (SqlDataReader sdr = cmd.ExecuteReader())
{
    StringBuilder sb = new StringBuilder();
    while (sdr.Read())
    {
        string temp = sdr["Code"].ToString();
        if (string.IsNullOrEmpty(temp))
        {
            sb.Append("0");
        }
        else
        {
            sb.Append(temp);
        }
        this.Update(temp);
    }
    C_E = sb.ToString();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900