C sharp - a field initializer cannot reference the nonstatic field, method, or property

 
Vista:

a field initializer cannot reference the nonstatic field, method, or property

Publicado por Andrés (4 intervenciones) el 02/12/2011 07:01:43
Hola, estoy programando un formulario donde se tienen que agregar datos (varios) e irlos guardando
en una planilla excel.
El formulario tiene 14 campos que serán las columnas del excel, cuando se completan los 14 campos los limpia (boton aceptar) y debería seguir guardando en la fila de abajo 14 nuevos campos. Luego al terminar la tarea diaria de ingreso se hará click al botón "EXPORTAR" y debería abrirse el archivo excel con todas las filas ingresadas.

Me sale el siguiente error:

A field initializer cannot reference the nonstatic field, method, or property....

Copio el código a continuación, lo coloqué en negrita. Es donde esta el (*)


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Data.OleDb;
using System.Reflection;
using System.Data.SqlClient;
using Microsoft.Office.Interop.Excel;


namespace WindowsFormsApplication1
{

public partial class FormIngresar : Form
{


int cont = 2;
int siguiente = 2;
Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application();
(*) Workbook wb = xls.Workbooks.Add(XlSheetType.xlWorksheet);
(*)Worksheet ws = (Worksheet)xls.ActiveSheet;

public FormIngresar()
{
InitializeComponent();
}

private void buttonAceptar_Click(object sender, EventArgs e)
{

Workbook wb = xls.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)xls.ActiveSheet;

if ((textBoxSondajeId.Text.Equals("")) || (textBoxDesde.Text.Equals("")) || (textBoxHasta.Text.Equals("")) || (textBoxCantMuestras.Text.Equals("")) ||
(textBoxNumBolsa.Text.Equals("")) || (textBoxNumBin.Text.Equals(""))
|| (textBoxTerraza.Text.Equals("")) || (comboBoxTipo.SelectedIndex.Equals(-1)) || (comboBoxTipo.SelectedItem.ToString().Equals("")))

{
MessageBox.Show("Uno o más campos están en blanco. Solamente los campos 'SOLICITUD' y 'N° LABORATORIO' pueden estar en blanco", "Sistema de Almacenamiento #150", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
ws.Cells[1, 1] = "FECHA";
ws.Cells[1, 2] = "SONDAJE ID";
ws.Cells[1, 3] = "TIPO";
ws.Cells[1, 4] = "MUESTRA";
ws.Cells[1, 5] = "DESDE";
ws.Cells[1, 6] = "HASTA";
ws.Cells[1, 7] = "CANT.MUESTRAS";
ws.Cells[1, 8] = "SOLICITUD";
ws.Cells[1, 9] = "BOLSA BIN";
ws.Cells[1, 10] = "NUM BIN";
ws.Cells[1, 11] = "TERRAZA";
ws.Cells[1, 12] = "NUM LAB";
ws.Cells[1, 13] = "LABORATORIO";

ws.Cells[this.cont, 1] = dateTimePickerFecha.Text;
ws.Cells[this.cont, 2] = textBoxSondajeId.Text;
ws.Cells[this.cont, 3] = comboBoxTipo.Text;
ws.Cells[this.cont, 4] = comboBoxMuestra.Text;
ws.Cells[this.cont, 5] = textBoxDesde.Text;
ws.Cells[this.cont, 6] = textBoxHasta.Text;
ws.Cells[this.cont, 7] = textBoxCantMuestras.Text;
ws.Cells[this.cont, 8] = comboBoxSolicitud.Text;
ws.Cells[this.cont, 9] = textBoxNumBolsa.Text;
ws.Cells[this.cont, 10] = textBoxNumBin.Text;
ws.Cells[this.cont, 11] = textBoxTerraza.Text;
ws.Cells[this.cont, 12] = textBoxNumLab.Text;
ws.Cells[this.cont, 13] = comboBoxLab.Text;

MessageBox.Show("Los datos fueron ingresado exitosamente, puede ingresar el siguiente pozo", "Sistema de Almacenamiento #150",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.cont++;

textBoxSondajeId.Text = "";
textBoxCantMuestras.Text = "";
textBoxDesde.Text = "";
textBoxHasta.Text = "";
textBoxNumBin.Text = "";
textBoxNumBolsa.Text = "";
textBoxTerraza.Text = "";
textBoxNumLab.Text = "";
comboBoxTipo.SelectedIndex = -1;
comboBoxSolicitud.SelectedIndex = -1;
comboBoxMuestra.SelectedIndex = -1;
comboBoxLab.SelectedIndex = -1;
}


}

private void FormIngresar_Load(object sender, EventArgs e)
{
dateTimePickerFecha.Format = DateTimePickerFormat.Custom;
}

private void button2_Click(object sender, EventArgs e)
{

DialogResult dr = MessageBox.Show("Saliendo del sistema, se perderán datos no guardados", "Sistema de Almacenamiento #150",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

if (dr == DialogResult.OK)
{
this.Close();
FormLogin NuevoLogin = new FormLogin();
NuevoLogin.Show();
}
else
{
}
}

private void buttonExportar_Click(object sender, EventArgs e)
{
DialogResult Resp = MessageBox.Show("Ha elegido la opción EXPORTAR, ¿Está seguro de haber terminado su KPI diario y desea exportar a Excel?", "Sistema de Almacenamiento #150",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

if (Resp == DialogResult.OK)
{
xls.Visible = true;
}
else
{
}
}
}
}


ESPERO ME PUEDAN AYUDAR
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

a field initializer cannot reference the nonstatic field, method, or property

Publicado por Andrés (4 intervenciones) el 02/12/2011 07:05:14
DEBAJO DEL

private void buttonAceptar_Click(object sender, EventArgs e)
{

ESTO ESTÁ COMENTADO!!!

//Workbook wb = xls.Workbooks.Add(XlSheetType.xlWorksheet);
//Worksheet ws = (Worksheet)xls.ActiveSheet;
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar