2012-06-14 31 views
0

上午使用VS2010的診所的建築物窗口窗體應用程序。 在窗體中有一個顯示註冊數據庫的searchbutton。
當我測試的應用程序,提供註冊號碼,並單擊搜索
按鈕什麼都沒有顯示,也沒有顯示錯誤信息。
這裏是我的代碼我的搜索按鈕不顯示數據

 string connectionstring = "Data Source=localhost;Initial Catalog=HMS;Persist Security Info=True;User ID=Developer;[email protected]"; 
     SqlConnection connection = new SqlConnection(connectionstring); 

     string SelectStatement = "SELECT * FROM MyTable where RegistrationNo = '@RegistraionNo'"; 
     SqlCommand insertcommand = new SqlCommand(SelectStatement, connection); 
     insertcommand.Parameters.AddWithValue("@RegistrationNo", txtsearch.Text); 
     SqlDataReader reader; 
     try 
     { 
      connection.Open(); 
      reader = insertcommand.ExecuteReader(); 

      while (reader.Read()) 
      { 
       textBox3.Text = reader["RegistratioNo"].ToString(); 
       textBox1.Text = reader["FirstName"].ToString(); 
       textBox2.Text = reader["LastName"].ToString(); 
       genderOption.Text = reader["Sex"].ToString(); 
       textBox7.Text = reader["Contact"].ToString(); 
       textBox4.Text = reader["Age"].ToString(); 
       textBox5.Text = reader["Weight"].ToString(); 
       textBox6.Text = reader["Weight"].ToString(); 
       comboBox1.Text = reader["Tribe"].ToString(); 

      }//end while 
      reader.Close(); 
     } 
     catch (SqlException ex) 
     { 
      throw ex; 

     }//end catch 
     finally 
     { 
      connection.Close(); 
     }// end finally 

`

+0

您可能沒有收到錯誤,因爲您僅捕獲SqlException,將看不到任何其他類型的異常。改變你的catch子句來捕獲(Exception ex),你應該能夠看到錯誤。 –

+0

你不應該'拋出ex',除非你喜歡在'InnerExceptions'中嵌入'Exceptions'。 – bluevector

+0

謝謝,這是很好的觀察 – kombo

回答

4

你有一個錯字

RegistraionNo而不是RegistrationNo

string SelectStatement = "SELECT * FROM MyTable where RegistrationNo = '@RegistraionNo'"; 

另一個

textBox3.Text = reader["RegistratioNo"].ToString(); 

你會複製100行

我要註冊,我要註冊,我要註冊。

+0

+1爲扣留 –

+0

+1的鷹的眼睛! – Mualig

+0

謝謝你的身體:Raphael Althaus – kombo

相關問題