I am having some major issues with this piece of code... and I am sure there has to be a better way of going about checking for a null value and doing an else command, but thats not even the issue every time I run this and there happens to be a string in the value of lets say Companyname in the database i get a incorrect string format error which makes some sence seeing that I am parsing a string to do a boolean on an int and then it has not clue what to do with the string. Now this of course works if the value is an Int... So I am lost on how to fix this issue any help would be great.
public Company[] GetCompany()
{
con = "Data Source=MENTALIS;Initial Catalog=LiveData;Integrated Security=True";
SqlDataReader d = SqlHelper.ExecuteReader(con,CommandType.StoredProcedure,"Company_Sel");
ArrayList Alpha = new ArrayList();
while(d.Read())
{
Company al = new Company();
al.CompanyID = d.IsDBNull(Int32.Parse(d["CompanyID"].ToString())) ? "" : d["CompanyID"].ToString();
al.Companyname = d.IsDBNull(Int32.Parse(["Companyname"].ToString())) ? " " : d["Companyname"].ToString();
al.Address = d.IsDBNull(Int32.Parse(d["Address"].ToString())) ? "" : d["Address"].ToString();
//al.Address2 = d.IsDBNull(Int32.Parse(d["Address2"].ToString())) ? "" : d["Address2"].ToString();
Alpha.Add(al);
}
return (Company[])Alpha.ToArray(typeof(Company));
}
Question
wacko412
I am having some major issues with this piece of code... and I am sure there has to be a better way of going about checking for a null value and doing an else command, but thats not even the issue every time I run this and there happens to be a string in the value of lets say Companyname in the database i get a incorrect string format error which makes some sence seeing that I am parsing a string to do a boolean on an int and then it has not clue what to do with the string. Now this of course works if the value is an Int... So I am lost on how to fix this issue any help would be great.
public Company[] GetCompany() { con = "Data Source=MENTALIS;Initial Catalog=LiveData;Integrated Security=True"; SqlDataReader d = SqlHelper.ExecuteReader(con,CommandType.StoredProcedure,"Company_Sel"); ArrayList Alpha = new ArrayList(); while(d.Read()) { Company al = new Company(); al.CompanyID = d.IsDBNull(Int32.Parse(d["CompanyID"].ToString())) ? "" : d["CompanyID"].ToString(); al.Companyname = d.IsDBNull(Int32.Parse(["Companyname"].ToString())) ? " " : d["Companyname"].ToString(); al.Address = d.IsDBNull(Int32.Parse(d["Address"].ToString())) ? "" : d["Address"].ToString(); //al.Address2 = d.IsDBNull(Int32.Parse(d["Address2"].ToString())) ? "" : d["Address2"].ToString(); Alpha.Add(al); } return (Company[])Alpha.ToArray(typeof(Company)); }Link to comment
Share on other sites
14 answers to this question
Recommended Posts