ii'm newbie to MVC and has been assigned a task to make register and login page in plain old sql queries etc. Advance will come later. I succeeded in making register page, it's done perfectly but problem is in login page, It returns -1 for sqlreader , i am passing correct values even debugging showed correct values but still it's behaving like nothing being found. Custom error: No of records being found are/is =-1
code:
view:
@using(Html.BeginForm("loginResult", "Home", FormMethod.Post, new {id="loginForm"}))
{
<div>
<i>@Html.Label("Email:")</i>
@Html.TextBox("txtboxEmail")
</div>
<div>
<i>@Html.Label("Password:")</i>
@Html.Password("txtboxPassword")
</div>
<div>
<button type="submit" id="btnLogin" name="Command" value="Login">Login</button>
</div>
}
controller:
[HttpPost]
public ActionResult loginResult(String command, FormCollection formData)
{
if (command == "Login")
{
var email = formData["txtboxEmail"];
var pwd = formData["txtboxPassword"];
String conStr = "Data Source=HUNAIN-PC;Initial Catalog=registration;User ID=sa;Password=abc123!@#";
database db = new database();
var status = db.Login_db(email, pwd, conStr);
ViewBag.Message = status.Message;
}
return View();
}
public ActionResult login()
{
ViewBag.Message = "Login";
return View();
}
model:
public ConnectionStatus Login_db( String email, String pwd, String conStr)
{
SqlConnection sqlCon = new SqlConnection(conStr);
SqlCommand sqlCom = new SqlCommand();
sqlCom.Connection = sqlCon;
sqlCom.CommandText = "select userEmail, userPwd from tblRegister where userEmail=@email AND userPwd=@pwd";
sqlCom.Parameters.AddWithValue("@email", email);
sqlCom.Parameters.AddWithValue("@pwd", pwd);
ConnectionStatus connectStatus = new ConnectionStatus();
int row_aff;
try
{
sqlCon.Open();
SqlDataReader dr = sqlCom.ExecuteReader();
row_aff = dr.RecordsAffected;
connectStatus.Message = "No of records being found are/is ="+ row_aff;
}
catch (Exception ex)
{
connectStatus.Message = ex.Message;
}
finally
{
sqlCon.Close();
}
return connectStatus;
}
}
}
ConnectionStatus is a class which has one property MESSAGE.
namespace LoginSys.Models
{
public class ConnectionStatus
{
public String Message { get; set; }
}
}