i am told t use old traditional way of SQL in MVC so created login register page but now problem is that i can\'t return data to VIEW from dataset.
Model:
You should not use DataSets and SqlDataAdapters in ASP.NET MVC. You should use models instead.
So let me try to rewrite your code. Start by defining the model that will represent your entity:
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
you could also have a ConnectionStatus
model:
public class ConnectionStatus { public T Result { get; set; } public string Message { get; set } }
and then your data layer might contain 2 methods (one for verifying the credentials and one for getting the list of users):
public static class Db
{
public static ConnectionStatus<bool> Login(string email, string password, string connectionString)
{
string hasedPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
using (SqlConnection sqlCon = new SqlConnection(connectionString))
using (SqlCommand sqlCom = sqlCon.CreateCommand())
{
sqlConn.Open();
sqlCom.CommandText = "SELECT count(*) FROM tblRegister WHERE userEmail=@email AND userPwd=@pwd";
sqlCom.Parameters.AddWithValue("@email", email);
sqlCom.Parameters.AddWithValue("@pwd", hasedPassword);
var status = new ConnectionStatus<bool>();
status.Result = false;
try
{
int rowsFound = Convert.ToInt32(sqlCom.ExecuteScalar());
if (rowsFound == 1)
{
status.Result = true;
status.Message = "User logged in successfully, " + rowsFound;
}
else
{
status.Message = "Invalid email/password combination.";
}
}
catch (Exception ex)
{
status.Message = ex.Message;
}
return status;
}
}
public static ConnectionStatus<IList<User>> GetUsers()
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
using (SqlCommand sqlCom = sqlCon.CreateCommand())
{
sqlConn.Open();
sqlCom.CommandText = "SELECT userID, userName FROM tblRegister";
var status = new ConnectionStatus<IList<User>>();
status.Result = new List<User>();
try
{
using (var reader = sqlCom.ExecuteReader())
{
while (reader.Read())
{
var user = new User();
user.Id = reader.GetInt32(reader.GetOrdinal("userID"));
user.Email = reader.GetString(reader.GetOrdinal("userName"));
status.Result.Add(user);
}
}
}
catch (Exception ex)
{
status.Message = ex.Message;
}
return status;
}
}
}
and then define a view model:
public class LoginViewModel
{
public string Command { get; set; }
public string TxtboxEmail { get; set; }
public string TxtboxPassword { get; set; }
}
that your controller action will take as parameter:
public ActionResult LoginResult(LoginViewModel model)
{
if (model.Command == "Login")
{
string conStr = "Data Source=HUNAIN-PC;Initial Catalog=registration;User ID=sa;Password=abc123!@#";
var loginStatus = Db.Login(model.TxtboxEmail, model.TxtboxPassword, conStr);
ViewBag.Message = loginStatus.Message;
}
var usersStatus = Db.GetUsers(conStr);
return View(usersStatus.Result);
}
and finally in your strongly typed view:
@model IList<User>
@{
ViewBag.Title = "Login Authentication";
}
<h4>@ViewBag.Message</h4>
<table>
@foreach (var user in Model)
{
<tr>
<td>@user.Id</td>
<td>@user.Email</td>
</tr>
}
</table>