[英]how to load selected data from database and display in textbox in another form
所以我有兩種形式。 一個是包含交貨列表框的主窗體,其中數據從數據庫加載。 另一種形式是交付形式,當用戶從列表框中選擇交付時,其詳細信息應顯示在交付形式中。 到目前為止,我已經成功地加載了數據並設法在我的交付表單中顯示了一些文本。 但是,我無法讓我的交貨單顯示列表框中所選交貨的數據。
這是我的一些代碼:
private void FrmDelivery_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\visits.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT CustomerName, CustomerAddress, ArrivalTime FROM tblVisits", con);
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
this.txtCustomerName.Text = sdr["CustomerName"].ToString();
this.txtCustomerAddress.Text = sdr["CustomerAddress"].ToString();
this.txtArrivalTime.Text = sdr["ArrivalTime"].ToString();
}
sdr.Close();
if (theDelivery != null)
{
txtCustomerName.Text = theDelivery.customerName;
txtCustomerAddress.Text = theDelivery.customerAddress;
txtArrivalTime.Text = theDelivery.arrivalTime;
}
}
我會用這種方式:
public string Get_Field(SqlConnection con, string Table,string Field,string where)
{
DataSet RS_Temp = new DataSet();
SqlDataAdapter DA;
string SqlStr = "SELECT " & Field & " FROM " & Table;
//Add an "If statment" if there is a where condition....
DA = new OleDbDataAdapter(SqlStr, con);
DA.Fill(RS_Temp, Table_Name);
if (RS_Temp.Tables[Table_Name].Rows.Count == 0)
{
return ("");
}
if (System.Convert.IsDBNull(RS_Temp.Tables[Table_Name].Rows[0][0]))
{
return ("");
}
if (RS_Temp.Tables[Table_Name].Rows.Count != 0)
{
return (RS_Temp.Tables[Table_Name].Rows[0][0].ToString());
}
return ("");
}
不要忘記添加 Try & catch 當然然后:
YOURTEXTBOX.Text = Get_Field(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.