【发布时间】:2016-10-02 13:14:05
【问题描述】:
我正在使用 mvc5 创建一个 Web 应用程序我的数据库中有多个用户 例如
用户
user1) 用户名=ibrahim 密码=1ibrahim user2) 用户名=管理员密码=4321
当我从用户 1(ibrahim) 登录时,页面成功重定向到欢迎页面, 但是当我从用户登录到(管理员)时,错误来了
“System.InvalidCastException”类型的异常发生在 mscorlib.dll 但未在用户代码中处理
附加信息:对象不能从 DBNull 转换为其他 类型。
在user = Convert.ToBoolean(cmd.ExecuteScalar());这一行
这是我的代码
public class loginuser
{
SqlCommand cmd;
public string role { get; set; }
public string username { get; set; }
public string password { get; set; }
public bool getlogintype(string role, string username, string password)
{
string tru = "";
string fals = "";
bool user;
string strname = "";
SqlConnection con = new SqlConnection("Data Source=erp.hti-india.com,1434;Initial Catalog=erp;Connect Timeout=3600;User Id=erprakesh;Password=14erprakesh14");
List<object> login = new List<object>();
if (role == "Admin" || role == "Super Admin" || role !=null)
{
cmd = new SqlCommand("select * from [admin] where userid='" + username + "' and pass ='" + password + "'", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
user = true;
//HttpContext.Current.Session["userid"] = username.ToString();
//HttpContext.Current.Session["tru"] = tru.ToString();
// want to redirect to welcome page if condition satisfied.
}
else
{
user = false;
//want to show the label error message(declare as string errormsg)
}
con.Close();
}
con.Open();
user = Convert.ToBoolean(cmd.ExecuteScalar());
con.Close();
return user;
}
}
【问题讨论】:
-
select * from然后使用cmd.ExecuteScalar()??不知道你想做什么。 -
我只想验证用户名和密码
-
@Ric 我应该用户选择像
select userid,pass from admin这样的语句
标签: c# asp.net-mvc asp.net-mvc-5