Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem converting VB to C# in web page

Posted on 2009-04-02
4
Medium Priority
?
745 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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:…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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