Problem with dataset crashing my app

D

David

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
 
G

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;

}

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

I'm not sure if DBNull.Value will help here... try it
 
D

David

Not quite following?

Do you mean something like...

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

???

Regards,
Dave.
 
C

Cowboy \(Gregory A. Beamer\)

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.
 
G

Guest

Not quite following?

Do you mean something like...

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

}

???

Regards,
Dave.





- 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...
 
D

David

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
 
D

David

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
 
G

Guest

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top