登录页面验证NIC号码
问题描述:
我面临以下j问题:我在Access 2010上有一个数据库,字段为NIC
,Active
和Page
,都是数字类型。我想要创建一个登录页面,将NIC
(数字)作为用户的输入,然后根据其NIC将其重定向到特定页面。登录页面验证NIC号码
不同的人会看到不同的页面..我得到一个错误在ExecuteScalar
命令,也许我的查询是不正确的或可能ExecuteScalar
不能持有查询...我收到data type mismatch
错误。
try
{
FirsstPage f = new FirsstPage();
SecondPage second = new SecondPage();
oledcon.Open();
string NIc = (TextBox1.Text);
// string query = "select * from LogINTable where NIC='" + NIc + "'AND Active=0 AND page=1";
//string query = "select * from LogINTable where NIC='" + nic + "'AND Active=0";
string query = "SELECT * FROM LogINTable WHERE NIC= '" + NIc + "' AND Active=0 AND page=1";
//string query = "select
OleDbCommand comm = new OleDbCommand(query,oledcon);
string a = (string) comm.ExecuteScalar();
if (a != null)
{
Response.Redirect("FirsstPage.aspx");
string update = "update into LogINTable Active='1' where NIC='" + NIc + "' ";
//OleDbCommand com = new OleDbCommand();
//int b = Convert.ToInt32(com.ExecuteScalar());
}
else
{
Response.Redirect("SecondPage.aspx");
string update = "update into LogINTable Active='1' where NIC='" +NIc + "' ";
}
oledcon.Close();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
oledcon.Close();
}
答
问题是您正在使用带有错误查询的ExecuteScalar。
string a = (string) comm.ExecuteScalar();
ExecuteScalar()将返回单值作为查询的结果。
请更改您的查询像吹查询从数据库代替整个colomn的返回单个值
Select NIC FROM LogINTable WHERE NIC= '" + NIc + "' AND Active=0 AND page=1"
来源:http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx
我希望这会帮助你。
+0
感谢您的回复,但我仍然收到此错误:标准表达式中的数据类型不匹配。 – 2013-05-13 15:00:19
我或多或少地重写了你的整个问题,使其实际可读,删除所有* l33t sp34k *等。如果你想让人们花时间做出回应,如果他们必须先花5分钟时间来理解他们不太愿意提供帮助的问题,那么请注意演示。 – 2013-05-13 09:20:19
“SELECT *”几乎总是一个坏主意(除了'EXISTS()'子句),特别是在这里,你只需要一列的值 – 2013-05-13 09:21:30
如果这个'NIC'列是数字,你为什么要把该值在引号中? – 2013-05-13 11:47:53