public IActionResult Motor(AddMotorModel addMotor) { 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({addMotor.Price}, {addMotor.CompanyID}, '{addMotor.Model}', 0, '{addMotor.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 Motor VALUES({productID}, {addMotor.Voltage}, {addMotor.Current}, {addMotor.RotationSpeed}); 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 Motor() { ViewData["CustomerId"] = HttpContext.Session.Get <int>("_AccountId"); ViewData["AdminId"] = HttpContext.Session.Get <int>("_AdminId"); AddMotorModel addMotor = new AddMotorModel(); addMotor.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(); addMotor.Companies.Add(new Company { CompanyID = companyID, Name = name }); } return(View(addMotor)); } } } catch (Exception ex) { } return(View()); }