private void TxtFarmerCode_TextChanged(object sender, EventArgs e)
{
try
{
cmd.Open();
cmd = new SqlCommand("Select farmername, from cottonpurchase where [email protected]", conn);
cmd.Parameters.Add("@aa", SqlDbType.Int).Value = TxtFarmerCode.Text;
dr = cmd.ExecuteReader();
if (dr.HasRows == false)
{
throw new Exception();
}
if (dr.Read())
{
// textBox1.Text = dr[0].ToString(); Since U r going to give the ID and retrieve in textBox1.
TxtFarmerName.Text = dr[0].ToString();
//textBox3.Text = dr[1].ToString();
//textBox4.Text = dr[2].ToString();
//textBox7.Text = dr[3].ToString();
//dateTimePicker1.Text = dr[4].ToString();
//dateTimePicker2.Text = dr[5].ToString();
//textBox5.Text = dr[6].ToString();
}
}
catch
{
// lblError = "THE GIVEN ID IS UNAVAILABLE";
}
finally
{
conn.Close();
}
Q
C#具有獲取
-3
A
回答
0
你缺少連接塊的一些錯誤(不管你要連接到更換connectionString
):
using (SqlConnection conn= new SqlConnection(
connectionString))
{
//You have cmd.Open it should be connection
conn.Open();
cmd = new SqlCommand("Select farmername, from cottonpurchase where [email protected]", conn);
cmd.Parameters.Add("@aa", SqlDbType.Int).Value = TxtFarmerCode.Text;
dr = cmd.ExecuteReader();
if (dr.HasRows == false)
{
throw new Exception();
}
if (dr.Read())
{
// textBox1.Text = dr[0].ToString(); Since U r going to give the ID and retrieve in textBox1.
TxtFarmerName.Text = dr[0].ToString();
//textBox3.Text = dr[1].ToString();
//textBox4.Text = dr[2].ToString();
//textBox7.Text = dr[3].ToString();
//dateTimePicker1.Text = dr[4].ToString();
//dateTimePicker2.Text = dr[5].ToString();
//textBox5.Text = dr[6].ToString();
}
}
catch
{
// lblError = "THE GIVEN ID IS UNAVAILABLE";
}
finally
{
conn.Close();
}
}
邊注... NEVER做catch {}
相關問題
- 1. 獲取具有特定ID
- 2. 獲取具有的inputType numberPassword
- 3. 獲取具有相同父
- 4. C# - 獲取具有runat =「server」的HTML標籤的數組/集合
- 5. Objective-C-獲取具有最大屬性值的對象
- 6. c#4.0獲取具有(指定屬性和attribute.data)的類屬性
- 7. 獲取行的總金額具有一定值C#WPF LINQ的
- 8. C#System.IO.FileInfo獲得具有關鍵
- 9. 具有獲取列表項被Python
- 10. 獲取具有min屬性的元素
- 11. SQL查詢來獲取具有每seat_count
- 12. 獲取具有URL參數,它
- 13. 獲取具有相同的顏色
- 14. 獲取具有相同的包ID
- 15. 獲取具有特定類的jQuery
- 16. 獲取具有指定時間
- 17. grep的:獲取不具有匹配
- 18. 獲取具有相同的ID列
- 19. 獲取具有recylerview的listview的值
- 20. C#獲取VolumeSerialNumber
- 21. 獲取在c#
- 22. 獲取硬盤沒有在PHP或C
- 23. ODBC:Python獲取數據,C#不是有時?
- 24. Sharepoint獲取所有列表數據c#
- 25. 獲取有使用C#lambda表達式
- 26. C++ winapi listview NM_CUSTOMDRAW:沒有獲取CDDS_ITEMPREPAINT
- 27. 獲取所有打開的表單,c#?
- 28. 獲取日期沒有時間在c#
- 29. 語言C - 獲取進程所有者
- 30. 如何使用C#獲取所有CheckBoxes?
請顯示實際的錯誤。 – 2011-06-16 16:10:15
你是否從某處複製代碼?看來博士,康恩和CMD沒有宣佈。 – marto 2011-06-16 16:21:03
我認爲這是來自[本文發表](http://www.daniweb.com/software-development/csharp/threads/95937/1090530#post1090530) – 2011-06-16 16:27:47