Problem with dataset crashing my app

Discussion in 'ASP .Net' started by David, May 7, 2007.

  1. David

    David Guest

    Hi,

    using C# asp.net 1.1

    I am having a problem. If I use the code below, but have null passed as the
    PostingGuid, then by the time I get to fill the dataset, I have "an object
    is not set to an instance of an object". This then totally knocks out any
    connection to my database for the rest of my application until I terminate
    the aspnet process.

    Basically, if I have a null PageGuid, then the result from my query will be
    empty (not sure if it is null).

    If I wrap an if statement around the whole thing (below DS and above return
    DS) checking for PostingGuid null, I don't have a problem, but I feel that
    this is a hack rather than a fix. Also, there could be a case where
    PostingGuid is not null but the return from the database is still empty (or
    null)


    public DataSet PagePropertiesRead(string PostingGuid)
    {
    DataSet DS = new DataSet();

    DataAccessLayer.ProviderFactory DL = new
    DataAccessLayer.ProviderFactory(DB);

    string sql = string.Empty;

    try
    {
    conn = DL.CreateConnection(ConnectionString);
    conn.Open();

    sql = "select * from Page where PageGuid = ? limit 0, 1"; // NOTE:
    This is MySQL

    cmd = DL.CreateCommand(sql, conn);
    cmd = AddParameter(DL, cmd, "@PageGuid", DbType.String, PostingGuid);

    DA = DL.CreateDataAdapter();
    DA.SelectCommand = cmd;

    DA.Fill(DS); <-- BREAKS HERE

    }
    catch (Exception ex)
    {
    string Text = ex.Message;
    }
    finally
    {
    conn.Close();
    }

    return DS;

    }

    --
    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available
    David, May 7, 2007
    #1
    1. Advertising

  2. On May 7, 12:39 pm, "David"
    <> wrote:
    > Hi,
    >
    > using C# asp.net 1.1
    >
    > I am having a problem. If I use the code below, but have null passed as the
    > PostingGuid, then by the time I get to fill the dataset, I have "an object
    > is not set to an instance of an object". This then totally knocks out any
    > connection to my database for the rest of my application until I terminate
    > the aspnet process.
    >
    > Basically, if I have a null PageGuid, then the result from my query will be
    > empty (not sure if it is null).
    >
    > If I wrap an if statement around the whole thing (below DS and above return
    > DS) checking for PostingGuid null, I don't have a problem, but I feel that
    > this is a hack rather than a fix. Also, there could be a case where
    > PostingGuid is not null but the return from the database is still empty (or
    > null)
    >
    > public DataSet PagePropertiesRead(string PostingGuid)
    > {
    > DataSet DS = new DataSet();
    >
    > DataAccessLayer.ProviderFactory DL = new
    > DataAccessLayer.ProviderFactory(DB);
    >
    > string sql = string.Empty;
    >
    > try
    > {
    > conn = DL.CreateConnection(ConnectionString);
    > conn.Open();
    >
    > sql = "select * from Page where PageGuid = ? limit 0, 1"; // NOTE:
    > This is MySQL
    >
    > cmd = DL.CreateCommand(sql, conn);
    > cmd = AddParameter(DL, cmd, "@PageGuid", DbType.String, PostingGuid);
    >
    > DA = DL.CreateDataAdapter();
    > DA.SelectCommand = cmd;
    >
    > DA.Fill(DS); <-- BREAKS HERE
    >
    > }
    > catch (Exception ex)
    > {
    > string Text = ex.Message;
    > }
    > finally
    > {
    > conn.Close();
    > }
    >
    > return DS;
    >
    > }
    >
    > --
    > Best regards,
    > Dave Colliver.http://www.AshfieldFOCUS.com
    > ~~http://www.FOCUSPortals.com- Local franchises available


    I think you should provide a value of a null based datatype

    I'm not sure if DBNull.Value will help here... try it
    Alexey Smirnov, May 7, 2007
    #2
    1. Advertising

  3. David

    David Guest

    Not quite following?

    Do you mean something like...

    if (PostingGuid == null)
    {
    PostingGuid = DBNull.Value;
    }

    ???

    Regards,
    Dave.


    > I think you should provide a value of a null based datatype
    >
    > I'm not sure if DBNull.Value will help here... try it
    >
    David, May 7, 2007
    #3
  4. On the retrieving end
    ---------------------
    You should input check for a null guid and not even try to retrieve data
    where it is null.Based on my reading of your code, this value should never
    be null.

    Of course, another method is to return an empty dataset when it is null. To
    accomplish this, move your query to a stored procedure, where you can
    properly branch it (assuming this a database, like SQL Server, of course).


    On the binding end
    -------------------
    Using a DataSet, you have a couple of choices.

    1. Hack the DataSet itself and hack out the offending error. This is fine,
    with strings (for example), where you can supply a default of String.Empty.
    I would not do this with Guid.
    2. Edit the binding event of the Data Grid and look for the null there,
    where you can supply an empty string for the cell instead, as everything is
    converted to string to output to the browser anyway.

    If you do it this way, you can remove conditions where a user clicking
    something hits a null guid.


    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com

    *********************************************
    Think outside the box!
    *********************************************
    "David" <> wrote in message
    news:...
    > Hi,
    >
    > using C# asp.net 1.1
    >
    > I am having a problem. If I use the code below, but have null passed as
    > the PostingGuid, then by the time I get to fill the dataset, I have "an
    > object is not set to an instance of an object". This then totally knocks
    > out any connection to my database for the rest of my application until I
    > terminate the aspnet process.
    >
    > Basically, if I have a null PageGuid, then the result from my query will
    > be empty (not sure if it is null).
    >
    > If I wrap an if statement around the whole thing (below DS and above
    > return DS) checking for PostingGuid null, I don't have a problem, but I
    > feel that this is a hack rather than a fix. Also, there could be a case
    > where PostingGuid is not null but the return from the database is still
    > empty (or null)
    >
    >
    > public DataSet PagePropertiesRead(string PostingGuid)
    > {
    > DataSet DS = new DataSet();
    >
    > DataAccessLayer.ProviderFactory DL = new
    > DataAccessLayer.ProviderFactory(DB);
    >
    > string sql = string.Empty;
    >
    > try
    > {
    > conn = DL.CreateConnection(ConnectionString);
    > conn.Open();
    >
    > sql = "select * from Page where PageGuid = ? limit 0, 1"; // NOTE:
    > This is MySQL
    >
    > cmd = DL.CreateCommand(sql, conn);
    > cmd = AddParameter(DL, cmd, "@PageGuid", DbType.String, PostingGuid);
    >
    > DA = DL.CreateDataAdapter();
    > DA.SelectCommand = cmd;
    >
    > DA.Fill(DS); <-- BREAKS HERE
    >
    > }
    > catch (Exception ex)
    > {
    > string Text = ex.Message;
    > }
    > finally
    > {
    > conn.Close();
    > }
    >
    > return DS;
    >
    > }
    >
    > --
    > Best regards,
    > Dave Colliver.
    > http://www.AshfieldFOCUS.com
    > ~~
    > http://www.FOCUSPortals.com - Local franchises available
    >
    Cowboy \(Gregory A. Beamer\), May 7, 2007
    #4
  5. On May 7, 2:18 pm, "David"
    <> wrote:
    > Not quite following?
    >
    > Do you mean something like...
    >
    > if (PostingGuid == null)
    > {
    > PostingGuid = DBNull.Value;
    >
    > }
    >
    > ???
    >
    > Regards,
    > Dave.
    >
    >
    >
    > > I think you should provide a value of a null based datatype

    >
    > > I'm not sure if DBNull.Value will help here... try it- Hide quoted text -

    >
    > - Show quoted text -


    yes, but it has to be supported by your DataAccessLayer... For
    example, in ADO.NET, when you have to pass a NULL value to the server,
    you should set the db-variable to DBNull.Value.

    But, you know what? Try to build sql as

    if (PostingGuid == null)
    {
    sql = "select * from Page where PageGuid IS NULL limit 0, 1";
    } else {
    sql = "select * from Page where PageGuid = " + PostingGuid.ToString()
    + " limit 0, 1";
    }

    Maybe the problem is in the "IS NULL" construction...
    Alexey Smirnov, May 7, 2007
    #5
  6. David

    David Guest

    The guid I am passing is actually a string representation of it. The output
    from the database is not actually being displayed but going into an
    httpcontext. The system is now quite complex to explain fully how it works,
    suffice to say that a page is referenced by a guid string. Theoretically, if
    a page is requested that is not represented by a guid string (which is
    exactly how I found this problem) then I have to fail gracefully.

    Your assumptions are correct. The value should never be null, but it can
    happen as I have found out (never considered that particular route.). So,
    how do I get around it? I have currently set up an if statement that
    compares if it is null and just skips the processing but returns an empty
    dataset. This works but it appears to be a hack. (I am not sure what happens
    yet if I pass an invalid guid string...)

    I will have a go with a couple of possibilities (like Alexey's) and try out
    other potential problems (like the one I mention here)

    Thanks guys for your assistance...
    --
    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available



    "Cowboy (Gregory A. Beamer)" <> wrote in
    message news:...
    > On the retrieving end
    > ---------------------
    > You should input check for a null guid and not even try to retrieve data
    > where it is null.Based on my reading of your code, this value should never
    > be null.
    >
    > Of course, another method is to return an empty dataset when it is null.
    > To accomplish this, move your query to a stored procedure, where you can
    > properly branch it (assuming this a database, like SQL Server, of course).
    >
    >
    > On the binding end
    > -------------------
    > Using a DataSet, you have a couple of choices.
    >
    > 1. Hack the DataSet itself and hack out the offending error. This is fine,
    > with strings (for example), where you can supply a default of
    > String.Empty. I would not do this with Guid.
    > 2. Edit the binding event of the Data Grid and look for the null there,
    > where you can supply an empty string for the cell instead, as everything
    > is converted to string to output to the browser anyway.
    >
    > If you do it this way, you can remove conditions where a user clicking
    > something hits a null guid.
    >
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    > http://gregorybeamer.spaces.live.com
    >
    > *********************************************
    > Think outside the box!
    > *********************************************
    > "David" <> wrote in message
    > news:...
    >> Hi,
    >>
    >> using C# asp.net 1.1
    >>
    >> I am having a problem. If I use the code below, but have null passed as
    >> the PostingGuid, then by the time I get to fill the dataset, I have "an
    >> object is not set to an instance of an object". This then totally knocks
    >> out any connection to my database for the rest of my application until I
    >> terminate the aspnet process.
    >>
    >> Basically, if I have a null PageGuid, then the result from my query will
    >> be empty (not sure if it is null).
    >>
    >> If I wrap an if statement around the whole thing (below DS and above
    >> return DS) checking for PostingGuid null, I don't have a problem, but I
    >> feel that this is a hack rather than a fix. Also, there could be a case
    >> where PostingGuid is not null but the return from the database is still
    >> empty (or null)
    >>
    >>
    >> public DataSet PagePropertiesRead(string PostingGuid)
    >> {
    >> DataSet DS = new DataSet();
    >>
    >> DataAccessLayer.ProviderFactory DL = new
    >> DataAccessLayer.ProviderFactory(DB);
    >>
    >> string sql = string.Empty;
    >>
    >> try
    >> {
    >> conn = DL.CreateConnection(ConnectionString);
    >> conn.Open();
    >>
    >> sql = "select * from Page where PageGuid = ? limit 0, 1"; // NOTE:
    >> This is MySQL
    >>
    >> cmd = DL.CreateCommand(sql, conn);
    >> cmd = AddParameter(DL, cmd, "@PageGuid", DbType.String, PostingGuid);
    >>
    >> DA = DL.CreateDataAdapter();
    >> DA.SelectCommand = cmd;
    >>
    >> DA.Fill(DS); <-- BREAKS HERE
    >>
    >> }
    >> catch (Exception ex)
    >> {
    >> string Text = ex.Message;
    >> }
    >> finally
    >> {
    >> conn.Close();
    >> }
    >>
    >> return DS;
    >>
    >> }
    >>
    >> --
    >> Best regards,
    >> Dave Colliver.
    >> http://www.AshfieldFOCUS.com
    >> ~~
    >> http://www.FOCUSPortals.com - Local franchises available
    >>

    >
    David, May 7, 2007
    #6
  7. David

    David Guest

    Cool,

    You can probably see that I have a function to build the cmd.parameters. I
    am passing the value (even if it is null) into the parameters. In my
    function, I am now checking if the passed parameter is null and if so,
    convert it to DBNull.Value.

    In thoery, it will make my code slightly slower I suppose (being checked for
    every parameter being passed) but I think it is a much better way of coding
    around the problem.

    Incidentally, I tried with a value that doesn't exist in my DB. This worked
    even though no returned records. That means that the value was meant to be a
    DBNull, not just any old null (quite what the difference is between them, I
    don't know.)

    --
    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available


    "Alexey Smirnov" <> wrote in message
    news:...
    > On May 7, 2:18 pm, "David"
    > <> wrote:
    >> Not quite following?
    >>
    >> Do you mean something like...
    >>
    >> if (PostingGuid == null)
    >> {
    >> PostingGuid = DBNull.Value;
    >>
    >> }
    >>
    >> ???
    >>
    >> Regards,
    >> Dave.
    >>
    >>
    >>
    >> > I think you should provide a value of a null based datatype

    >>
    >> > I'm not sure if DBNull.Value will help here... try it- Hide quoted
    >> > text -

    >>
    >> - Show quoted text -

    >
    > yes, but it has to be supported by your DataAccessLayer... For
    > example, in ADO.NET, when you have to pass a NULL value to the server,
    > you should set the db-variable to DBNull.Value.
    >
    > But, you know what? Try to build sql as
    >
    > if (PostingGuid == null)
    > {
    > sql = "select * from Page where PageGuid IS NULL limit 0, 1";
    > } else {
    > sql = "select * from Page where PageGuid = " + PostingGuid.ToString()
    > + " limit 0, 1";
    > }
    >
    > Maybe the problem is in the "IS NULL" construction...
    >
    David, May 7, 2007
    #7
  8. On May 7, 5:21 pm, "David"
    <> wrote:
    > Incidentally, I tried with a value that doesn't exist in my DB. This worked
    > even though no returned records. That means that the value was meant to be a
    > DBNull, not just any old null (quite what the difference is between them, I
    > don't know.)


    A null in C# means the absence of a reference to an object. DBNull
    means the absence of a known database value.

    > In thoery, it will make my code slightly slower I suppose (being checked for
    > every parameter being passed) but I think it is a much better way of coding
    > around the problem.


    You have to do it to avoid the unnecessary exceptions.
    Alexey Smirnov, May 7, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DC Gringo

    problem with crashing

    DC Gringo, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    418
    Greg Burns
    Sep 23, 2004
  2. Christopher Dean

    MSSQL JDBC driver crashing Java app?

    Christopher Dean, Dec 4, 2003, in forum: Java
    Replies:
    0
    Views:
    523
    Christopher Dean
    Dec 4, 2003
  3. Milan Krejci
    Replies:
    4
    Views:
    312
    Jim Langston
    Aug 28, 2007
  4. Ross
    Replies:
    0
    Views:
    349
  5. gelonida
    Replies:
    1
    Views:
    493
    Terry Reedy
    Feb 4, 2011
Loading...

Share This Page