私が直面している問題を解決するために、あなたの支援をお願いしたいと思います.
私の問題は、orderID 1 に関連付けられている製品が製品 2 であるため、ドロップダウン リストに Test と Sample ではなく SAMPLE の 2 つの項目 (製品 2) が表示されることです。
しかし、OrdersTable の productID を 2 から 1 に変更すると、すべてが正しく表示されます。何故ですか?
注文表
OrderID ProductID
1 2
製品表
ProductID ProductName
1 Test
2 Sample
ASP.NET コード
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetOrders();
GetProducts();
}
}
private void GetOrders()
{
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.Text;
string SQL =
"SELECT o.OrderID, o.ProductID, p.ProductName FROM Orders AS o, Products AS p WHERE o.ProductID = p.ProductID";
com.CommandText = SQL;
SqlDataReader data = com.ExecuteReader();
while (data.Read())
{
ddlProduct.SelectedItem.Text = data["ProductName"].ToString();
}
}
private void GetProducts()
{
con1.Open();
SqlCommand com = new SqlCommand();
com.Connection = con1;
com.CommandType = CommandType.Text;
com.CommandText =
"SELECT * FROM Products";
SqlDataReader data = com.ExecuteReader();
ddlProduct.DataSource = data;
ddlProduct.DataValueField = "ProductID";
ddlProduct.DataTextField = "ProductName";
ddlProduct.DataBind();
data.Close();
con1.Close();
}