I am trying to get manufacturer, supplier, item group id's from their tables based on the names in the combobox. Means I am passing id to a variable based on the names in the combobox and then passing that id to database. But when I run this application I am getting same result for manufacturer, supplier and item group variable. Why is that?
It was working fine earlier, but not now! I can't figure it out!
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Manufact_Id from Manufacturer Where Name=@Name1", con);
cmd.Parameters.AddWithValue("@Name1", combomanufacture_Createitem.Text);
int a = Convert.ToInt32(cmd.ExecuteScalar());
SqlCommand cmd2 = new SqlCommand("SELECT Supplier_Id from Supplier Where Supplier_Name=@Name2", con);
cmd.Parameters.AddWithValue("@Name2", combo_supplierCreateitem.Text);
int b = Convert.ToInt32(cmd.ExecuteScalar());
SqlCommand cmd3 = new SqlCommand("SELECT ItemGroup_Id from ItemGroup Where Name=@Name3", 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");
}
}
INSERTstatement, you should usecmd1.ExecuteNonQuery();to execute it (not the.ExecuteReader()call which will return anIDataReaderto handle a result set being returned)