c# - Using DBNullable Types -
here's code:
protected void btnsearch_click(object sender, eventargs e) { list<statbatter> completelist = new list<statbatter>(); dalbatter batter = new dalbatter(); batter.namelast = txtlastname.text; datatable batters = dalbatter.getbattersbylastname(batter); (int = 0; < batters.rows.count; i++) { datarow bat = batters.rows[i]; statbatter stat = new statbatter(); stat.playerid = bat.field<string>("playerid"); stat.g = bat.field<int32>("g"); stat.ab = bat.field<int32>("ab"); stat.h = bat.field<int32>("h"); stat.bb = bat.field<int32>("bb"); stat.cs = bat.field<int32>("cs"); stat.doub = bat.field<int32>("2b"); stat.trip = bat.field<int32>("3b"); stat.hr = bat.field<int32>("hr"); stat.rbi = bat.field<int32>("rbi"); stat.sb = bat.field<int32>("sb"); stat.k = bat.field<int32>("so"); stat.ibb = bat.field<int32>("ibb"); stat.hbp = bat.field<int32>("hbp"); stat.sh = bat.field<int32>("sh"); stat.sf = bat.field<int32>("sf"); stat.gidp = bat.field<int32>("gidp"); //calculated fields stat.namefull = statbatter.fullname(bat.field<string>("namelast"), bat.field<string>("namefirst")); stat.avg = statbatter.battingaverage(stat.h, stat.ab); stat.obp = statbatter.onbasepercentage(stat.ab, stat.h, stat.bb, stat.hbp, stat.sf); stat.slg = statbatter.slugging(stat.h, stat.doub, stat.trip, stat.hr, stat.ab); completelist.add(stat); } gvbatters.datasource = completelist; gvbatters.databind(); }
here's problem:
i attached mdf file project in app_data file, created connection string use local mdf. working fine. took project pc, , on line:
stat.cs = bat.field<int32>("cs");
i
cannot cast dbnull.value type 'system.int32'. please use nullable type.
not sure why worked on other pc, nonetheless, how can prevent error?
the column cs in database can null, shouldn't use reader int
values. use nullabledatareader instead int?
type. use similar:
stat.cs = dr.getnullableint32("cs");
Comments
Post a Comment