c# - Gridview binding not working -


i trying bind gridview control following code. getting error "data null. method or property cannot called on null values." error line " p.materialname = reader.getstring(reader.getordinal("materialname"));" have data in fields in materials table. wrong code? please me fix it.

public static list<product> getmaterials() {     sqlhelper objsqlhelper = new sqlhelper();     sqldatareader reader = objsqlhelper.executereader("getmaterials");     list<product> objmaterials = new list<product>();     product p = new product();     while (reader.read())     {         p.materialid = reader.getint32(reader.getordinal("materialid"));         p.materialname = reader.getstring(reader.getordinal("materialname"));         p.desc = reader.getstring(reader.getordinal("desc"));         p.materialprice = reader.getdecimal(reader.getordinal("materialprice"));         p.datasheet = reader.getstring(reader.getordinal("datasheet"));         objmaterials.add(p);     }     reader.close();     return objmaterials; } 

i know said there should data, typically throw when data missing.

try checking if "materialname" null in database:

p.materialname = reader.isdbnull("materialname")                    ? null : reader.getstring(reader.getordinal("materialname")) 

if works, error on line, might have check nulls on lines too.


for "materialprice" field, use this: (if p.materialprice not nullable, replace (decimal)null default value 0)

p.materialprice     = reader.isdbnull("materialprice")         ? (decimal)null : reader.getdecimal(reader.getordinal("materialprice")) 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -