Help with Repeater control and advanced data manipulation

Discussion in 'ASP .Net' started by Mike Cain, Oct 21, 2006.

  1. Mike Cain

    Mike Cain Guest

    Hi,

    The Repeater control seems like exactly what I want to output rows of data
    from my database. However I need to do some manipulation to the data prior
    to it being output and I'm not understanding how to go about this properly.

    For instance, the db column "myVal" is defined as a double in the db. In my
    table I want to display the value of that number by 1000. I used the code
    below and it does the trick. However, for some rows myVal may be null
    (DBNull) so in that case I get an exception thrown. I can't exclude rows
    where myVal is null because its just one field I need to display for the
    row. If it is null, I'd like to output "N/A" instead of the value/1000.
    How can this be done?
    <itemtemplate>

    <tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    "myVal"))/1000 %></td></tr>

    <tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    "myVal2"))/1000 %></td></tr>

    </itemtemplate>

    Along these same lines - is there a way to create some program logic and
    display the results of that logic in the Repeater template? For instance
    even if there is a simple solution to the above, I have other cases where I
    need some fancy if/then/else logic to really determine the value I want to
    display in the table for a given data element.

    Am I better off reading the rows out of the table into a temp array and
    putting the values into the array as I want them to display, and then
    binding the array to the Repeater instead of trying to work with the dataset
    directly in the Repeater? If so, are there any good examples you can point
    me to for this?

    Thanks in advane!!
     
    Mike Cain, Oct 21, 2006
    #1
    1. Advertising

  2. Mike Cain

    Riki Guest

    Mike Cain wrote:
    > Hi,
    >
    > The Repeater control seems like exactly what I want to output rows of
    > data from my database. However I need to do some manipulation to the
    > data prior to it being output and I'm not understanding how to go
    > about this properly.
    > For instance, the db column "myVal" is defined as a double in the db.
    > In my table I want to display the value of that number by 1000. I
    > used the code below and it does the trick. However, for some rows
    > myVal may be null (DBNull) so in that case I get an exception thrown.
    > I can't exclude rows where myVal is null because its just one field I
    > need to display for the row. If it is null, I'd like to output "N/A"
    > instead of the value/1000. How can this be done?
    > <itemtemplate>
    >
    > <tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    > "myVal"))/1000 %></td></tr>
    >
    > <tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    > "myVal2"))/1000 %></td></tr>
    >
    > </itemtemplate>


    Write a helper function and call that function from your template:
    (I'm using VB.NET)
    Function Helper(obj As Object) As String
    If(IsDbNull(obj)) Then
    Return "N/A"
    Else If ...
    ...
    End If
    End Function

    <tr><td><%# Helper(DataBinder.Eval(Container.DataItem,
    "myVal")) %></td></tr>

    --

    Riki
     
    Riki, Oct 21, 2006
    #2
    1. Advertising

  3. ItemDataBound event is the answer to all your questions. Research it first
    and then come back and ask more if you need.

    Good luck,

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]


    "Mike Cain" <> wrote in message
    news:...
    > Hi,
    >
    > The Repeater control seems like exactly what I want to output rows of data
    > from my database. However I need to do some manipulation to the data
    > prior to it being output and I'm not understanding how to go about this
    > properly.
    >
    > For instance, the db column "myVal" is defined as a double in the db. In
    > my table I want to display the value of that number by 1000. I used the
    > code below and it does the trick. However, for some rows myVal may be
    > null (DBNull) so in that case I get an exception thrown. I can't exclude
    > rows where myVal is null because its just one field I need to display for
    > the row. If it is null, I'd like to output "N/A" instead of the
    > value/1000. How can this be done?
    > <itemtemplate>
    >
    > <tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    > "myVal"))/1000 %></td></tr>
    >
    > <tr><td><%# Convert.ToDouble(DataBinder.Eval(Container.DataItem,
    > "myVal2"))/1000 %></td></tr>
    >
    > </itemtemplate>
    >
    > Along these same lines - is there a way to create some program logic and
    > display the results of that logic in the Repeater template? For instance
    > even if there is a simple solution to the above, I have other cases where
    > I need some fancy if/then/else logic to really determine the value I want
    > to display in the table for a given data element.
    >
    > Am I better off reading the rows out of the table into a temp array and
    > putting the values into the array as I want them to display, and then
    > binding the array to the Repeater instead of trying to work with the
    > dataset directly in the Repeater? If so, are there any good examples you
    > can point me to for this?
    >
    > Thanks in advane!!
    >
    >
     
    Eliyahu Goldin, Oct 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    865
  2. Replies:
    0
    Views:
    905
  3. Neo Geshel
    Replies:
    2
    Views:
    765
    Neo Geshel
    Nov 17, 2005
  4. MC felon

    advanced string manipulation

    MC felon, Oct 8, 2006, in forum: C++
    Replies:
    4
    Views:
    266
    peter koch
    Oct 8, 2006
  5. Michele Simionato
    Replies:
    1
    Views:
    632
    Lacrima
    Mar 27, 2010
Loading...

Share This Page