我試圖讓生產商,供應商,項目組的ID從表基於在組合框中的名稱。意思是我將ID傳遞給基於組合框名稱的變量,然後將該ID傳遞給數據庫。但是當我運行這個應用程序時,我得到了相同的結果,製造商,供應商和物品組變量。這是爲什麼?C#Windows窗體數據庫問題
這是工作的罰款較早,但不是現在!我無法弄清楚!
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Manufact_Id from Manufacturer Where [email protected]", con);
cmd.Parameters.AddWithValue("@Name1", combomanufacture_Createitem.Text);
int a = Convert.ToInt32(cmd.ExecuteScalar());
SqlCommand cmd2 = new SqlCommand("SELECT Supplier_Id from Supplier Where [email protected]", con);
cmd.Parameters.AddWithValue("@Name2", combo_supplierCreateitem.Text);
int b = Convert.ToInt32(cmd.ExecuteScalar());
SqlCommand cmd3 = new SqlCommand("SELECT ItemGroup_Id from ItemGroup Where [email protected]", con);
cmd.Parameters.AddWithValue("@Name3", combo_itemgroupCreateitem.Text);
int c = Convert.ToInt32(cmd.ExecuteScalar());
try
{
SqlCommand cmd1 = new SqlCommand("INSERT INTO Items (Custom_Code, Name, Manufacturer, Supplier, Item_Group, Activate, Purchase_Rate, Landing_Cost, Profit_Percentage, Price_to_Customer, MRP,Opening_Stock, Manage_Stock, Description, Discount) VALUES (@Customcode, @Name, @Manufacturer, @Supplier, @Itemgroup, @Activate, @Purchasedate, @Landingcost, @Profitpercentage, @PricetoCustomer, @MRP, @Openingstock, @Managestock, @Description1, @Discount)", con);
cmd1.Parameters.AddWithValue("@Customcode", txt_customcode_Createitem.Text);
cmd1.Parameters.AddWithValue("@Name", txt_nameCreateitem.Text);
cmd1.Parameters.AddWithValue("@Manufacturer", a);
cmd1.Parameters.AddWithValue("@Supplier", b);
cmd1.Parameters.AddWithValue("@Itemgroup", c);
cmd1.Parameters.AddWithValue("@Activate", combo_activateCreateitem.Text);
cmd1.Parameters.AddWithValue("@Purchasedate", txt_purchasedateCreateitem.Text);
cmd1.Parameters.AddWithValue("@Landingcost", txt_landingcosCreateitem.Text);
cmd1.Parameters.AddWithValue("@Profitpercentage", txt_activateCreateitem.Text);
cmd1.Parameters.AddWithValue("@PricetoCustomer", txt_PricetocustCreateitem.Text);
cmd1.Parameters.AddWithValue("@MRP", txt_mrpCreateitem.Text);
cmd1.Parameters.AddWithValue("@Openingstock", txt_openingstockCreateitem.Text);
cmd1.Parameters.AddWithValue("@Managestock", combomanagestock_Createitem.Text);
cmd1.Parameters.AddWithValue("@Description1", txt_descriptionCreateitem.Text);
cmd1.Parameters.AddWithValue("@Discount", txt_DiscountCreateitem.Text);
cmd1.ExecuteReader();
MessageBox.Show("Items added Successfully");
con.Close();
}
catch (Exception e1)
{
MessageBox.Show(e1 + "Please enter valid data");
}
}
旁註:因爲你不期望任何數據從你的'INSERT'語句返回,你應該使用'cmd1.ExecuteNonQuery();'來執行它(不是'.ExecuteReader()'調用,它會返回一個'IDataReader'來處理返回的結果集) –