Solved

Problem converting VB to C# in web page

Posted on 2009-04-02
4
742 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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