getting error of "NullReferenceException was unhandled by user code" while i try to display first name after successful login in ASP.NET MVC -
i getting error of "nullreferenceexception unhandled user code" while try display first name after successful login in asp.net mvc.
model:
using system; using system.collections.generic; using system.componentmodel.dataannotations; using system.data; using system.data.sqlclient; using system.linq; using system.web; using system.data.linq.mapping; namespace club.models { public class user { int j; public string type1 { get; set; } [required] [display(name = "user name")] public string username { get; set; } [required] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [display(name = "remember on computer")] public bool rememberme { get; set; } /// <summary> /// checks if user given password exists in database /// </summary> /// <param name="_username">user name</param> /// <param name="_password">user password</param> /// <returns>true if user exist , password correct</returns> /// public bool isvalid(string _username, string _password) { using (var cn = new sqlconnection(@"data source=.\sqlexpress;attachdbfilename=e:\softwares\asp.net\club_hardik\club\app_data\club.mdf;integrated security=true;user instance=true")) { string _sql = @"select [contactno],[type] tbllogin " + @"where [contactno] = @u , [password] = @p"; var cmd = new sqlcommand(_sql, cn); console.writeline(cmd); cmd.parameters .add(new sqlparameter("@u", sqldbtype.nvarchar)) .value = _username; cmd.parameters .add(new sqlparameter("@p", sqldbtype.nvarchar)) .value = models.sha1.encode(_password); cn.open(); sqldatareader reader = cmd.executereader(); if (reader.hasrows) { reader.read(); type1 = reader.getstring(1); reader.dispose(); cmd.dispose(); return true; } else { reader.dispose(); cmd.dispose(); return false; } } } }
}
controller:
using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using system.web.security; using club.models; using system.text.regularexpressions; namespace club.controllers { public class usercontroller : controller { // // get: /user/ public actionresult index() { return view(); } [httpget] public actionresult member(user model) { var model1 = new m_reg(); using (var db = new clubdatacontext()) { var content = p in db.m_registarions p.m_contactno == model.username select p; foreach (m_registarion p in content) { model1.fname = p.m_fname; } } return view(); } [httpget] public actionresult memberdisplay(user u) { return view(); } [httpget] public actionresult login() { return view(); } [httppost] public actionresult login(models.user user) { if (modelstate.isvalid) { if (user.isvalid(user.username, user.password)) { formsauthentication.setauthcookie(user.username, user.rememberme); string = (user.type1).tostring(); string fixedstringone = regex.replace(a, @"\s+", string.empty); if (string.equals(fixedstringone, "member")) { return redirecttoaction("member",user); } else if (fixedstringone.equals("admin")) { return redirecttoaction("admin", "admin"); } else { } } else { modelstate.addmodelerror("", "login data incorrect!"); } } return view(user); } public actionresult logout() { formsauthentication.signout(); return redirecttoaction("index", "home"); } }
} view:
@model club.models.m_reg @{ viewbag.title = "member"; } <h2>member</h2> @using (html.beginform( )) { <div> <fieldset> <div class="editor-label"> <span>welcome @model.fname </span>
here getting error of** null reference unhandled usercode
</div> </fieldset> </div>
}
when debug line line 'firstname' database not display in view , getting error of "nullreference exception". don't know meaning of error. me solve error.
the model
property in view null
. should pass model created in member
action view:
[httpget] public actionresult member(user model) { var model1 = new m_reg(); // ... return view(model1); }
looking @ code, have same potential errors in memberdisplay
, login
actions.
Comments
Post a Comment