Solved

Problem converting VB to C# in web page

Posted on 2009-04-02
4
743 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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