2015-04-06 67 views
0

我想創建一個註冊表單。我想進行驗證檢查,如果文本框中的ID已經存在,它會顯示錯誤消息。然而,每當我嘗試搜索ID,並把它轉換成字符串我得到一個錯誤C#SQL搜索結果到一個字符串/文本框

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll 

這裏是我的代碼:

public SignUpForm() 
    { 
     InitializeComponent(); 
    } 
    // Connection String 
    SqlCeConnection connect = new SqlCeConnection("Data Source= Accounts.sdf;Persist Security Info=false;"); 

    private void btnRegister_Click(object sender, EventArgs e) { 
     String verifyID = ""; 

     connect.Open(); 

     using (SqlCeCommand cmd = new SqlCeCommand("SELECT Student ID FROM Users WHERE Student ID = @ID", connect)) 
     { 
      cmd.Parameters.AddWithValue("@ID", txtID.Text); 
      using (SqlCeDataReader reader = cmd.ExecuteReader()) 
      { 
       while (reader.Read()) 
       { 
        verifyID = (string)reader["Student ID"]; 
       } 
      } 
     } 

     txtTEMP.Text = verifyID; 

回答

2

您的SQL SELECT [Student ID] FROM Users WHERE Student ID = @ID似乎並不正確。它應該是

SELECT [Student ID] FROM Users WHERE [Student ID] = @ID

0

也許錯誤是在你的SQL語句。考慮將其更改爲:

SELECT StudentID FROM Users WHERE StudentID = @ID 
+0

即使列名在他們的空間?我仍然得到錯誤 – user2782104

+0

請看Saagar的回答:) – Minh

相關問題