vb.net - Convert DBNull To String Before Populating Data To Datagridview -
i need way convert dbnull string when populating data database datagridview. below code
private sub fetchdata() sql = "select contributiontype,status,paymethod,bankname,accountno,amount,fullname,district,refno,contributorgrp" sql &= " tblincome txnid=@id" command = new oledbcommand(sql, connection) command.parameters.add(new oledbparameter("@id", oledbtype.varchar)).value = lbltxnid.text adapter = new oledbdataadapter(command) dt = new datatable adapter.fill(dt) each row in dt.rows populategrid(row(0), row(1), row(2), row(3), row(4), row(5), row(6), row(7), row(8), row(9)) next dt.rows.clear() dt.dispose() end sub private sub populategrid(contributiontype string, status string, paymethod string, bankname string _ , accountno string, amount double, fullname string, district string _ , refno string, contributorgrp string) dim row string() = new string() {contributiontype, status, paymethod, bankname, accountno, amount.tostring("n") _ , fullname, district, refno, contributorgrp} dgvcontributions.rows.add(row) end sub
the fields bankname , accountno allow null entries ones null values.
you use method:
public shared function convertdbval(of t)(obj object, optional nullfallbackval t = nothing) t if obj nothing orelse obj.equals(dbnull.value) return nullfallbackval else return ctype(obj, t) end if end function
usage:
dim bankname string = convertdbval(row(3), "") dim accnumber = convertdbval(row(4), "")
if don't specify (optional) fallback-value have specify type explicitly:
dim bankname string = convertdbval(of string)(row(3))
wiki
Comments
Post a Comment