so i have two forms. one is the main form which contains a listbox of deliveries, where data gets loaded from a database. the other form is the delivery form which when the user selects the delivery from the listbox, its details should get displayed in the delivery form. so far i have managed to load the data in and managed to get some text displayed in my deliveryform. however, i cannot get my deliveryform to display the data of the selected delivery from the list box.
here is a bit of my code:
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;
}
}
I would use this way:
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 ("");
}
Dont forget to add Try & catch of course then:
YOURTEXTBOX.Text = Get_Field(...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.