Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static List<spectrumitem> GetSpectrumBandStartValue(SqlConnection nodeDbCon, uint Starttime)
        {
            List<spectrumitem> SpectList = new List<spectrumitem>();
         
                int cid = 0;
                uint st = 0;
                int pid = 0;
                int bs = 0;
                int tresh = 0;
                short dwl = 0;
                short corl = 0;
                int sp = 0;
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = nodeDbCon;
                cmd.Parameters.Add("@StartTime", SqlDbType.Int).Value = Starttime;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "dbo.usp_GetSpectrumBandStart";
                SpectrumItem SpectrumItem = new SpectrumItem(st, pid, bs, tresh, dwl, corl, sp,cid);
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                foreach (DataRow cdr in dt.Rows)
                {
                    st = Convert.ToUInt32(cdr[0]);
                    bs = Convert.ToInt32(cdr[1]);
                    pid = Convert.ToInt32(cdr[2]);
                    cid = Convert.ToInt32(cdr[3]);
                    SpectList.Add(SpectrumItem);
                }
             
            return SpectList;
                
        }

When i execute the code everything runs good,list item count is 230.. but all list values of 230 is 0.. How to add the values to list.. Please help me
Posted
Updated 10-Feb-15 0:03am
v2

The problem is with/around your loop...
What you do is:
1. Create a variable named SpectrumItem (outside the loop)
2. Run a loop, in which you update several variables and add the SpectrumItem variable to the list (the VERY SAME each time)

What you have to do is:
1. Run a loop, and add a newly created SpectrumItem in each iteration...
C#
foreach (DataRow cdr in dt.Rows)
{
  st = Convert.ToUInt32(cdr[0]);
  bs = Convert.ToInt32(cdr[1]);
  pid = Convert.ToInt32(cdr[2]);
  cid = Convert.ToInt32(cdr[3]);

  SpectrumItem SpectrumItem = new SpectrumItem(st, pid, bs, tresh, dwl, corl, sp,cid);

  SpectList.Add(SpectrumItem);
}
 
Share this answer
 
Um.
Why are you adding the same item each time?
You create one instance of a SpectrumItem:
C#
SpectrumItem SpectrumItem = new SpectrumItem(st, pid, bs, tresh, dwl, corl, sp,cid);
with all zero values, and then you add it repeatedly without changing it:
C#
foreach (DataRow cdr in dt.Rows)
{
    st = Convert.ToUInt32(cdr[0]);
    bs = Convert.ToInt32(cdr[1]);
    pid = Convert.ToInt32(cdr[2]);
    cid = Convert.ToInt32(cdr[3]);
    SpectList.Add(SpectrumItem);
}
I suspect that what you actually want to do is this:
C#
foreach (DataRow cdr in dt.Rows)
{
    st = Convert.ToUInt32(cdr[0]);
    bs = Convert.ToInt32(cdr[1]);
    pid = Convert.ToInt32(cdr[2]);
    cid = Convert.ToInt32(cdr[3]);
    SpectrumItem SpectrumItem = new SpectrumItem(st, pid, bs, tresh, dwl, corl, sp,cid);
    SpectList.Add(SpectrumItem);
}
And remove the earlier instance completely.
 
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