Adding list in list

I have class SellStatement

public class SellStatement
{
    public long billNo
    public DateTime paymentDate;
    public List<string>  ProductName;
    public List<double> quantity;
    public List<double> ratePerQuantity;
}

When i am trying to access function GetSaleDetails

public Exception GetSaleDetails(List<SellStatement> lss1,string query)
    {
        try
        {
            for (int i = 0; i < lss1.ToArray().Length; i++)
            {
                query = "select * from [product details] where [bill no]=@billno";
                com = new SqlCeCommand(query, con);
                con.Open();
                com.Parameters.AddWithValue("@billno",lss1[i].billNo);
                sdr = com.ExecuteReader();
                while (sdr.Read())
                {
                    lss1[i].ProductName.Add(sdr.GetString(1));//Exception line
                    lss1[i].quantity.Add(sdr.GetDouble(2));
                    lss1[i].ratePerQuantity.Add(sdr.GetDouble(3));       
                }
            }
            con.Close();
            return null;
        }
        catch (Exception e)
        {
            con.Close();
            return e;
        }
    }

Null Reference Exception comes up atlss1[i].ProductName.Add(sdr.GetString(1));.I think error might be because of null value in at sdr.GetString(1) but i checked it has some value .My friend told me that you can't change Function argument value like this so i try to copy one list to other like this .

 List<SellStatement> lss1 = new List<SellStatement>() ;
            lss1.AddRange(lss);

But it doesn't help me out. I am not able to figure out what's wrong while adding element.

Answers


If you showed us your complete SellStatement class in the question, then the reason is clear: You never initialized ProductName, quantity and ratePerQuantity. They are null and that's exactly what the exception is telling you.

To fix it, change your class to this:

public class SellStatement
{
    public long billNo
    public DateTime paymentDate;
    public List<string> ProductName = new List<string>();
    public List<double> quantity = new List<double>();
    public List<double> ratePerQuantity = new List<double>();
}

Please note that this goes against the normal C# design guidelines that say you shouldn't have public fields. Consider redesigning your class like so:

public class SellStatement
{
    List<string> _productName = new List<string>();
    List<double> _quantity = new List<double>();
    List<double> _ratePerQuantity = new List<double>();

    public long billNo {get; set;}
    public DateTime paymentDate  {get; set;}
    public List<string> ProductName { get { return _productName; } }
    public List<double> quantity { get { return _quantity; } }
    public List<double> ratePerQuantity { get { return _ratePerQuantity; } }
}

Need Your Help

How to remove not installed application on Android

android

Before I release new version of app I always test upgrade process on device. What I do is I uninstall application I use to debug, go to market, download old version. Then install new APK using adb ...

Correct usage of scipy.interpolate.RegularGridInterpolator

python numpy scipy interpolation

I am a little confused by the documentation for scipy.interpolate.RegularGridInterpolator.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.