private void MotorcycleDgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { //get the motorcycle id from datagridview hidden column int motorcycleId = int.Parse(MotorcycleDgv.Rows[MotorcycleDgv.CurrentRow.Index].Cells[2].Value.ToString());; if (!motorcycleService.HasMotorcycle(motorcycleId)) { MessageBox.Show("Invalid motorcycle data", GlobalConstants.MessageBoxTopInfo); return; } UserMotorcycleViewModel model = motorcycleService.GetMotorcycleForMainForm(motorcycleId); motorcycleService.SetMotorcycleDataVariables(model); mainForm.MotorcycleMakeLabel.Text = model.Make; mainForm.MotorcycleModelLabel.Text = model.Model; DialogResult dialogResult = MessageBox.Show($"Selected motorcycle is {model.Make} {model.Model}", GlobalConstants.MessageBoxTopInfo, MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (dialogResult == DialogResult.Yes) { this.Close(); return; } MessageBox.Show("Choose Again", GlobalConstants.MessageBoxTopInfo); }
public List <UserMotorcycleViewModel> GetUserMotorcycles(int userId) { try { StringBuilder getUserMotorcyclesQuerySb = new StringBuilder(); getUserMotorcyclesQuerySb.Append(" select md.id, md.make, md.model "); getUserMotorcyclesQuerySb.Append(" from motorcycledata md "); getUserMotorcyclesQuerySb.Append($" where md.user_id = {userId} "); if (connection.State == ConnectionState.Closed) { connection.Open(); } command.CommandText = getUserMotorcyclesQuerySb.ToString(); List <UserMotorcycleViewModel> userMotorcycles = new List <UserMotorcycleViewModel>(); using (reader = command.ExecuteReader()) { while (reader.Read()) { UserMotorcycleViewModel model = new UserMotorcycleViewModel { Make = reader["Make"].ToString(), Model = reader["Model"].ToString(), MotorcycleId = int.Parse(reader["id"].ToString()), }; userMotorcycles.Add(model); } } return(userMotorcycles); } catch (Exception) { throw; } finally { connection.Close(); } }
public UserMotorcycleViewModel GetMotorcycleForMainForm(int motorcycleId) { UserMotorcycleViewModel model = new UserMotorcycleViewModel(); try { if (connection.State == ConnectionState.Closed) { connection.Open(); } StringBuilder getMotorcycleInfoQuery = new StringBuilder(); getMotorcycleInfoQuery.Append(" select md.id, md.make, md.model "); getMotorcycleInfoQuery.Append(" from motorcycledata md "); getMotorcycleInfoQuery.Append($" where md.id = {motorcycleId} "); command.CommandText = getMotorcycleInfoQuery.ToString(); using (reader = command.ExecuteReader()) { while (reader.Read()) { model.MotorcycleId = int.Parse(reader["id"].ToString()); model.Make = reader["make"].ToString(); model.Model = reader["model"].ToString(); } } } catch (Exception ex) { logger.LogExceptionText(ex.ToString(), $"Motorcycle ID e:{motorcycleId}"); } finally { connection.Close(); } return(model); }
public void SetMotorcycleDataVariables(UserMotorcycleViewModel model) { GlobalVariables.CurrentBikeId = model.MotorcycleId; GlobalVariables.CurrentMotorcycleMake = model.Make; GlobalVariables.CurrentMotorcycleModel = model.Model; }