sql - Can't check null value with these 2 ways C# datareader Null value Error -
the problem ; use code inside class , call class main form. got error datareader cant null value. how can check coming data sp not null. inside code if see null change , return outval (0000)
public string opt() { string strng= ""; string outval= ""; sqlconnection conn = new sqlconnection(); conn.connectionstring = configurationmanager.connectionstrings["nmrbg"].connectionstring; conn.open(); sqlcommand cmd = new sqlcommand("nmp_sp_sy", conn); cmd.commandtype = commandtype.storedprocedure; sqldatareader rdr = cmd.executereader(); // if (rdr.hasrows) // { // while (rdr.read()) // { // int nm= rdr.getint32(0); // strng= convert.tostring(nm); // outval= strng; // } // } while (rdr.read()) { int nm= rdr.getint32(0); strng= convert.tostring(nm); outval= strng; } if (!rdr.hasrows) { outval= "0000"; } rdr.close(); rdr.dispose(); conn.close(); conn.dispose(); return outval; }
`
sp ;
begin begin transaction declare @minsira int select @minsira=min(sirano) nmr_tbl_srbklynlr turid=2 , aktifpasif=1 update top (1) nmr_tbl_srbklynlr set aktifpasif = 0 sirano=@minsira select @minsira commit transaction end
test returning values on null: database return null on int. wrap idisposable
using: prevents resource leakage on both normal flow , exceptions.
public string opt() { using (sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["nmrbg"].connectionstring)) { conn.open(); using (sqlcommand cmd = new sqlcommand("nmp_sp_sy", conn)) { cmd.commandtype = commandtype.storedprocedure; using (sqldatareader rdr = cmd.executereader()) { // if there's no records read (rdr empty) return default if (!rdr.read()) return "0000"; object rawdata = rdr.getvalue(0); if (object.refrenceequals(null, rawdata)) return "0000"; // <- or whatever on null value else return rawdata.tostring(); } } } }
Comments
Post a Comment