c# - How to Eliminate Checking of Null Value Datatable in IF condition -
in page fetching value database & filling values in datatable. comparing values mac
string in if. based upon condition in query there no records fetched, stuck in if condition , throws no row @ position 0
exception rather going else part.
my code is:
string mac = getmac(); string qry = "select vuserid,password passtable vuserid='" + username.text + "' , flag='a'"; string qry = "select vuserid,password passtable flag='a'"; string strq = "select mac_id sysinfo appflag='a'"; using (sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["evalcon"].connectionstring)) { try { sqlcommand cmd = new sqlcommand(qry, conn); sqlcommand cmd1 = new sqlcommand(qry, conn); sqlcommand cmd2 = new sqlcommand(strq, conn); conn.open(); sqldataadapter da = new sqldataadapter(cmd); sqldataadapter daa = new sqldataadapter(cmd1); sqldataadapter dap = new sqldataadapter(cmd2); datatable dt = new datatable(); datatable dtt = new datatable(); datatable tab = new datatable(); da.fill(dt); daa.fill(dtt); dap.fill(tab); (int = 0; < tab.rows.count; i++) { (int x = 0; x <= dtt.rows.count - 1; x++) { if (mac == tab.rows[i]["mac_id"].tostring() || tab.rows.count != 0) { if (username.text == dtt.rows[x]["vuserid"].tostring() && password.text == dtt.rows[x]["password"].tostring()) { response.redirect("~/changepass.aspx"); break; } else { lblmessage.visible = true; lblmessage.forecolor = system.drawing.color.red; lblmessage.text = "invalid username or password !!!"; } } else { lblmessage.visible = true; lblmessage.forecolor = system.drawing.color.red; lblmessage.text = "invalid access point evaluation !!!"; } } } } { conn.close(); conn.dispose(); } }
first of all, may want give more meaningful names variables.
on side note, may want change loops foreach loop:
foreach (datarow tabrow in tab.rows.count) { foreach (datarow dttrow in dtt.rows.count) { // logic here // tab.rows[i]["mac_id"] becomes tabrow["mac_id"] // , // dtt.rows[x]["vuserid"] becomes dttrow["vuserid"] // , on... } }
this way if there no records fetched won't go in.
after that, may want check conditions on rowcount > 0
datatables before going inside loops , act outside loop if rowcount 0.
Comments
Post a Comment