DataTable.Load gets blanks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • george1106
    New Member
    • Oct 2006
    • 14

    DataTable.Load gets blanks

    Hi. I have a little trouble here.

    when I use a IDataReader to get info from a DataBase, and try to put it into a DataTable (C#), I just get blanks. I have spaces for all the recods in the tabla where the reader comes from, buy I don't have data.

    Any help will be appreciate.
    Thanks
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Please post the relevant code...

    Comment

    • george1106
      New Member
      • Oct 2006
      • 14

      #3
      Code:
      /* these are my declarations: */
              /// <summary>
              /// conection to db
              /// </summary>
              private IDbConnection conexion = null;
              /// <summary>
              /// command used in multiple queryes
              /// </summary>
              private IDbCommand command = null;
              /// <summary>
              /// reader to hold data
              /// </summary>
              protected IDataReader reader;
      
      /* i make de read in this function: */
      // cadena is the SQL sentence with wich I make the query
      
              protected bool LeerDeBDTR(string cadena)
              {
                  if (conexion.State == ConnectionState.Open)
                  {
                      this.command = this.conexion.CreateCommand();
                      this.command.CommandType = CommandType.Text;
                      command.CommandText = cadena;
                      reader = command.ExecuteReader(CommandBehavior.Default);
                      return true;
                  }
                  return false;
              }
      
      /* and here is where I try to load data from datareader to dataTable */
      
      private DataSet LlenarDataSet(DataSetMaker maker, string tabla, string cadenaDeConsulta)
              {
                  DataTable aux = new DataTable(tabla);
                  reader = null;
                  if (this.LeerDeBDTR(cadenaDeConsulta))
                  {
                      if (reader.FieldCount > 0)
                          aux.Load(reader);
                      maker.Datos.Tables.Add(aux);
                  }
                  reader.Close();
                  return maker.Datos;
              }
      
      /* the DataSetMaker is only a container for the dataSet. */
      
          class DataSetMaker
          {
              DataSet dsDatos;
              /// <summary>
              /// Constructor principal
              /// </summary>
              /// <param name="nombre">nombre del DataSet</param>
              public DataSetMaker(string nombre)
              {
                  dsDatos = new DataSet();
                  dsDatos.DataSetName = nombre;
              }
              /// <summary>
              /// Get. DataSet para uso externo
              /// </summary>
              public DataSet Datos
              {
                  get
                  {
                      return dsDatos;
                  }
              }
              /// <summary>
              /// Crea una tabla en el data set
              /// </summary>
              /// <param name="nombre">nombre de la tabla</param>
              public void CreateTabla(string nombre)
              {
                  if (!dsDatos.Tables.Contains(nombre))
                      dsDatos.Tables.Add(nombre);
              }
         }
      thanks in advance.
      Last edited by kenobewan; Feb 21 '07, 04:34 AM. Reason: Add code tags

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        I believe that your problem of not returning the data relates the return true after the execute reader and then you set the reader to null. How is the data getting from your execute statement to the datareader and table? Also I believe that trying to use your datareader in a separate function doesn't help...

        Here is an article that may help:
        DataReader made simple
        or use a dataset:
        Dynamic Database Creation - 2

        Comment

        • george1106
          New Member
          • Oct 2006
          • 14

          #5
          thank you. it helped a lot :D

          Comment

          Working...