Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/03/2011, 22:46
Avatar de GzaPro
GzaPro
 
Fecha de Ingreso: marzo-2011
Ubicación: San Nicolas de los Arroyos
Mensajes: 5
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: Error ABM C# "Referencia al objeto no establecida como instancia de un obj

y este es el código del formulario Clientes:

Código C:
Ver original
  1. namespace abm
  2. {
  3.     public partial class frmCliente : Form
  4.     {
  5.         public frmCliente()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         /*VARIABLE PARA ID DE CLIENTE*/
  11.         string ID;
  12.  
  13.         /*VARIABLE QUE INDICA SI SE PULSA NUEVO O MODIFICAR*/
  14.         private int temp = 0;
  15.  
  16.         /*VARIABLE PARA LA FECHA ACTUAL*/
  17.         DateTime FechaActual = DateTime.Now;
  18.  
  19.         public void MostrarGrid()
  20.         {
  21.             clientes persona = new clientes();
  22.             dtgDetalle.DataSource = persona.ListarCliente();
  23.         }
  24.  
  25.         /*MÉTODO PARA MOSTRAR LA LOCALIDAD*/
  26.         public void MostrarLocalidad()
  27.         {
  28.             localidades milocalidad = new localidades();
  29.             cboLocalidad.DataSource = milocalidad.ListarLocalidades();
  30.             cboLocalidad.DisplayMember = "nombre_localidad";
  31.             cboLocalidad.ValueMember = "id_localidad";
  32.         }
  33.  
  34.        
  35.  
  36.         public void GuardarCliente()
  37.         {
  38.             if (txtNombre.Text.Trim().Length > 0)
  39.  
  40.             {
  41.                 clientes persona = new clientes();
  42.                
  43.                 if (temp == 0)
  44.                 {
  45.                     persona.AgregarCliente(txtNombre.Text, txtApellido.Text, txtCuil.Text, txtDireccion.Text, Convert.ToInt32(cboLocalidad.SelectedValue.ToString()), txtTelefono.Text, txtCelular.Text, txtEmail.Text, FechaActual);
  46.                 }
  47.                 else
  48.                 {
  49.                     persona.ModificarCliente(Convert.ToInt32(ID), txtNombre.Text, txtApellido.Text, txtCuil.Text, txtDireccion.Text, Convert.ToInt32(cboLocalidad.SelectedValue.ToString()), txtTelefono.Text, txtCelular.Text, txtEmail.Text, FechaActual);
  50.                 }
  51.             }
  52.         }
  53.  
  54.         private void btnNuevo_Click(object sender, EventArgs e)
  55.         {
  56.             temp = 0;
  57.  
  58.             //VALORES EN BLANCO//
  59.             txtNombre.Text = "";
  60.             txtApellido.Text = "";
  61.             txtCuil.Text = "";
  62.             txtDireccion.Text = "";
  63.             txtTelefono.Text = "";
  64.             txtCelular.Text = "";
  65.             txtEmail.Text = "";
  66.             cboLocalidad.Text = "";
  67.  
  68.             //CONTROLES//
  69.             txtNombre.ReadOnly = false;
  70.             txtApellido.ReadOnly = false;
  71.             txtDireccion.ReadOnly = false;
  72.             txtCuil.ReadOnly = false;
  73.             txtTelefono.ReadOnly = false;
  74.             txtCelular.ReadOnly = false;
  75.             txtEmail.ReadOnly = false;
  76.             cboLocalidad.Enabled = true;
  77.  
  78.             //BOTONES//
  79.             btnNuevo.Enabled = false;
  80.             btnGuardar.Enabled = true;
  81.             btnModificar.Enabled = false;
  82.             btnEliminar.Enabled = false;
  83.             btnListar.Enabled = false;
  84.             btnCancelar.Enabled = true;
  85.             btnAtras.Enabled = false;
  86.  
  87.             //GRID//
  88.             dtgDetalle.Enabled = false;
  89.  
  90.             //FOCO//
  91.             txtNombre.Focus();
  92.         }
  93.  
  94.         private void btnGuardar_Click(object sender, EventArgs e)
  95.         {
  96.             GuardarCliente();
  97.         }
  98.  
  99.         private void btnModificar_Click(object sender, EventArgs e)
  100.         {
  101.             if ((dtgDetalle.Rows.Count > 0) && (dtgDetalle.CurrentRow.Cells[0].Value != null))
  102.             {
  103.                 temp = 1;
  104.                 ID = dtgDetalle.CurrentRow.Cells[0].Value.ToString();
  105.                
  106.                 /*CONTROLES*/
  107.                 txtNombre.ReadOnly = false;
  108.                 txtApellido.ReadOnly = false;
  109.                 txtDireccion.ReadOnly = false;
  110.                 txtCuil.ReadOnly = false;
  111.                 txtTelefono.ReadOnly = false;
  112.                 txtCelular.ReadOnly = false;
  113.                 txtEmail.ReadOnly = false;
  114.                 cboLocalidad.Enabled = true;
  115.                
  116.                 /*BOTONES*/
  117.                 btnNuevo.Enabled = false;
  118.                 btnGuardar.Enabled = true;
  119.                 btnModificar.Enabled = false;
  120.                 btnCancelar.Enabled = true;
  121.                 btnEliminar.Enabled = false;
  122.                 btnListar.Enabled = false;
  123.                 btnAtras.Enabled = false;
  124.                
  125.                 /*GRID*/
  126.                 dtgDetalle.Enabled = false;
  127.  
  128.             }
  129.         }
  130.  
  131.         private void btnEliminar_Click(object sender, EventArgs e)
  132.         {
  133.              if ((dtgDetalle.Rows.Count > 0) && (dtgDetalle.CurrentRow.Cells[0].Value != null))
  134.             {
  135.                 DialogResult resultado = MessageBox.Show("¿Está seguro que desea eliminar?", "Eliminar", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  136.                 if (resultado == DialogResult.Yes)
  137.                 {
  138.                     ID = dtgDetalle.CurrentRow.Cells[0].Value.ToString();
  139.                     clientes persona = new clientes();
  140.                     persona.EliminarCliente(Convert.ToInt32(ID));
  141.                     MostrarGrid();
  142.                 }
  143.             }
  144.                 else
  145.                 {
  146.                 MessageBox.Show("No ha sellecionado ningún dato", "Eliminar", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  147.                 }
  148.            
  149.         }
  150.  
  151.         private void btnCancelar_Click(object sender, EventArgs e)
  152.         {
  153.             /*VALORES EN BLANCO*/
  154.             txtNombre.Text = "";
  155.             txtApellido.Text = "";
  156.             txtCuil.Text = "";
  157.             txtDireccion.Text = "";
  158.             txtTelefono.Text = "";
  159.             txtCelular.Text = "";
  160.             txtEmail.Text = "";
  161.            
  162.             /*CONTROLES*/
  163.             txtNombre.ReadOnly = true;
  164.             txtApellido.ReadOnly = true;
  165.             txtCuil.ReadOnly = true;
  166.             txtDireccion.ReadOnly = true;
  167.             cboLocalidad.Enabled = false;
  168.             txtTelefono.ReadOnly = true;
  169.             txtCelular.ReadOnly = true;
  170.             txtEmail.ReadOnly = true;
  171.  
  172.             /*BOTONES*/
  173.             btnNuevo.Enabled = true;
  174.             btnGuardar.Enabled = false;
  175.             btnModificar.Enabled = false;
  176.             btnEliminar.Enabled = false;
  177.             btnCancelar.Enabled = false;
  178.             btnListar.Enabled = true;
  179.             btnAtras.Enabled = true;
  180.  
  181.             /*GRID*/
  182.             dtgDetalle.Enabled = true;
  183.             txtNombre.Focus();
  184.         }
  185.  
  186.         private void btnListar_Click(object sender, EventArgs e)
  187.         {
  188.             btnAtras.Enabled = true;
  189.             MostrarGrid();
  190.         }
  191.  
  192.         private void dtgDetalle_CellEnter(object sender, DataGridViewCellEventArgs e)
  193.         {
  194.             /*ORDENAMIENTO DE COLUMNAS*/
  195.             txtNombre.Text = dtgDetalle.CurrentRow.Cells[1].Value.ToString();
  196.             txtApellido.Text = dtgDetalle.CurrentRow.Cells[2].Value.ToString();
  197.             txtCuil.Text = dtgDetalle.CurrentRow.Cells[3].Value.ToString();
  198.             txtDireccion.Text = dtgDetalle.CurrentRow.Cells[4].Value.ToString();
  199.             cboLocalidad.Text = dtgDetalle.CurrentRow.Cells[5].Value.ToString();
  200.             txtTelefono.Text = dtgDetalle.CurrentRow.Cells[6].Value.ToString();
  201.             txtCelular.Text = dtgDetalle.CurrentRow.Cells[7].Value.ToString();
  202.             txtEmail.Text = dtgDetalle.CurrentRow.Cells[8].Value.ToString();
  203.  
  204.             /*ENCABEZADO DE COLUMNAS*/
  205.             dtgDetalle.Columns[0].HeaderText = "Código";
  206.             dtgDetalle.Columns[1].HeaderText = "Nombre";
  207.             dtgDetalle.Columns[2].HeaderText = "Apellido";
  208.             dtgDetalle.Columns[3].HeaderText = "Cuil";
  209.             dtgDetalle.Columns[4].HeaderText = "Dirección";
  210.             dtgDetalle.Columns[5].HeaderText = "Código Localidad";
  211.             dtgDetalle.Columns[6].HeaderText = "Teléfono";
  212.             dtgDetalle.Columns[7].HeaderText = "Celular";
  213.             dtgDetalle.Columns[8].HeaderText = "Email";
  214.             dtgDetalle.Columns[9].HeaderText = "Fecha registro";
  215.  
  216.             /*ESTADO DE BOTONES*/
  217.             btnModificar.Enabled = true;
  218.             btnEliminar.Enabled = true;
  219.         }
  220.  
  221.        
  222.        
  223.     }
  224. }

El problema viene a que cuando le doy a guardar me sale el error de "referencia a objeto no establecida como instancia de un objeto", "nullexception", y tampoco me trae los datos de la tabla localidades al cboLocalidades.

Porfavor necesito que me ayuden y me digan que es lo que pasa, sinceramente pienso que es una tontería, pero no encuentro que es!

Saludos a todos y gracias de ante mano.