Entered correct data but log in keeps failing c# asp.net -


this question has answer here:

i new c# asp.net. got code tutorial getting error. label says log in failed though entered correct data. please me :( , says: object reference not set instance of object when entered incorrect data.

here's connectionclass.cs

public class connectionclass  {  private static sqlconnection conn;  private static sqlcommand command;  static connectionclass() {     string connectionstring = configurationmanager.connectionstrings["coffeedbconnectionstring"].tostring();     conn = new sqlconnection(connectionstring);     command = new sqlcommand("", conn); }  public static user loginuser(string login, string password) {      //check if user exists     string query = string.format("select count(*) skymusic.dbo.user username = '{0}'", login);     command.commandtext = query;      try     {         conn.open();         int numofusers = (int) command.executescalar();          if (numofusers < 1)         {             //user exists, check if passwords match             query = string.format("select password user username = '{0}'", login);             command.commandtext = query;             string dbpassword = command.executescalar().tostring();              if (dbpassword == password)             {                 //passwords match                 query = string.format("select email, user_type users username = '{0}'", login);                 command.commandtext = query;                  sqldatareader reader = command.executereader();                 user user = null;                  while (reader.read())                 {                     string email = reader.getstring(0);                     string type = reader.getstring(1);                      user = new user(login, password, email, type);                 }                 return user;             }             else             {                 //passwords not match                 return null;             }         }         else         {             return null;         }     }         {         conn.close();     }  } 

my login:

<asp:content id="content1" contentplaceholderid="head" runat="server">  </asp:content> <asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server">  <div id="formcont"> <table> <tr> <td><asp:label id="label1" runat="server" text="username:"></asp:label></td> <td><asp:textbox id="textbox1" runat="server" width="142px"></asp:textbox></td> </tr> <tr> <td><asp:label id="label2" runat="server" text="password:"></asp:label></td> <td><asp:textbox id="textbox2" runat="server" width="142px"></asp:textbox></td> </tr> <tr> <td colspan="2"></br><asp:button id="button1" runat="server" text="log in" />     <br />     </td> </tr> </table> <p>don't have account? click <a href="register.aspx" runat="server">here</a>!</p> </div> </asp:content> 

and

public partial class login : system.web.ui.page {     protected void page_load(object sender, eventargs e)     {      }      protected void button1_click(object sender, eventargs e)     {         user user = connectionclass.loginuser(txtuname.text, txtpass.text);           if (user != null)         {             session["login"] = user.login;             session["type"] = user.type;             response.redirect("home.aspx");         }         else         {             lblerror.text = "login failed";         }      } 

please :( here's db id, username, password, email, user_type

there 2 problems or error
1. can not login correct credential:
check this.

if (numofusers < 1)     {         //user exists, check if passwords match         query = string.format("select password user username = '{0}'", login); 

if no of user less 1 how can user exists in system.

2. null reference error:
in case user not exists method returned null should handling null , showing incorrect credential or user not exists

edit rahul seems correct in comments of question. debug code first.

edit

as per comment using
if (numofusers < 1)
then changed if (numofusers <= 1)
use if (numofusers > 0)


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -