Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Problem with dataset crashing my app

Reply
Thread Tools

Problem with dataset crashing my app

 
 
David
Guest
Posts: n/a
 
      05-07-2007
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


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-07-2007
On May 7, 12:39 pm, "David"
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
David
Guest
Posts: n/a
 
      05-07-2007
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
>



 
Reply With Quote
 
Cowboy \(Gregory A. Beamer\)
Guest
Posts: n/a
 
      05-07-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-07-2007
On May 7, 2:18 pm, "David"
<(E-Mail Removed)> 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...

 
Reply With Quote
 
David
Guest
Posts: n/a
 
      05-07-2007
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)" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>>

>



 
Reply With Quote
 
David
Guest
Posts: n/a
 
      05-07-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> On May 7, 2:18 pm, "David"
> <(E-Mail Removed)> 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...
>



 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-07-2007
On May 7, 5:21 pm, "David"
<(E-Mail Removed)> 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.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
My Python / wxPython app crashing - suggestions? Ross Python 0 11-27-2009 09:27 PM
why is returning an int from a pointer crashing my app Milan Krejci C++ 4 08-28-2007 04:13 AM
DataSet and dataSet JimO ASP .Net 2 03-08-2006 02:39 PM
MSSQL JDBC driver crashing Java app? Christopher Dean Java 0 12-04-2003 05:53 PM
Ccopying a datatable content from an untyped dataset into a table which is inside a typed dataset Nedu N ASP .Net 1 10-31-2003 02:39 AM



Advertisments