I have a form in Asp.Net for registration of vehicles, where there are DropDownList
that are filled with data from the bank. However, when you click on Write it returns the error
System.Exception: 'IT WAS NOT POSSIBLE TO RECORD System.ArgumentOutOfRangeException: 'dpModel' has a SelectedValue which is invalid because it does not exist in the list of items.
Follow the code below:
public partial class index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CarregarDono();
CarregarModelo();
}
BLL_Cliente bllcliente = new BLL_Cliente();
BLL_Modelo bllmodelo = new BLL_Modelo();
BLL_Veiculo bllveiculo = new BLL_Veiculo();
DTO_Veiculo veiculo = new DTO_Veiculo();
private void Limpar()
{
txtPlaca.Text = "";
txtAno.Text = "";
dpCor.Text = "";
dpModelo.Text = "";
dpDono.Text = "";
}
private void CarregarDono()
{
dpDono.DataSource = bllcliente.ListarClientes();
dpDono.DataValueField = "id";
dpDono.DataTextField = "nome";
dpDono.DataBind();
}
private void CarregarModelo()
{
dpModelo.DataSource = bllmodelo.ListarTodosModelos();
dpModelo.DataValueField = "id";
dpModelo.DataTextField = "nome";
dpModelo.DataBind();
}
protected void btnCadastrar_Click(object sender, EventArgs e)
{
try
{
veiculo.Placa = txtPlaca.Text;
veiculo.Ano = txtAno.Text;
veiculo.Cor = dpCor.SelectedValue;
veiculo.Id_dono = int.Parse(dpDono.SelectedValue);
veiculo.Id_modelo = int.Parse(dpModelo.SelectedValue);
bllveiculo.InserirVeiculo(veiculo);
string message = "Cadastro efetuado com sucesso";
Response.Write("<script>alert('" + message + "');</script>");
Limpar();
}
catch (Exception ex)
{
{ throw new Exception("NAO FOI POSSIVEL GRAVAR" + ex); }
}
}
}