Error, C # NullReferenceException was unhandled by user code

0

How can I solve this problem of NullReferenceException was unhandled by user code, where I have a table inserted and it tells me that the value that returns to me is null?

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;


    /// <summary>
    ///Clase Cliente
    /// </summary>
    public class clscliente: clsconexion
    {

        string tabla = "Clientes"; /

/

    Nombre de mi tabla
        protected string Nombre, Direccion, Telefono;
        protected int idCliente;
        public clscliente(int idCliente, string Nombre, string Direccion, string Telefono)
        {
    this.idcliente = idcliente;
    this.Nombre = Nombre;
    this.Direccion = Direccion;
    this.Telefono = Telefono;

}

//metodos para establecer y recuperar datos
public int idcliente {
    set { idCliente = value; }
    get { return idCliente; }
}
public string nombre{
    set { Nombre = value; }
    get { return Nombre; }
}
public string direccion
{
    set { Direccion = value; }
    get { return Direccion; }
}
public string telefono
{
    set { Telefono = value; }
    get { return Telefono; }
}
//metodo agregar
public void agregar() {
    conectar(tabla);
    DataRow fila;
    fila = Data.Tables["tabla"].NewRow(); 
    fila["idCliente"] = idcliente;
    fila["Nombre"] = Nombre;
    fila["Direccion"] = Direccion;
    fila["Telefono"] = Telefono;

    Data.Tables[tabla].Rows.Add(fila);
    AdaptadorDatos.Update(Data, tabla);


    }
}
    
asked by G.Fermin 16.04.2017 в 23:15
source

3 answers

0

that exception occurs when accessing an uninitialized object, that is, pointing to null . Put a break point in that line and verify that both Data as Data.Tables["tabla"] are not null .

    
answered by 16.04.2017 в 23:38
0

To use .NewRow () the first thing you should have is an object of type DataTable with its defined columns: see DataTable.NewRow Method () ; there you will see that they create the object, add columns and request a new row.

    
answered by 17.04.2017 в 00:13
0

Check that there is a table called "Table"

    
answered by 05.07.2018 в 10:40