Marijn Marijn - 1 month ago 9
ASP.NET (C#) Question

asp.mcv how to convert string into boolean flag

I'm trying too make a login page. But when I press F5 the page this line gives an error:

flag = Convert.ToBoolean(cmd.ExecuteScalar());


Login.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace LoginFormApp.Models
{
public class Login
{
[Required(ErrorMessage = "Username is required")]
[Display(Name = "Username")]
public string username { get; set; }
[Required(ErrorMessage = "Password is required")]
[Display(Name = "Password")]
public string password { get; set; }
public bool checkUser(string username, string password)
{
bool flag = false;
string connString = ConfigurationManager.ConnectionStrings["MyDatabaseEntities"].ConnectionString ;
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("Select count(*) from Login where username='" + username + "' and password='" + password + "'", conn);
flag = Convert.ToBoolean(cmd.ExecuteScalar());
return flag;
}
}
}
}


Index.cshtml

@model LoginFormApp.Models.Login
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>DNDB Login</title>
</head>
<body>
<div style="margin:0 auto; text-align: center; border: 2px; border-style: solid; width:400px;background-color:lightgrey">
@using (Html.BeginForm())
{
<table>
<tr>
<td>@Html.LabelFor(m => m.username)</td>
<td>@Html.TextBoxFor(m => m.username)</td>
<td>@Html.ValidationMessageFor(m => m.username)</td>
</tr>
<tr>
<td>@Html.LabelFor(m => m.password)</td>
<td>@Html.PasswordFor(m => m.password)</td>
<td>@Html.ValidationMessageFor(m => m.password)</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
<tr>
<td></td>
<td>
@ViewBag.Message
</td>
</tr>
</table>
}
</div>
</body>
</html>


show.cshtml

@model LoginFormApp.Models.Login
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Show</title>
</head>
<body>
<div>
<h3> Hi! @Model.username</h3>
</div>
</body>
</html>


Homecontroller.cs

using LoginFormApp.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LoginFormApp.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ViewResult Index(Login login)
{
if (ModelState.IsValid)
{
if (login.checkUser(login.username, login.password))
{
return View("Show", login);
}
else
{
ViewBag.Message = "Invalid Username or Password";
return View();
}
}
else
{
return View();
}
}
}
}


Thanks in advance.

Answer

You did it wrongly, ExecuteScalar() always returns the value of the first column, so in your case it returns the total counts which cannot be direclty convert to boolean.

int totalCount = Convert.ToInt32(cmd.ExecuteScalar());
flag = totalCount > 0;
Comments