public IActionResult LED(AddLEDModel addLED) { string connString = @"Data Source=DESKTOP-JM077BN;Initial Catalog=ArduinoStore;Integrated Security=True"; try { using (SqlConnection conn = new SqlConnection(connString)) { string query = $"INSERT INTO Product VALUES({addLED.Price}, {addLED.CompanyID}, '{addLED.Model}', 0, '{addLED.ProductImage}'); SELECT SCOPE_IDENTITY()"; SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); int productID = int.Parse(dr[0].ToString()); dr.Close(); query = $"INSERT INTO LED VALUES({productID}, '{addLED.Color}', {addLED.Diameter}, {addLED.Current}); SELECT SCOPE_IDENTITY()"; cmd = new SqlCommand(query, conn); dr = cmd.ExecuteReader(); if (dr.HasRows) { conn.Close(); return(RedirectToAction("index", "admin")); } } else { conn.Close(); } } } catch (Exception ex) { } return(View()); }
public IActionResult LED() { ViewData["CustomerId"] = HttpContext.Session.Get <int>("_AccountId"); ViewData["AdminId"] = HttpContext.Session.Get <int>("_AdminId"); AddLEDModel addLED = new AddLEDModel(); addLED.Companies = new List <Company>(); string connString = @"Data Source=DESKTOP-JM077BN;Initial Catalog=ArduinoStore;Integrated Security=True"; try { using (SqlConnection conn = new SqlConnection(connString)) { string query = $"SELECT * FROM Company ORDER BY name"; SqlCommand cmd = new SqlCommand(query, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { int companyID = int.Parse(dr["CompanyID"].ToString()); string name = dr["Name"].ToString(); addLED.Companies.Add(new Company { CompanyID = companyID, Name = name }); } return(View(addLED)); } } } catch (Exception ex) { } return(View()); }