Solved

Problem converting VB to C# in web page

Posted on 2009-04-02
4
735 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I've fixed it now. The syntax should be:

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

Thanks for you input.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now