OracleDataReader Read(); return false and System.NullReferenceException












0














I have done this manny times but now I get a error and I don't know how to fix it. The variables aren't null, they have value; I tried this in the DB and all is ok it return 1 row



List<Partido_E> lista = new List<Partido_E>();

try
{
conexion = bd.LeerDeBaseDeDatos();
orden = new OracleCommand(
@"select * from partido
where TO_CHAR(fecha, 'DD/MM/YYYY') = :anyo AND
equipo_l = :equipol AND
equipo_v = :equipov ", conexion);

orden.Parameters.Add(new OracleParameter("anyo", fecha));
orden.Parameters.Add(new OracleParameter("equipol", equipoL));
orden.Parameters.Add(new OracleParameter("equipov", equipoV));
orden.BindByName = true;

lector = orden.ExecuteReader();

while(lector.Read())
{
lista.Add(new Partido_E(lector.GetString(0),
lector.GetString(1),
lector.GetDateTime(2),
lector.GetString(3),
lector.GetString(4),
lector.IsDBNull(5) ? 0 : lector.GetInt32(5),
lector.IsDBNull(6) ? 0 : lector.GetInt32(6),
lector.IsDBNull(7) ? 0 : lector.GetInt32(7)
));
}


lector.Close();
lector.Dispose();
orden.Dispose();

bd.CerrarConexion();
}
catch (Exception e)
{
Console.WriteLine("Error " + e.ToString());
Console.ReadLine();
}

return lista;









share|improve this question
























  • You should prefer WHERE fecha = :anyo and orden.Parameters.Add(new OracleParameter("anyo", OracleDbType.Date)).Value = fecha.Date; (assuming fecha is a DateTime value rather than a string)
    – Wernfried Domscheit
    Nov 20 at 7:20












  • The fecha is string, isn't DateTime
    – xLioneth
    Nov 20 at 11:30
















0














I have done this manny times but now I get a error and I don't know how to fix it. The variables aren't null, they have value; I tried this in the DB and all is ok it return 1 row



List<Partido_E> lista = new List<Partido_E>();

try
{
conexion = bd.LeerDeBaseDeDatos();
orden = new OracleCommand(
@"select * from partido
where TO_CHAR(fecha, 'DD/MM/YYYY') = :anyo AND
equipo_l = :equipol AND
equipo_v = :equipov ", conexion);

orden.Parameters.Add(new OracleParameter("anyo", fecha));
orden.Parameters.Add(new OracleParameter("equipol", equipoL));
orden.Parameters.Add(new OracleParameter("equipov", equipoV));
orden.BindByName = true;

lector = orden.ExecuteReader();

while(lector.Read())
{
lista.Add(new Partido_E(lector.GetString(0),
lector.GetString(1),
lector.GetDateTime(2),
lector.GetString(3),
lector.GetString(4),
lector.IsDBNull(5) ? 0 : lector.GetInt32(5),
lector.IsDBNull(6) ? 0 : lector.GetInt32(6),
lector.IsDBNull(7) ? 0 : lector.GetInt32(7)
));
}


lector.Close();
lector.Dispose();
orden.Dispose();

bd.CerrarConexion();
}
catch (Exception e)
{
Console.WriteLine("Error " + e.ToString());
Console.ReadLine();
}

return lista;









share|improve this question
























  • You should prefer WHERE fecha = :anyo and orden.Parameters.Add(new OracleParameter("anyo", OracleDbType.Date)).Value = fecha.Date; (assuming fecha is a DateTime value rather than a string)
    – Wernfried Domscheit
    Nov 20 at 7:20












  • The fecha is string, isn't DateTime
    – xLioneth
    Nov 20 at 11:30














0












0








0







I have done this manny times but now I get a error and I don't know how to fix it. The variables aren't null, they have value; I tried this in the DB and all is ok it return 1 row



List<Partido_E> lista = new List<Partido_E>();

try
{
conexion = bd.LeerDeBaseDeDatos();
orden = new OracleCommand(
@"select * from partido
where TO_CHAR(fecha, 'DD/MM/YYYY') = :anyo AND
equipo_l = :equipol AND
equipo_v = :equipov ", conexion);

orden.Parameters.Add(new OracleParameter("anyo", fecha));
orden.Parameters.Add(new OracleParameter("equipol", equipoL));
orden.Parameters.Add(new OracleParameter("equipov", equipoV));
orden.BindByName = true;

lector = orden.ExecuteReader();

while(lector.Read())
{
lista.Add(new Partido_E(lector.GetString(0),
lector.GetString(1),
lector.GetDateTime(2),
lector.GetString(3),
lector.GetString(4),
lector.IsDBNull(5) ? 0 : lector.GetInt32(5),
lector.IsDBNull(6) ? 0 : lector.GetInt32(6),
lector.IsDBNull(7) ? 0 : lector.GetInt32(7)
));
}


lector.Close();
lector.Dispose();
orden.Dispose();

bd.CerrarConexion();
}
catch (Exception e)
{
Console.WriteLine("Error " + e.ToString());
Console.ReadLine();
}

return lista;









share|improve this question















I have done this manny times but now I get a error and I don't know how to fix it. The variables aren't null, they have value; I tried this in the DB and all is ok it return 1 row



List<Partido_E> lista = new List<Partido_E>();

try
{
conexion = bd.LeerDeBaseDeDatos();
orden = new OracleCommand(
@"select * from partido
where TO_CHAR(fecha, 'DD/MM/YYYY') = :anyo AND
equipo_l = :equipol AND
equipo_v = :equipov ", conexion);

orden.Parameters.Add(new OracleParameter("anyo", fecha));
orden.Parameters.Add(new OracleParameter("equipol", equipoL));
orden.Parameters.Add(new OracleParameter("equipov", equipoV));
orden.BindByName = true;

lector = orden.ExecuteReader();

while(lector.Read())
{
lista.Add(new Partido_E(lector.GetString(0),
lector.GetString(1),
lector.GetDateTime(2),
lector.GetString(3),
lector.GetString(4),
lector.IsDBNull(5) ? 0 : lector.GetInt32(5),
lector.IsDBNull(6) ? 0 : lector.GetInt32(6),
lector.IsDBNull(7) ? 0 : lector.GetInt32(7)
));
}


lector.Close();
lector.Dispose();
orden.Dispose();

bd.CerrarConexion();
}
catch (Exception e)
{
Console.WriteLine("Error " + e.ToString());
Console.ReadLine();
}

return lista;






c# oracle oracle11g odp.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 4:32









T.S.

9,63593153




9,63593153










asked Nov 20 at 1:50









xLioneth

12




12












  • You should prefer WHERE fecha = :anyo and orden.Parameters.Add(new OracleParameter("anyo", OracleDbType.Date)).Value = fecha.Date; (assuming fecha is a DateTime value rather than a string)
    – Wernfried Domscheit
    Nov 20 at 7:20












  • The fecha is string, isn't DateTime
    – xLioneth
    Nov 20 at 11:30


















  • You should prefer WHERE fecha = :anyo and orden.Parameters.Add(new OracleParameter("anyo", OracleDbType.Date)).Value = fecha.Date; (assuming fecha is a DateTime value rather than a string)
    – Wernfried Domscheit
    Nov 20 at 7:20












  • The fecha is string, isn't DateTime
    – xLioneth
    Nov 20 at 11:30
















You should prefer WHERE fecha = :anyo and orden.Parameters.Add(new OracleParameter("anyo", OracleDbType.Date)).Value = fecha.Date; (assuming fecha is a DateTime value rather than a string)
– Wernfried Domscheit
Nov 20 at 7:20






You should prefer WHERE fecha = :anyo and orden.Parameters.Add(new OracleParameter("anyo", OracleDbType.Date)).Value = fecha.Date; (assuming fecha is a DateTime value rather than a string)
– Wernfried Domscheit
Nov 20 at 7:20














The fecha is string, isn't DateTime
– xLioneth
Nov 20 at 11:30




The fecha is string, isn't DateTime
– xLioneth
Nov 20 at 11:30












1 Answer
1






active

oldest

votes


















0














Here is your problem - read comments



 while (lector.Read())
{
// if you never come here, your reader is closed
}
lector.Close(); // <-- problem here


Oracle:




An OracleDataReader instance is constructed by a call to the ExecuteReader method of the OracleCommand object. The only properties that can be accessed after the DataReader is closed or has been disposed, are IsClosed and RecordsAffected.




This line lector.Read() can not throw null reference exception because line before that lector = orden.ExecuteReader(); will always return reader. If no rows, it will be closed.



Your code is not wormed well. should be



using (conn = new connection/getconnection)
{
using (cmd = new command)
{
using (reader = cmd.ExecuteReader)
{
} // no need to explicitly dispose/close here
}
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385112%2foracledatareader-read-return-false-and-system-nullreferenceexception%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Here is your problem - read comments



     while (lector.Read())
    {
    // if you never come here, your reader is closed
    }
    lector.Close(); // <-- problem here


    Oracle:




    An OracleDataReader instance is constructed by a call to the ExecuteReader method of the OracleCommand object. The only properties that can be accessed after the DataReader is closed or has been disposed, are IsClosed and RecordsAffected.




    This line lector.Read() can not throw null reference exception because line before that lector = orden.ExecuteReader(); will always return reader. If no rows, it will be closed.



    Your code is not wormed well. should be



    using (conn = new connection/getconnection)
    {
    using (cmd = new command)
    {
    using (reader = cmd.ExecuteReader)
    {
    } // no need to explicitly dispose/close here
    }
    }





    share|improve this answer


























      0














      Here is your problem - read comments



       while (lector.Read())
      {
      // if you never come here, your reader is closed
      }
      lector.Close(); // <-- problem here


      Oracle:




      An OracleDataReader instance is constructed by a call to the ExecuteReader method of the OracleCommand object. The only properties that can be accessed after the DataReader is closed or has been disposed, are IsClosed and RecordsAffected.




      This line lector.Read() can not throw null reference exception because line before that lector = orden.ExecuteReader(); will always return reader. If no rows, it will be closed.



      Your code is not wormed well. should be



      using (conn = new connection/getconnection)
      {
      using (cmd = new command)
      {
      using (reader = cmd.ExecuteReader)
      {
      } // no need to explicitly dispose/close here
      }
      }





      share|improve this answer
























        0












        0








        0






        Here is your problem - read comments



         while (lector.Read())
        {
        // if you never come here, your reader is closed
        }
        lector.Close(); // <-- problem here


        Oracle:




        An OracleDataReader instance is constructed by a call to the ExecuteReader method of the OracleCommand object. The only properties that can be accessed after the DataReader is closed or has been disposed, are IsClosed and RecordsAffected.




        This line lector.Read() can not throw null reference exception because line before that lector = orden.ExecuteReader(); will always return reader. If no rows, it will be closed.



        Your code is not wormed well. should be



        using (conn = new connection/getconnection)
        {
        using (cmd = new command)
        {
        using (reader = cmd.ExecuteReader)
        {
        } // no need to explicitly dispose/close here
        }
        }





        share|improve this answer












        Here is your problem - read comments



         while (lector.Read())
        {
        // if you never come here, your reader is closed
        }
        lector.Close(); // <-- problem here


        Oracle:




        An OracleDataReader instance is constructed by a call to the ExecuteReader method of the OracleCommand object. The only properties that can be accessed after the DataReader is closed or has been disposed, are IsClosed and RecordsAffected.




        This line lector.Read() can not throw null reference exception because line before that lector = orden.ExecuteReader(); will always return reader. If no rows, it will be closed.



        Your code is not wormed well. should be



        using (conn = new connection/getconnection)
        {
        using (cmd = new command)
        {
        using (reader = cmd.ExecuteReader)
        {
        } // no need to explicitly dispose/close here
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 at 4:26









        T.S.

        9,63593153




        9,63593153






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385112%2foracledatareader-read-return-false-and-system-nullreferenceexception%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?