c# - 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 trying access function getsaledetails
public exception getsaledetails(list<sellstatement> lss1,string query) { try { (int = 0; < lss1.toarray().length; i++) { query = "select * [product details] [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 atlss1[i].productname.add(sdr.getstring(1));
.i think error might because of null value in @ sdr.getstring(1)
checked has value .my friend told me can't change function argument value try copy 1 list other .
list<sellstatement> lss1 = new list<sellstatement>() ; lss1.addrange(lss);
but doesn't me out. not able figure out what's wrong while adding element.
if showed complete sellstatement
class in question, reason clear:
never initialized productname
, quantity
, rateperquantity
. null
, that's exception telling you.
to fix it, change class 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 goes against normal c# design guidelines shouldn't have public fields. consider redesigning class 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 { { return _productname; } } public list<double> quantity { { return _quantity; } } public list<double> rateperquantity { { return _rateperquantity; } } }
Comments
Post a Comment