I have a GridView where several of the columns are hidden i.e. Visible="false" .
My GridView fills its columns with the information from a .txt file , later I want to include these loaded data in my DB .
The problem is that when doing the INSERT INTO it gives me the following error:
The input string is not in the correct format.
But if the columns mentioned above I do not place them hidden, that is, I place them Visible="true" .
I can perform the INSERT INTO without any problem.
My question is how do I get the value of those columns that I have placed hidden and then insert them.
Attachment HTML code of the GridView :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" OnRowDataBound="GridView1_RowDataBound" Width="100%">
<Columns>
<asp:BoundField DataField="Operador" HeaderText="Operador" >
<ControlStyle Width="400px" />
<FooterStyle Width="400px" />
<HeaderStyle HorizontalAlign="Left" Width="400px" />
<ItemStyle Font-Bold="True" HorizontalAlign="Left" Width="400px" />
</asp:BoundField>
<asp:BoundField DataField="AcumuladoDeAnuladosMes" HeaderText="Acum De Anul Mes" DataFormatString="{0:N2}" Visible = "true" />
<asp:BoundField DataField="VentaBruta" HeaderText="Venta Bruta" DataFormatString="{0:N2}" Visible = "true" />
<asp:BoundField DataField="Anulaciones" HeaderText="Anulaciones" DataFormatString="{0:N2}" Visible = "true" />
<asp:BoundField DataField="Devoluciones" HeaderText="Devoluciones" Visible = "true" />
<asp:BoundField DataField="VentasNetas" HeaderText="Ventas Netas" DataFormatString="{0:N2}" Visible = "true" />
<asp:BoundField DataField="IVA" HeaderText="IVA" DataFormatString="{0:N2}" Visible = "true" />
<asp:BoundField DataField="NetoAcumulado" HeaderText="Neto Acumulado" DataFormatString="{0:N2}" Visible = "true" />
<asp:BoundField DataField="TotalVentas" HeaderText="Total Ventas" DataFormatString="{0:N2}" Visible="true" />
< !--ESTAS 2 COLUMNAS LAS COLOCO EN FALSE PUES NO DEBEN MOSTRARSE-- >
< !--SI LAS COLOCO EN FALSE ME DA ERROR-- >
< !--SI LAS COLOCO EN TRUE REALIZA MI INSERT-- >
< asp:BoundField DataField="AcumPosPeriodoActual" HeaderText="Acum Pos Periodo Actual" DataFormatString="{0:N2}" Visible ="false" />
<asp:BoundField DataField="AcumNegPeriodoActual" HeaderText="Acum Neg Periodo Actual" DataFormatString="{0:N2}" Visible ="false" />
</Columns>
<FooterStyle BackColor="#FF9900" ForeColor="White" HorizontalAlign="Right"/>
<HeaderStyle BackColor="Black" Font-Size="Small" ForeColor="White" HorizontalAlign="Left" VerticalAlign="Top" Font-Bold="True" Font-Names="Arial" />
<RowStyle Font-Size="Small" HorizontalAlign="Right" Font-Names="Arial" />
</asp:GridView>
Annex INSERT INTO code :
protected void btnInsert(object sender, EventArgs e)
{
string cs = (Session[CWAuthorization.kCWAuthorizationKey] as CWAuthorization).allowedStore;
string server = cs + "sql";
string bd = "HYPER+DB";
string user = "UserReportePosWeb";
string password = "R3p0rt3P0sW3b";
string cadenaconexion = "Data Source=" + server +
";Database=" + bd +
";User Id=" + user +
";Password=" + password +
"";
SqlConnection conex = new SqlConnection(cadenaconexion);
conex.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO REPORTE_Z4 VALUES " +
"(@Operador," +
" @AcumuladoDeAnuladosMes," +
" @VentaBruta," +
" @Anulaciones," +
" @Devoluciones," +
" @VentasNetas," +
" @IVA," +
" @NetoAcumulado," +
" @TotalVentas," +
" @AcumPosPeriodoActual," +
" @AcumNegPeriodoActual)", conex);
try
{
foreach (GridViewRow row in GridView1.Rows)
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Operador", Convert.ToString(row.Cells[0].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@AcumuladoDeAnuladosMes", Convert.ToDecimal(row.Cells[1].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@VentaBruta", Convert.ToDecimal(row.Cells[2].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@Anulaciones", Convert.ToDecimal(row.Cells[3].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@Devoluciones", Convert.ToDecimal(row.Cells[4].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@VentasNetas", Convert.ToDecimal(row.Cells[5].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@IVA", Convert.ToDecimal(row.Cells[6].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@NetoAcumulado", Convert.ToDecimal(row.Cells[7].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@TotalVentas", Convert.ToDecimal(row.Cells[8].Text.ToString().Trim()));
// ESTAS DOS LINEAS ESTAN OCULTAS EN MI GRIDVIEW Y ES AQUI DONDE ME ARROJA EL ERROR
// La cadena de entrada no tiene el formato correcto.
// QUE PUEDO AGREGARLE A ESAS LINEAS PARA OBTENER EL VALOR DE ESAS COLUMNAS OCULTAS
cmd.Parameters.AddWithValue("@AcumPosPeriodoActual", Convert.ToDecimal(row.Cells[9].Text.ToString().Trim()));
cmd.Parameters.AddWithValue("@AcumNegPeriodoActual", Convert.ToDecimal(row.Cells[10].Text.ToString().Trim()));
cmd.ExecuteNonQuery();
}
lblStatus.Text = "REGISTROS INSERTADOS";
}
catch (Exception ex)
{
lblStatus.Text = "ERROR: " + ex.Message;
}
finally
{
conex.Close();
}
}
For some reason that I do not know, the attribute
Visible="false"
does not allow the field to be collected, if you want the field to be hidden but you can obtain its data, use css classes that hide the field but allow its manipulation, for example:Hope this can help you.