Login with Database using DropDownList

Douglasphillips24
Posted by Douglasphillips24 under ASP.NET MVC category on | Points: 40 | Views : 20955
I have a dropdownlist consisting of User ID and textbox for entering the password. Dropdownlist user id is coming from database. I want that when the user select the particular userid from the dropdown list and enter the password for that id. it should authenticate. I tried but failed

public ActionResult Section()
{
SectionLogin Sl = new SectionLogin();
Sl.UserList = GetUser();
return View(Sl);
}

[HttpPost]
public ActionResult Section(SectionLogin Ls)
{
Ls.UserList = GetUser();
using (LoginEntities dbu = new LoginEntities())
{
//var db = Ls.UserList.Where(model => model.Text == Ls.Section_name.ToString() && model.Value == Ls.Section_pwd).FirstOrDefault();
var db = dbu.SectionLogins.Where(model => model.Section_name == Ls.Section_name && model.Section_pwd == Ls.Section_pwd).FirstOrDefault();
if (db == null)
{
ViewBag.Message = "Wrong User name or pwd";
}
else
{
ViewBag.Message = "Successfull";
}
}

return View();
}

private static List<SelectListItem> GetUser()
{
string str = ConfigurationManager.ConnectionStrings["Con"].ConnectionString;
List<SelectListItem> List = new List<SelectListItem>();
using(SqlConnection con=new SqlConnection(str))
{
string query = "select Section_name from [CPSEdb].[dbo].[SectionLogin]";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection=con;
con.Open();
using (SqlDataReader rd = cmd.ExecuteReader())
{
while (rd.Read())
{
List.Add(new SelectListItem
{
Text = rd["Section_name"].ToString(),
Value = rd["Section_name"].ToString()

});
}
}
con.Close();

}
}
return List;
}


@using (Html.BeginForm("Section","Home",FormMethod.Post))
{
@Html.DropDownListFor(model => model.Section_name, Model.UserList, "--Select Section--")
@Html.TextBoxFor(model => model.Section_pwd)
<input type="submit" value="submit" />

}


public partial class SectionLogin
{
public string Section_name { get; set; }
public string Section_pwd { get; set; }
public List<SelectListItem> UserList {get;set;}
}

Comments or Responses

Login to post response