2013-05-13 65 views
1

我面臨以下j問題:我在Access 2010上有一個數據庫,字段爲NIC,ActivePage,都是數字類型。我想要創建一個登錄頁面,將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(); 
} 
+0

我或多或少地重寫了你的整個問題,使其實際可讀,刪除所有* l33t sp34k *等。如果你想讓人們花時間做出迴應,如果他們必須先花5分鐘時間來理解他們不太願意提供幫助的問題,那麼請注意演示。 – 2013-05-13 09:20:19

+0

「SELECT *」幾乎總是一個壞主意(除了'EXISTS()'子句),特別是在這裏,你只需要一列的值 – 2013-05-13 09:21:30

+0

如果這個'NIC'列是數字,你爲什麼要把該值在引號中? – 2013-05-13 11:47:53

回答

1

問題是您正在使用帶有錯誤查詢的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