c# - Why is this appearing: System.NullReferenceException: Object reference not set to an instance of an object -
this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i read other answers on here nothing has worked. issue if user enters incorrect password error (username fine):
system.nullreferenceexception: object reference not set instance of object.
stack trace:
[nullreferenceexception: object reference not set instance of object.] login.btnlogin_click(object sender, eventargs e) in c:\users\michelle\desktop\comf510_65300_hs_task_2\login.aspx.cs:31 system.web.ui.webcontrols.button.onclick(eventargs e) +9628614 system.web.ui.webcontrols.button.raisepostbackevent(string eventargument) +103 system.web.ui.webcontrols.button.system.web.ui.ipostbackeventhandler.raisepostbackevent(string eventargument) +10 system.web.ui.page.raisepostbackevent(ipostbackeventhandler sourcecontrol, string eventargument) +13 system.web.ui.page.raisepostbackevent(namevaluecollection postdata) +35 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +1724
the login.aspx.cs :
using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.configuration; public partial class login : system.web.ui.page { protected void page_load(object sender, eventargs e) { } protected void btnlogin_click(object sender, eventargs e) { sqlconnection mydb = new sqlconnection(configurationmanager.connectionstrings["loginconnectionstring"].connectionstring); mydb.open(); string checkuser = "select count (*) users username = '"+txtusername.text+"'"; sqlcommand com = new sqlcommand(checkuser, mydb); int temp = convert.toint32(com.executescalar().tostring()); mydb.close(); if (temp == 1) { mydb.open(); string checkpassword = "select password users password = '" + txtpassword.text + "'"; sqlcommand passcom = new sqlcommand(checkpassword, mydb); string pass = passcom.executescalar().tostring().replace(" ",""); if(pass == txtpassword.text) { session["new"] = txtusername.text; response.redirect("empwelcome.aspx"); } else { response.write("incorrect details! please try again.");// if password incorrect } } else { response.write("incorrect details! please try again."); // if username incorrect } } }
the source error:
line 31: string pass = passcom.executescalar().tostring().replace(" ","");
i'm sure easy fix experts pretty new in c#. thank in advance.
com.executescalar()
null (no results found)
try following code (and don't remember dispose db objects using
constructions).
protected void btnlogin_click(object sender, eventargs e) { using (sqlconnection mydb = new sqlconnection(configurationmanager.connectionstrings["loginconnectionstring"].connectionstring)) { mydb.open(); string passwordobject = "select password users username = '" + txtusername.text + "'"; using (sqlcommand com = new sqlcommand(passwordobject, mydb)) { var res = com.executescalar(); if (res != null) { string checkpassword = passwordobject string; if (txtpassword.text == checkpassword) { session["new"] = txtusername.text; response.redirect("empwelcome.aspx"); } else { response.write("incorrect details! please try again.");// if password incorrect } } else { response.write("incorrect details! please try again."); // if username incorrect } } } }
Comments
Post a Comment