0
I have an Asp.Net form for vehicle registration, where there are DropDownList
which are filled with database data. However, clicking Save returns the error
System.Exception: 'Unable to record System.Argued tofrangeexception: 'dpModel' has a Selectedvalue which is invalid because it does not exist in the list of items.
Follow the code:
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); }
}
}
}
Where the error occurs in
bllInserir.InserirVeiculo()
? you debugged the code and verified that you are actually receiving a warm value for theId_Modelo
(which is not null and exists in the database)?– Leandro Angelo
In vdd it does not give error in bll it gives error in "vehicle.Id_model = int. Parse(dpModel.Selectedvalue);" but I just checked in debug and it gets a value validated yes.
– Gabr13l_Alm31da