Alguém poderia me explicar por qual motivo está disparando a exceção ao adicionar o registro na base?
Add.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PocoLibrary;
namespace ProjWebTestePoco
{
public partial class Add : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAddCliente_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
try
{
using (NorthwindPocoContext contexto = new NorthwindPocoContext())
{
Customer customer = new Customer();
customer.CustomerID = txtCodigoCliente.Text.Trim().Substring(0, 10);
customer.CompanyName = txtNomeEmpresa.Text.Trim().Substring(0, 80);
contexto.Customers.AddObject(customer);
contexto.SaveChanges();
lblMensagem.Text = "Cliente Registrado com sucesso";
}
}
catch (Exception ex)
{
lblMensagem.Text = "<pre>" + ex.ToString() + "</pre>";
}
}
}
}
}
Customer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace PocoLibrary
{
public class Customer
{
public string CustomerID { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactTitle { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public List<Order> Orders { get; set; }
}
}
System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at ProjWebTestePoco.Add.btnAddCliente_Click(Object sender, EventArgs e)
Pergunta
ursolouco
Salve,
Alguém poderia me explicar por qual motivo está disparando a exceção ao adicionar o registro na base?
Add.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using PocoLibrary; namespace ProjWebTestePoco { public partial class Add : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnAddCliente_Click(object sender, EventArgs e) { if (IsPostBack) { try { using (NorthwindPocoContext contexto = new NorthwindPocoContext()) { Customer customer = new Customer(); customer.CustomerID = txtCodigoCliente.Text.Trim().Substring(0, 10); customer.CompanyName = txtNomeEmpresa.Text.Trim().Substring(0, 80); contexto.Customers.AddObject(customer); contexto.SaveChanges(); lblMensagem.Text = "Cliente Registrado com sucesso"; } } catch (Exception ex) { lblMensagem.Text = "<pre>" + ex.ToString() + "</pre>"; } } } } }Customer.csusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PocoLibrary { public class Customer { public string CustomerID { get; set; } public string CompanyName { get; set; } public string ContactName { get; set; } public string ContactTitle { get; set; } public string Address { get; set; } public string City { get; set; } public string Region { get; set; } public string PostalCode { get; set; } public string Country { get; set; } public string Phone { get; set; } public string Fax { get; set; } public List<Order> Orders { get; set; } } }Add.aspx ExceptionLink para o comentário
Compartilhar em outros sites
2 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.