hasta me doy cuenta que el código que pase en realidad es de el apartado de ventas y ese si da por que el ddl si trabaja directamente con int, en el caso de productos, hice una conexion de datos al ddl atraves de asistente
<div class="form-group">
<label>Marca</label>
</div>
<div class="form-group">
<asp:DropDownList ID="ddlMarca" runat="server" DataSourceID="SqlDataSourceMarca" DataTextField="Marca" DataValueField="Marca" Width="184px" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceMarca" runat="server" ConnectionString="<%$ ConnectionStrings:SistemCSTConnectionString %>" SelectCommand="SELECT [Marca] FROM [MARCAS]"></asp:SqlDataSource>
</div>
<div class="form-group">
<label>Categoria</label>
</div>
<div class="form-group">
<asp:DropDownList ID="ddlCategoria" runat="server" DataSourceID="SqlDataSourceCate" DataTextField="Categoria" DataValueField="Categoria" Width="184px" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCate" runat="server" ConnectionString="<%$ ConnectionStrings:SistemCSTConnectionString %>" SelectCommand="SELECT [Categoria] FROM [CATEGORIAS]"></asp:SqlDataSource>
Y en caso del estos dos la relación esta obviamente con el id de sus respectivas tablas, pero como seria difícil identificar marcas o categorías por id, quise que mostrara el nombre de la categoría y creo que es ahí donde esta el problema pero no se como solucionarlo
Tengo dos DropDownList
y en la base de datos son dos claves foráneas en una tabla, como saben tipo int
, pero en mi proyecto de ASP.NET hice un llenado de los dos DDL con SqlDataSource
para que me salgan los nombres de las categorías y marca y no ingresar un 1 o 2 ya que si tengo muchas marcas o categorías seria difícil identificarlos, pero al momento de dar guardar me dice que no tiene el formato correcto. Aquí el mensaje:
La excepción se generó originalmente en esta pila de llamadas: System.Number.StringToNumber(string, System.Globalization.NumberStyles, ref System.Number.NumberBuffer, System.Globalization.NumberFormatInfo, bool) System.Number.ParseInt32(string, System.Globalization.NumberStyles, System.Globalization.NumberFormatInfo) System.Convert.ToInt32(string) CapaPresentacion.fmrProductos.btnAgregar_Click(object, System.EventArgs) en fmrProductos.aspx.cs System.Web.UI.WebControls.Button.OnClick(System.EventArgs) System.Web.UI.WebControls.Button.RaisePostBackEvent(string) System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(string) System.Web.UI.Page.RaisePostBackEvent(System.Web.UI.IPostBackEventHandler, string) System.Web.UI.Page.RaisePostBackEvent(System.Collections.Specialized.NameValueCollection) System.Web.UI.Page.ProcessRequestMain(bool, bool)
[![Tabla][1]][1]
Les dejo el codigo que uso en C#:
protected void btnAgregar_Click(object sender, EventArgs e)
{
if (cn.State == ConnectionState.Closed)
cn.Open();
SqlCommand sqlCmd = new SqlCommand("ProductosCreateOrUpdate", cn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.AddWithValue("@id", (hfProductoID.Value == "" ? 0 : Convert.ToInt32(hfProductoID.Value)));
sqlCmd.Parameters.AddWithValue("@descripcion", txtDescripcion.Text.Trim());
sqlCmd.Parameters.AddWithValue("@idmarca", Convert.ToInt32(ddlMarca.SelectedValue?.Trim()));
sqlCmd.Parameters.AddWithValue("@idcategoria", Convert.ToInt32(ddlCategoria.SelectedValue?.Trim()));
sqlCmd.Parameters.AddWithValue("@existencia", Convert.ToInt32(txtExistencia.Text.Trim()));
sqlCmd.Parameters.AddWithValue("@precioA", float.Parse(txtprecioA.Text.Trim()));
sqlCmd.Parameters.AddWithValue("@precioB", float.Parse(txtpreciob.Text.Trim()));
sqlCmd.Parameters.AddWithValue("@precioC", float.Parse(txtprecioc.Text.Trim()));
sqlCmd.Parameters.AddWithValue("@precioD", float.Parse(txtpreciod.Text.Trim()));
sqlCmd.ExecuteNonQuery();
cn.Close();
string id = hfProductoID.Value;
clear();
if (id == "")
lblSuccesseMessage.Text = "Agregado Correctamente";
else
lblSuccesseMessage.Text = "Modificado Correctamente";
FillGridView();
}
ALTER PROC [dbo].[ProductosCreateOrUpdate]
@id int,
@descripcion varchar(100),
@idmarca int,
@idcategoria int,
@existencia int,
@precioA float,
@precioB float,
@precioC float,
@precioD float
AS
If(@id=0)
BEGIN
INSERT INTO PRODUCTOS(descripcion, idMarca, idCateg, existencia, precioA, precioB,
precioC,PrecioD)
VALUES (@descripcion, @idmarca, @idcategoria, @existencia, @precioA, @precioB,
@precioC, @PrecioD)
END
ELSE
BEGIN
UPDATE PRODUCTOS
SET
descripcion = @descripcion,
idMarca= @idmarca,
idCateg=@idcategoria,
existencia =@existencia,
precioA=@precioA,
precioB=@precioB,
precioC=@precioC,
PrecioD=@PrecioD
WHERE id_prod = @id
END
[1]: https://i.stack.imgur.com/siU3U.png
Convert.ToInt32
no es un número entero. Debuguea y comprueba esos campos (apostaría por el ddlDescripcion.Text :P)