2014-08-30 64 views
-2

我正在處理一個簡單的asp.net登錄頁面。一個簡單的代碼,當用戶登錄時驗證與SQL和直接到頁面的人員或管理員。但我有這個錯誤(int.Parse(myReader.ToString())> 0)...輸入字符串的不正確的格式 我的代碼..輸入字符串的格式不正確c#和asp.net

string Connection = "Data Source=(ip);Initial Catalog=..;Persist Security Info=True;User ID=(id);Password=(pass)"; 
SqlConnection myConn = new SqlConnection(Connection); 

SqlCommand SelectCommand = new SqlCommand("select * from tar_login where Username ='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and Position='" + ddlPosition.Text + "';", myConn); 

myConn.Open(); 

var myReader = SelectCommand.ExecuteScalar(); 
myConn.Close(); 

if (myReader != null) 
{ 
    if (int.Parse(myReader.ToString()) > 0) 
    { 
     if (ddlPosition.Text == "Admin") 
     { 
      Response.Redirect("manager.aspx"); 
     } 
    } 
} 

if (myReader != null) 
{ 
    if (int.Parse(myReader.ToString()) > 0) 
    { 
     if (ddlPosition.Text == "Staff") 
     { 
      Response.Redirect("staff.aspx"); 
     } 
    } 
} 

else 
{ 
    Label1.ForeColor = System.Drawing.Color.Red; 
    Label1.Text = "Incorrect. Please try again"; 
    myConn.Close(); 
} 

。幫助作爲即時通訊新的ASP。 net和c#

+0

這意味着'myReader.ToString()'不是一個數,或不的格式是一個int。 – Noctis 2014-08-30 06:55:31

+0

調試你的代碼,在var var myReader = SelectCommand.ExecuteScalar();添加斷點並檢查它的值是什麼,可能不是整數或null – Farrokh 2014-08-30 06:57:57

+0

@Noctis在斷點我得到myReader爲null – Tar7 2014-08-30 07:09:16

回答

0

ExecuteScalar返回一個對象(該行)或null。
那你試試吧ToString吧。除非你只返回一列,這是一個int,這不會是int.Parse將能夠解析的東西。

您可以嘗試int.TryParse,但它會返回false我相信。 如果你在那裏放置一個斷點,你在myReader裏有什麼?它可能會讓我們更好地瞭解你正在處理的事情。


編輯:

O_O?!

你有兩個子句說:if (myReader != null)。合併它們。

無論何時使用數據庫或您需要處理的任何資源,請使用using,如提供了Saunders

除此之外,你回答了你的問題。如果您收到null,則無法將其解析爲int

您必須找出您的數據庫問題或您的查詢,然後重試。

+0

在斷點我得到myReader是nul – Tar7 2014-08-30 07:09:35

0

您的ExecuteScalar()帶來一個非數字字符(例如'A'),它不能轉換爲整數。

似乎在你的代碼沒有必要檢查> 0

myReader的!= null是足夠的。

無論Samiey上面提出的建議看起來都很完美。

0

嘗試這個代碼

string Connection = "Data Source=(ip);Initial Catalog=..;Persist Security Info=True;User ID=(id);Password=(pass)"; 
    SqlConnection myConn = new SqlConnection(Connection); 

    SqlCommand SelectCommand = new SqlCommand("select * from tar_login where Username ='" + txtUsername.Text + "' and Password='" + txtPassword.Text + "' and Position='" + ddlPosition.Text + "';", myConn); 

    myConn.Open(); 

    Sqldatareader myReader = SelectCommand.ExecuteReader(); 
    myConn.Close(); 

    if (reader.HasRows) 
     { 
if (ddlPosition.Text == "Admin") 
     { 
      Response.Redirect("manager.aspx"); 
     } 
else if (ddlPosition.Text == "Staff") 
     { 
      Response.Redirect("staff.aspx"); 
     } 
    else 
    { 
     Label1.ForeColor = System.Drawing.Color.Red; 
     Label1.Text = "Incorrect. Please try again"; 
     myConn.Close(); 
    } 
+0

嗨,我試過你的代碼,但我HasRows下面有一條波浪線。當我把我的光標放在它上面,我得到這個錯誤:'對象'不包含'HasRows'的定義,並且沒有方法'HasRows'接受類型'對象'的第一個參數的擴展可以被發現 – Tar7 2014-08-31 07:05:49

+0

http:/ /stackoverflow.com/questions/12609959/how-to-check-if-sqldatareader-has-no-rows檢查檢查datareader是否有行的鏈接 – 2014-09-01 05:05:50