I am having difficulty with detecting null string values,
as I build web pages from database search results. The
display of the normal datagrid including nulls is fine -
but I have set up an export function, which passes the
datagrid's table to an Excel sheet. In this excel sheet,
nulls cause columns to be ignored - so rows with null
values are out of whack. for example, if a datagrid shows:
A,B,C,D
A, ,C,D
A,B, ,D
the Excel sheet will show
A,B,C,D
A,C,D
A,B,D
But if i try to include "if string.empty then" lines, it
compiles fine but complains at runtime that I am trying to
detect a property from a non existant object. How do I
detect if the object exists? There is no longer a "null"
value in VB.NET, so it's very hard to find a way around
this. One option is to set the default value for columns
in the DB to some default, but I'd rather not do that.
Any help on this is most welcome!
Paul.
PaulN - 23 Feb 2004 16:34 GMT
I have found a solution, which solves my problem, but
doesn't answer my question. I have used the SQL "CASE"
statement (it's an Oracle DB) to cope with nulls - so they
never get as far as the datagrid. But how are nulls
detected, in case I hit this problem in a non DB querying
applucation?
Paul.
>-----Original Message-----
>I am having difficulty with detecting null string values,
[quoted text clipped - 25 lines]
> Paul.
>.
Yan-Hong Huang[MSFT] - 24 Feb 2004 06:23 GMT
Hello Paul,
Thanks for posting in the group.
Based on my understanding, now the issue is: You exported a datagrid to an
Excel sheet in asp.net programming. However, if the cell in data source is
null, the value is missing in Excel worksheet. Right?
Could you please paste the code here on how you export datagrid to excel in
asp.net? Also, please check the HTML source of that web page to see the
exact content that datagrid render a null value. (It may be " ", so
please double check it)
By the way, in the description, you wrote "But if i try to include "if
string.empty then" lines", could you please let us know where you add this
line?
I look forward to your response.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ?C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Paul N - 24 Feb 2004 21:50 GMT
Hi Yan-Hong,
OK, your answer to this question doesn't actually answer
my overall question - but suggests what that answer might
be (and so raises further questions). Vis a vis writing
the table to Excel - I literally write the table
(Response.Write("<TABLE BORDER=1><TR><TD>......) - you get
the idea. The reason i am doing that is that I have been
unnable to output a datagrid as excel and see any more
than 50 rows -and some of my tables are several thousand
rows long. The Response.Write method has no difficulty in
coping with that many. Looking at what it outputs as HTML
(rather than excel document type), the null values simply
are not there at all - I assume the datatable ignores
them. I am stepping through each row of the datatable in a
numeric order fashion. The reason for this is that the
table is dynamic - some users will have it configured not
to have certain columns. It would be possible to cope with
that in a column names manner, but I can't be bothered
going back and re-coding everything again - as I say my
altered query copes with the problem.
The "if null value" problem has occurred in MANY
different places - and not just in the case of the column
values, but for elsewhere that I have needed to be able to
detect if an expected value is a null or not. A
straight "if value = null" option or "if value.IsNull" or
something would help a LOT.
Thanks,
Paul.
Yan-Hong Huang[MSFT] - 25 Feb 2004 08:00 GMT
Hi Paul,
Anyway, I am glad to see that you workaround it by changing the DB query. I
am glad to share my ideas with you. Writing it to a table manually should
be OK. The following method may be also useful:
If Request.QueryString("bExcel") = "1" Then
' Set the content type to Excel.
Response.ContentType = "application/vnd.ms-excel"
' Remove the charset from the Content-Type header.
Response.Charset = ""
' Turn off the view state.
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' Get the HTML for the control.
DataGrid1.RenderControl(hw)
' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.
Response.End()
End If
It will render the HTML output of datagrid control to client side. I think
they are also HTML table style.
For details on it, please refer to MSDN article:
"HOW TO: Export Data in a DataGrid on an ASP . NET WebForm to Microsoft
Excel"
http://support.microsoft.com/?id=317719
For the if null value question, the Visual Basic runtime and the .NET
Framework evaluate Nothing differently when it comes to strings. Consider
the following example:
Dim MyString As String = "This is my string"
Dim stringLength As Integer
' Explicitly set the string to Nothing.
MyString = Nothing
' stringLength = 0
stringLength = Len(MyString)
' This line, however, causes an exception to be thrown.
stringLength = MyString.Length
The Visual Basic .NET runtime evaluates Nothing as an empty string; that
is, "". The .NET Framework, however, does not, and will throw an exception
whenever an attempt is made to perform a string operation on Nothing
Hope that helps.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ?C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
DotNetJunkies User - 19 Nov 2004 20:20 GMT
I know its an old post . But I stumbled upon it as I was looking for some solution to my problem. I found that in oracle DB if uou try to update a non -null coulmn with a string which has a value set to string.empty, oracle returns an error "cannot insert Null in ...column. so I have to explicitly set the value=" " and then do the insert , and it works fine !
So i am guessing same problem may happen in comparsion as well wherin string.empty may not be working as expected
---
Yan-Hong Huang[MSFT] - 22 Nov 2004 01:28 GMT
Hi,
ADO.net should be the right group for this question. :) Thanks.
Best regards,
Yanhong Huang
Microsoft Community Support
Get Secure! ?C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn
This posting is provided "AS IS" with no warranties, and confers no rights.