Option Strict On disallows late binding

PEAFLENTLEAGS

New Member
When I have Strict="True", I get the error, "Option Strict On disallows late binding." when I execute the following code:<BR><BR><BR><asp:Repeater<BR> ID="DataGrid"<BR> EnableViewState="False"<BR> Runat="Server"><BR><BR><ItemTemplate><BR><tr><BR> <td><%# Container.DataItem("FirstName") & " " & Container.DataItem("LastName") %></td> <!-- ERROR HERE --><BR> <td><%# Container.DataItem("City") %></td><BR> <td><%# Container.DataItem("State") %></td><BR> <td><%# iif("[" & Container.DataItem("EMailAddress") & "]" = "[]","&nbsp;", _<BR> Container.DataItem("EMailAddress")) %></td><BR> <td><a href="javascript:DelAlumni("<%# Container.DataItem("ID") %>")">Delete</a></td><BR> <td><a href="EditAlumni.aspx?T=E&ID=<%# Container.DataItem("ID") %>">Edit</a></td><BR></tr><BR></ItemTemplate><BR><BR></asp:Repeater><BR><BR><BR>With strict turned off, I have no problems. I'm new to ASP.NET, and I have no idea what this means, nor how to fix it. I set the Data Source and do a data bind in a function named PerformSearch which is triggered when a user presses a Search button.<BR><BR>Any ideas?<BR><BR>Thanks,<BR>Jesseso turn option strict off.Unfortunately, that's the only thing that makes it work, but I don't want to turn it off, because Strict="True" catches certain coding mistakes that I may make. What I'd like to know is how to resolve this issue while keeping Strict set to on.<BR><BR>Jessedon't use late binding.<BR><BR>look, you can't have your cake AND eat it. pick one, throw it away.Sorry, I'm new to ASP.NET, and there's a lot I don't understand yet. How do I use early binding vs. late binding?<BR><BR>Jessein this context, I'm not 100% sure what's going on. that's not all the code, is it?<BR><BR>I suspect that fact that you're databinding with the <%# %> convention is the problem. so turn option strict off.But there *is* a way around it however, without having to turn off Option Strict. <BR><BR><BR><asp:Repeater <BR> ID="DataGrid" <BR> EnableViewState="False" <BR> Runat="Server"> <BR><BR><ItemTemplate> <BR><tr> <BR> <td><%# DirectCast(Container.DataItem, System.Data.DataRowView)("FirstName") & " " & DirectCast(Container.DataItem, System.Data.DataRowView)("LastName") %></td> <BR> <td><%# DirectCast(Container.DataItem, System.Data.DataRowView)("City") %></td> <BR> <td><%# DirectCast(Container.DataItem, System.Data.DataRowView)("State") %></td> <BR> <td><%# iif("[" & DirectCast(Container.DataItem, System.Data.DataRowView)("EMailAddress") & "]" = "[]"," ", _ <BR> DirectCast(Container.DataItem, System.Data.DataRowView)("EMailAddress")) %></td> <BR> <td><a href="javascript:DelAlumni("<%# DirectCast(Container.DataItem, System.Data.DataRowView)("ID") %>")">Delete</a></td> <BR> <td><a href="EditAlumni.aspx?T=E&ID=<%# DirectCast(Container.DataItem, System.Data.DataRowView)("ID") %>">Edit</a></td> <BR></tr> <BR></ItemTemplate> <BR><BR></asp:Repeater> <BR>Use Databinder.Eval to get it via reflection at runtime, and aviod the option strict errors, but using the explict cast gives you (of course) far better performance.Thanks for chiming in on this. I was hoping someone would have a solution that would allow me to keep Strict on. After converting the above (and throwing in a few CStr()'s to take care of some other problems), the page will now compile w/o a problem. However, when I submit a search, I get an error, "System.InvalidCastException: Specified cast is not valid" on that first line contiaining the Container lines. Any clues what I should do about that?<BR><BR>Thanks,<BR>JesseWhat's the datasource? Not a dataset, perhaps? If not, you may have to cast to another type. <BR><BR>Try changing the DirectCast to CType, and then wrap in in a Convert.ToString() like so:<BR><BR><BR><%# Convert.ToString(CType(Container.DataItem, System.Data.DataRowView)("FirstName")) & " " & Convert.ToString(CType(Container.DataItem, DataRowView)("LastName")) %><BR><BR><BR>If that doesn't work...<BR><BR><%# DataBinder.Eval(Container.DataItem, "FirstName") & " " & Databind.Eval(Container.DataItem, "LastName") %><BR><BR>will be slower but should be able to correctly type the data container at runtime.The "er" on the second "DataBinder.Eval"The CStr function doesn't like DBNull values. So, this should work, and be the most efficient method:<BR><BR> -- IIS/PWS<BR> -- JScript<BR> -- COM/COM+<BR> -- XML<BR> -- C#<BR> -- Site-Related<BR> -- Social <BR><BR>--------------------------------------------------------------------------------<BR> <BR>Recent Articles <BR>All Articles <BR>ASP.NET Articles <BR>ASPFAQs.com <BR>Coding Tips <BR>Related Web Technologies <BR>User Tips <BR>Search <BR> <BR> <BR> <BR> <BR>Learn About the Apache Beehive Project. <BR> <BR><BR><BR> <BR> <BR><BR> <BR><BR> <BR> Sections: <BR>Book Reviews<BR>Sample Chapters <BR>Commonly Asked Message Board Questions <BR>Headlines from ASPWire.com <BR>JavaScript Tutorials <BR>Official Docs <BR>Security <BR>Stump the SQL Guru! <BR>Web Hosts <BR>XML Info <BR>Information: <BR>Advertise <BR>Feedback <BR>Author an Article <BR> <BR> <BR><BR> <BR> Web directory <BR>Flower Delivery <BR>Price Comparison <BR>Calling Cards <BR>Submit Your Site <BR>Auto Insurance <BR>Flooring <BR>Flowers <BR>eCommerce Web Hosting <BR>Televisions <BR> <BR> <BR> <BR><BR> <BR> Developer<BR>Downloads<BR>International<BR>Internet Lists<BR>Internet News<BR>Internet Resources<BR>IT<BR>Linux/Open Source<BR>Small Business <BR>Windows Technology<BR>Wireless Internet<BR>xSP Resources <BR><BR>Search internet.com<BR>Advertise<BR>Corporate Info<BR>Newsletters<BR>Tech Jobs<BR>E-mail Offers<BR> <BR> <BR> <BR> <BR> To post in forums you must have an account - Create one now!<BR>Customize the Forum Display &#124 Change Your Password &#124 Forgot your Username/Password? &#124 Post Count! &#124 Logout <BR> Intersting...<BR>Xanderno - 7/2/2004 11:03:19 AM<BR><BR>--------------------------------------------------------------------------------<BR> What's the datasource? Not a dataset, perhaps? If not, you may have to cast to another type. <BR><BR>Try changing the DirectCast to CType, and then wrap in in a Convert.ToString() like so: <BR><BR><BR><%# Convert.ToString(DirectCast(Container.DataItem, System.Data.DataRowView)("FirstName")) & " " & Convert.ToString(DirectCase(Container.DataItem, DataRowView)("LastName")) %> <BR><BR><BR>In otherwords...If you change the CStr()'s that you needed to Convert.ToString()'s, it should work for you. <BR>OK, Now I'm confused. Quite a bit of your message seemed to be a copy of the menu from this web page. So, I'm not sure that your message had the intended content (Unless I just missed it). Anyway, according to your previous message, I made the following changes:<BR><BR><BR><td><%# Convert.ToString(CType(Container.DataItem, System.Data.DataRowView)("FirstName")) & _<BR> " " & Convert.ToString(CType(Container.DataItem, System.Data.DataRowView)("LastName")) %></td><BR><BR><BR>But, I got the same "System.InvalidCastException: Specified cast is not valid." error message. Maybe your last message contained the solution, but I couldn't find it in all of that?<BR><BR>Thanks,<BR>JesseAnyway, you may have to use reflection to get at it. <BR><BR>Try this, and see if it helps the issue. <BR><BR><%# DataBinder.Eval(Container.DataItem, "FirstName") & " " & Databinder.Eval(Container.DataItem, "LastName") %> <BR><BR>What is the data that you're getting back from the search..By the way? <BR>THAT DID THE TRICK! Thanks so much for your help. My final code ended up being as follows:<BR><BR><BR><td><%# CStr(DataBinder.Eval(Container.DataItem, "FirstName")) & " " & CStr(Databinder.Eval(Container.DataItem, "LastName")) %></td><BR><td><%# DataBinder.Eval(Container.DataItem,"City") %></td><BR><td><%# DataBinder.Eval(Container.DataItem,"State") %></td><BR><td><%# CStr(iif(IsDBNull(DataBinder.Eval(Container.DataIt em,"EMailAddress")),"&nbsp;", _<BR> DataBinder.Eval(Container.DataItem,"EMailAddress"))) %></td><BR><td><a href="javascript:DelAlumni("<%# DataBinder.Eval(Container.DataItem,"ID") %>")">Delete</a></td><BR><td><a href="EditAlumni.aspx?T=E&ID=<%# DataBinder.Eval(Container.DataItem,"ID") %>">Edit</a></td><BR><BR><BR>Thanks again!<BR>JesseI think that the reason that the explict cast didn't work, is that populating it from a DataTable the first go round, and a DataReader after the search, no? <BR><BR>The explicit cast that I gave you (System.Data.DataRowView) was for a DataTable/DataSet...For a DataReader, the items would have needed to be cast to System.Data.IDBRecord instead. But, of course, DataBinder.Eval is smart enough to figure all that out on the fly. <BR><BR>Cheers,<BR><BR>Xander
 
Top