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.
returnan Exception... Youthrowthem. See MSDN.Secondly, have you checked if your queries return valid values? You are not checking on anything. See MSDN.