Solved

Problem converting VB to C# in web page

Posted on 2009-04-02
4
739 Views
Last Modified: 2013-12-17
Ive got a problem with a snippet of VB Im trying to convert to C#. Its complicated because the problem is buried within a .aspx page. This page has a datagrid which display records from a SQL Server database.  When I first set up this page, I had a problem that numeric values were displaying with unnecessary trailing zeros. The customer asked me to remove them. Sometimes the value will be something like 0.000012. Other times it would be 0.340000 Its in the latter example that I need to remove the trailing zeros. So its not a simple case of formatting to a fixed number of places. I wrote a function to remove trailing zeros from a string value. Heres the original code from my web page which called my function worked perfectly when I was using VB&&

<asp:TemplateColumn HeaderText="Price Each" >
   <ItemTemplate >
      <%# RemoveTrailingZeros(Container.DataItem("MyPriceEach")) %>
  </ItemTemplate>
</asp:TemplateColumn>

Now thatI am using C# as the page language, the above gives me a compilation error:

Non-invocable member 'System.Web.UI.WebControls.DataGridItem.DataItem' cannot be used like a method.

I have seen this before. It means that I have to change the round brackets for square ones. So I changed them thus:

<%# RemoveTrailingZeros(Container.DataItem["MyPriceEach"]) %>

Now I get a different error:

Cannot apply indexing with [] to an expression of type 'object'

How do I fix this?

Ian
0
Comment
Question by:ipendlebury
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Andy Green
ID: 24048170
Hi
Can you not format the data in your stored proc (query) to return the data in the right shape and do away with the function.
Andy
0
 

Author Comment

by:ipendlebury
ID: 24048321
Hi Andy, I wish I knew how to, but i cant find a way to remove trailing zeros in SQL.

This works very well in VB. So surely it must be a simple tweak to change the syntax for C#?

Ian
0
 
LVL 3

Accepted Solution

by:
Andy Green earned 500 total points
ID: 24048373
I only know VB so cant help with the C# side of things. If I ever have to convert between the 2 I use http://www.codechanger.com/
You might find that because the data returned is always the same length, it is stored as char() type. Try to cast (or convert) to a Varchar(), it might work.
Andy
0
 

Author Comment

by:ipendlebury
ID: 24049070
I've fixed it now. The syntax should be:

<%# RemoveTrailingZeros(DataBinder.Eval(Container.DataItem, "MyPriceEach").ToString) %>

Thanks for you input.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TimeZone, day light savings, Sql server, asp.net 6 38
How to read text with RegEx... 2 33
Import MySQL data into MS Access using VB.Net interface 5 47
C# XML Get Values 4 33
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question