public static ArrayList GetMetalsByType(string metalsType) { ArrayList list = new ArrayList(); string query = string.Format("SELECT * FROM Metali WHERE type LIKE '{0}'", metalsType); try { conn.Open(); command.CommandText = query; SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { int id = reader.GetInt32(0); string type = reader.GetString(1); double price = reader.GetDouble(2); string weight = reader.GetString(3); string image = reader.GetString(4); Metals metals = new Metals(id, type, price, weight, image); list.Add(metals); } } finally { conn.Close(); } return(list); }
public static void AddMetal(Metals metals) { string query = string.Format("INSERT INTO metali VALUES ('{0}',@price,'{1}','{2}')", metals.Type, metals.Weight, metals.Image); command.CommandText = query; command.Parameters.Add(new SqlParameter("price", metals.Price)); try { conn.Open(); command.ExecuteNonQuery(); } finally { conn.Close(); command.Parameters.Clear(); } }
protected void btnSave_Click(object sender, EventArgs e) { try { string type = txtType.Text; double price = Convert.ToDouble(txtPrice.Text); price = price / 100; string weight = txtWeight.Text; string image = "~/Imgs" + ddlImage.SelectedValue; Metals metals = new Metals(type, price, weight, image); ConnectionClass.AddMetal(metals); lblResult.Text = "Upload succesful!"; ClearTextFields(); } catch (Exception) { lblResult.Text = "Upload failed!"; } }