上午使用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
`
您可能沒有收到錯誤,因爲您僅捕獲SqlException,將看不到任何其他類型的異常。改變你的catch子句來捕獲(Exception ex),你應該能夠看到錯誤。 –
你不應該'拋出ex',除非你喜歡在'InnerExceptions'中嵌入'Exceptions'。 – bluevector
謝謝,這是很好的觀察 – kombo