On line 65 of Events_Calendar.aspx-
<div style="padding: 3px;">
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%#Eval("title") %>' NavigateUrl='<%# "Events_view.aspx?Eventid=" &CStr( Eval("ID"))%>'
ToolTip='<%# truncate(CStr(Eval("description"))) %>' /></div>
The debugger tells me "InvalidCastException unhandled by user code"
I found out that the data entry form for events allows the user to leave the description field empty. So this translates to NULL in the SQL Server 2005 database. the NULL value in the description field causes the exception, I can make it go away by replacing the NULL value with a blank value, directly editing the table using SQL Server Management Studio Express.
I am new to this. Any number of solutions would work, but the complexity of these data entry forms has me beating my head on the desk. I am familiar with building forms for desktop apps in VB and VFP)
Any real simple answer to one of these questions would be helpful.
Can I force a default value to be an empty string (or anything) instead of NULL on the data entry form when a user does not enter anything in a field? I could use either a field or record validation rule, or simply a default value property - how is this done?
Or, how would one force a user to fill out a field on the Events_Edit.aspx form?
Thanks
![]() |
0 |
![]() |
Check out this tip from ASPNet101.com:
http://aspnet101.com/aspnet101/tips.aspx?id=27
David Wier
MCP/ASPInsider
ASPNet101.com - where to look first!
Please Vote for ASPNet101 - 'Best Community Resource'!
Control Grouper - easily control properties for multiple controls with one control!
Calendar Express - The Best HTML Calendar Generator on the web!
(Please 'Mark as Answer' when it applies)
![]() |
0 |
![]() |
Lacking the patience to change more stuff, my solution is to avoid trying to perform the operation that may fail. So I just changed the failing CStr() function with myCStr() in the pages where it is failing.
<script runat="server">
function myCStr(Byval test as object) as String
if isdbnull(test) then
return("")
else
return CStr(test)
end if
end function
</script>
![]() |
0 |
![]() |
To force a description use Required Field validation
You can add requiredfieldvalidator using design view. or use this code.In Events_Edit.aspx
Replace in InsertItemTemplate
<td class="formlabel">
<asp:Label ID="Label1" runat="server" Text="Description:" />
</td>
<td align="left">
With
<td class="formlabel">
<asp:Label ID="Label1" runat="server" Text="Description: Required" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="descriptionTextBox"
ErrorMessage="Description Required!">Description Required!</asp:RequiredFieldValidator></td>
<td align="left">
and Replace in EditItemTemplte
<td class="formlabel">
<asp:Label ID="Label1" runat="server" Text="Description:" />
</td>
<td align="left">
With
<td class="formlabel" style="height: 180px">
<asp:Label ID="Label1" runat="server" Text="Description: Required" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="descriptionTextBox"
ErrorMessage="Description Required!">Description Required!</asp:RequiredFieldValidator></td>
<td align="left">
Support@aspsksolutions.com
![]() |
0 |
![]() |
Great fix, just what I was after
Regards
<<<Bryan Avery>>>
Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
![]() |
0 |
![]() |
The fix I decided to go with:
Update the insert and update SQL statements to use isnull(@description,'') instead of just @description.
A blank string will always be stored in the database instead of a null then.
![]() |
0 |
![]() |
GavinPollock:
The fix I decided to go with:
Update the insert and update SQL statements to use isnull(@description,'') instead of just @description.
A blank string will always be stored in the database instead of a null then.
There is really no reason to store a 'blank' (empty string) to get around this issue. After working with Microsoft Access for 14 years, Nulls are a fact of life ... and really not a problem. You just have to deal with them appropriately. If you store an empty string, you may have other issues down the road ... if for example ... for whatever reason ... you need to test for Null in the Description field - the default value for a new record - but - forget that you have put an empty string in that field.
The best approach here so far is the Function given by lutz2:<script runat="server">
function myCStr(Byval test as object) as String
if isdbnull(test) then
return("")
else
return CStr(test)
end if
end function
</script>Better yet would be to use an IIF in the line of code ToolTip='<%# truncate(CStr(Eval("description"))) %>' .... replacing a Null with "" (empty string) on the fly. Unfortunately, I can't quite get the syntax right for this. Lutz2's function essentially does this.
And really ... why ... is it necessary to take the CStr of the Eval of the Description field?? The Description field is already a String (text) data type! This error would not even occur if you didn't use CStr and Eval! And actually, not sure Truncate is necessary either!And FOR SURE ... there is no reason to 'require' the Description field and do a validation !!
BTW ... this same issue/error occurs in the News section ... where I also used lutz2's fixEOP.
![]() |
0 |
![]() |
Can anyone give me an example of this fix? I am getting the same error using the events page and don't know exactly how to change this code:
Conversion from type 'DBNull' to type 'String' is not valid.
![]() |
0 |
![]() |
1) in the Events_List.aspx, add the myCStr function to the code at the top of the page, inside the script tags
<script runat="server">
(other code)
...
function myCStr(...
...
</script>
In any of the lines that cause errors use the myCStr function. I dont recall what was there previously,
probably the regular CSstr(...), but this example is shortly after the first <!-- begin news item -->
<asp:Label ID="descriptionLabel" runat="server" Text='<%# truncate(myCStr(Eval("description"))) %>' />
![]() |
0 |
![]() |
I feel like such a dummy... It worked with no problems. Thanks.
AZ
![]() |
0 |
![]() |
Don't feel that way, I'm glad to help. I'm new to this. I've spent hours or even days of fighting with difficult issues, to find that the needed changes are "simple". Luckily I know pre-.NET VB, C and databases.
Until just now, I did not know how to make the function global for the whole website, so that the function would not need to be declared in the script portion of every page where it is needed.
The answer appears to be in ~/App_Code/Shared_routines.vb
Place the function in Shared_ routines.vb, and declare it public like this:
Public function ....
....
Return ...
End Function
I have not tried this yet, but it should work
![]() |
0 |
![]() |
Thanks each of the contributors to this! Took me 12 tries not really knowing what I'm doing but it's working...for now(lol)
Dravend
![]() |
0 |
![]() |
Hi,
Can you offer me any help in fixing this issue in c# ?
J. Jones
![]() |
0 |
![]() |
<rant>
Hate to bump this old message, but I really disagree with the notion that "nulls are a fact of life". They are if you let them, and there really is a world of difference between Access databases and enterprise scale relational models.
In a pure relational model (Boyce Codd form, 3rd +, etc), a traditional null value means that an attribute of an entity is not defined. Because you (should) have built your model correctly, an attribute that may or may not exist for a given entity simply means that there is no relationship.
If you have employees, some of which are sales people, you don't place a "salesGroup" column in the employee table, and then just have nulls for the employees that aren't in that group. Well I suppose you can, but it's pretty Mickey Mouse to be honest.
Where you will run into nulls, even with a relational model, is generally when you get into left outer joins (or any outer join for that matter), because you're intentionally saying "get me everything from the employee table, and match the sales table. If there's no match, I still want the employees, and just give me nulls for the difference". But now you are controlling the nulls, and they exist at query time, and aren't stored anywhere.
Sorry to get on a relational-holier-than-thou trip, but generally the only reason I have ever seen (in the 10 years I've been doing data modeling and development) for nulls in a relational database is laziness, a poorly defined schema, and more often than not, developers who simply don't understand what they're doing.
</rant>
![]() |
0 |
![]() |
I wouldn't say that the folks who created this don't know what they are doing, but why not just update the stored procedure, PagedUpcommingEventList, to not return nulls for the description field?
For example:
SELECT Events.id, Events.starttime, Events.title, ISNULL(Events.description, '') AS description, Events.photo,
Locations.title AS locationname
FROM Events LEFT OUTER JOIN Locations ON Events.location = Locations.id
WHERE (Events.starttime > @keydate OR
(Events.starttime = @keydate) AND (events.id > @keyid))
ORDER BY Events.starttime asc, Events.id asc
![]() |
0 |
![]() |
I converted the above function to c# and when I place it in Event_list.aspx I get complie error for isDBnull not defined correctly. Error:The name 'IsDBNull' does not exist in the current context C:\club_rtm_v1_cs\Events_List.aspx . How can I fix it. any suggestion will be appricated.
public string myCStr(object test)
{
if ( isdbnull(test))
{
return ("");
}
else
{
return System.Convert.ToString(test);
}
}
thanks
![]() |
0 |
![]() |
Change isdbnull(test) toif (test
is DBNull)
![]() |
0 |
![]() |
thanks allankn it worked .
if(test is DBNull)
sk
![]() |
0 |
![]() |
when i attempt to add event #4 i get this message:
Exception Details: System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.
Source Error:
|
Obi Wan, my force is weak and yoda is drunk, can you help
![]() |
0 |
![]() |
Before you create a method against this item, you must first check to make sure it is NOT Null - - you're expecting a string here - instead the results are Null - If you make sure to ONLY do this method against a string (if it's not null), it should work
David Wier
MCP/ASPInsider
ASPNet101.com - where to look first!
Please Vote for ASPNet101 - 'Best Community Resource'!
Control Grouper - easily control properties for multiple controls with one control!
Calendar Express - The Best HTML Calendar Generator on the web!
(Please 'Mark as Answer' when it applies)
![]() |
0 |
![]() |
Same problem with "Conversion from type 'DBNull' to type 'String' is not valid. " Please help!
|
![]() |
0 |
![]() |