locked
Timer RRS feed

  • Question

  • User669521406 posted

    I have a timer and a repeater, the timer help to display the duration of the item in the repeater, and but the main problem is that the item stay on the page even when the duration is finished, how can i remove the item without any postback please

    Saturday, January 11, 2014 7:34 PM

All replies

  • User-1509636757 posted

    Some points to consider if I understood your requirement:

    • Code need to bind the repeater every time WHEN any item duration is expired
    • Code dont need to bind the repeater everytime on Timer Tick event. You only need to update the duration in repeater items.
    • You should use UpdatePanel and place repeater and Timer inside it to have everything without postback.

    Check below example, run it and you will have the idea. I ran it on myside; working perfectly. It updates the duration of each repeater item as well as remove out item from repeater of which duration is expired.

    ASPX Code:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager runat="server" />
                <asp:UpdatePanel runat="server">
                    <ContentTemplate>
                        <asp:Repeater runat="server" ID="repItems">
                            <HeaderTemplate>
                                <table border="1">
                                    <tr>
                                        <td>Item ID</td>
                                        <td>Item Name</td>
                                        <td>End Date</td>
                                        <td>Duration</td>
                                    </tr>
                            </HeaderTemplate>
                            <ItemTemplate>
                                <tr>
                                    <td>
                                        <%# Eval("ItemID") %>
                                        <asp:HiddenField runat="server" ID="hdItemID" Value='<%# Eval("ItemID") %>' />
                                    </td>
                                    <td>
                                        <%# Eval("ItemName") %>
                                    </td>
                                    <td>
                                        <asp:Label ID="lblEndDate" Text='<%# Eval("EndDate") %>' runat="server" />
                                    </td>
                                    <td>
                                        <asp:Label ID="lblDuration" Text='<%# Eval("Duration") %>' runat="server" />
                                    </td>
                                </tr>
                            </ItemTemplate>
                            <FooterTemplate>
                                </table>
                            </FooterTemplate>
                        </asp:Repeater>
                        <asp:Timer runat="server" ID="Timer1" OnTick="Timer1_Tick" Enabled="true" Interval="1000"></asp:Timer>
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>
    </html>

    Code Behind:

    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                BindItems();
        }
    
        //-- you only need to rebind the repeater again from database
        //-- i have to take session to retain the datatable as i am not using any database here
        private void BindItems()
        {
            DataTable dt = new DataTable();
            if (Session["dt"] == null)
            {
                dt = CreateData();
                Session["dt"] = dt;
            }
            else
                dt = (DataTable)Session["dt"];
    
            DataView dw = dt.DefaultView;
            //-- filter the DataTable to only show items which are not expired
            dw.RowFilter = "EndDate >='" + DateTime.Now.ToString() + "'";
            //--bind the repeater
            repItems.DataSource = dt;
            repItems.DataBind();
        }
    
        //-- this is JUST example code to bind the data. You may be binding this data from database table
        private DataTable CreateData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ItemID"));
            dt.Columns.Add(new DataColumn("ItemName"));
            dt.Columns.Add(new DataColumn("EndDate"));
            dt.Columns.Add(new DataColumn("Duration"));
    
            for (int i = 0; i < 10; i++)
            {
                DateTime endDate = DateTime.Now.AddMinutes(i + 1);
                DataRow dr = dt.NewRow();
                dr["ItemID"] = i + 1;
                dr["ItemName"] = "Item " + (i + 1).ToString();
                dr["EndDate"] = endDate;
                dr["Duration"] = endDate - DateTime.Now;
                dt.Rows.Add(dr);
            }
            return dt;
        }
    
        //--timer to update the duration on each second
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            //--this method will fetch the end date from repeater item label and update the duration
            UpdateDuration();
        }
    
        private void UpdateDuration()
        {
            foreach (RepeaterItem item in repItems.Items)
            {
                //--calculate the duration
                TimeSpan duration = Convert.ToDateTime((item.FindControl("lblEndDate") as Label).Text) - DateTime.Now;
                
                //--Here is the important part: if any duration is expired then bind the grid again
                if (duration.Ticks < 0)
                {
                    //-- you can also write code to update database item status to = expired
                    //-- get the item id
                    Int64 itemId = Convert.ToInt64((item.FindControl("hdItemID") as HiddenField).Value);
                    //-- write code here to update item with this itemId to set the status to expire.
    
                    //-- bind the repeater again
                    BindItems();
                    break;
                }
                (item.FindControl("lblDuration") as Label).Text = new DateTime(duration.Ticks).ToString("HH:mm:ss");
            }
        }
    }

    hope it helps./.

    Saturday, January 11, 2014 11:29 PM
  • User669521406 posted

    the main difference here is that i don't have duration in my table, i have startdate and endate,

    when i trying it give only the first duration of the first item

    for every item in the repeater what i want is that each item has their own duration

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Net.Mail
    Imports System.Web.Configuration
    Imports System.Web.Script.Services
    Imports System.Web.Services
    Partial Class tRYBID
        Inherits System.Web.UI.Page
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("VirgoPlaza").ToString())
        Dim EndDate As DateTime
        Dim auction As Integer
    
        Private ReadOnly _start As String
        Public Sub New()
            _start = WebConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
    
                Using con1 As New SqlConnection(_start)
    
    
                    Dim sql1 As String = "SELECT Item.ItemID,Item.Price,Auction.StartingPrice,Item.Name,Item.Image1, Item.Description, Item.Price,Auction.EndDate,Auction.AuctionID,Auction.Status FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID Where Auction.Status='Valid' AND Auction.EndDate>=@endate "
                    Dim myCommand1 As New SqlCommand(sql1, con1)
                    myCommand1.Parameters.AddWithValue("@endate", DateTime.Now)
    
                    myCommand1.CommandType = CommandType.Text
    
                    con1.Open()
    
                    Dim category As SqlDataReader
                    category = myCommand1.ExecuteReader()
    
                    'binding data from category table to ddlcategory
                    repItems.DataSource = category
                    repItems.DataBind()
    
                End Using
    
            End If
    
        End Sub
        Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
            '--this method will fetch the end date from repeater item label and update the duration
            UpdateDuration()
        End Sub
        Private Sub UpdateDuration()
            For Each item As RepeaterItem In repItems.Items
                '--calculate the duration
                Dim duration As TimeSpan = Convert.ToDateTime(TryCast(item.FindControl("lblEndDate"), Label).Text) - DateTime.Now
    
                '--Here is the important part: if any duration is expired then bind the grid again
                If duration.Ticks < 0 Then
                    '-- you can also write code to update database item status to = expired
                    '-- get the item id
                    Dim itemId As Int64 = Convert.ToInt64(TryCast(item.FindControl("hdItemID"), HiddenField).Value)
                    '-- write code here to update item with this itemId to set the status to expire.
    
                    '-- bind the repeater again
                    BindItems()
                    Exit For
                End If
                TryCast(item.FindControl("lblDuration"), Label).Text = New DateTime(duration.Ticks).ToString("HH:mm:ss")
            Next
        End Sub
        Private Sub BindItems()
            Dim dt As New DataTable()
            If Session("dt") Is Nothing Then
                dt = CreateData()
                Session("dt") = dt
            Else
                dt = DirectCast(Session("dt"), DataTable)
            End If
    
            Dim dw As DataView = dt.DefaultView
            '-- filter the DataTable to only show items which are not expired
            dw.RowFilter = "EndDate >='" + DateTime.Now.ToString() + "'"
            '--bind the repeater
            repItems.DataSource = dt
            repItems.DataBind()
        End Sub
    
        Private Function CreateData() As DataTable
            Dim dt As New DataTable()
            dt.Columns.Add(New DataColumn("ItemID"))
            dt.Columns.Add(New DataColumn("ItemName"))
            dt.Columns.Add(New DataColumn("EndDate"))
            dt.Columns.Add(New DataColumn("Duration"))
    
            For i As Integer = 0 To 9
                Dim endDate As DateTime = DateTime.Now.AddMinutes(i + 1)
                Dim dr As DataRow = dt.NewRow()
                dr("ItemID") = i + 1
                dr("ItemName") = "Item " + (i + 1).ToString()
                dr("EndDate") = endDate
                dr("Duration") = endDate - DateTime.Now
                dt.Rows.Add(dr)
            Next
            Return dt
        End Function
    End Class

    Sunday, January 12, 2014 2:20 AM
  • User669521406 posted

    and one more question, will i need to replace the session by my sql statement or i can left it like it is?, what change will it implies please if i let it like it is?

    Sunday, January 12, 2014 3:08 AM
  • User-1509636757 posted

    Hello,

    You don't need these two function: BindItems and CreateData because I created these two methods just to bring some data in my repeater you just need to call the same binding code that you are calling in Page_Load under IsPostBack Check, where you are trying to call BindItems in Timer_Tick event.

    Based on what I understood; here can be your code look like and you can check if it meets you requirement or not:

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Net.Mail
    Imports System.Web.Configuration
    Imports System.Web.Script.Services
    Imports System.Web.Services
    Partial Class tRYBID
        Inherits System.Web.UI.Page
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("VirgoPlaza").ToString())
        Dim EndDate As DateTime
        Dim auction As Integer
    
        Private ReadOnly _start As String
        Public Sub New()
            _start = WebConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
        End Sub
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
                BindItems()
            End If
    
        End Sub
        Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
            '--this method will fetch the end date from repeater item label and update the duration
            UpdateDuration()
        End Sub
        Private Sub UpdateDuration()
            For Each item As RepeaterItem In repItems.Items
                '--calculate the duration
                Dim duration As TimeSpan = Convert.ToDateTime(TryCast(item.FindControl("lblEndDate"), Label).Text) - DateTime.Now
    
                '--Here is the important part: if any duration is expired then bind the grid again
                If duration.Ticks < 0 Then
                    '-- you can also write code to update database item status to = expired
                    '-- get the item id
                    Dim itemId As Int64 = Convert.ToInt64(TryCast(item.FindControl("hdItemID"), HiddenField).Value)
                    '-- write code here to update item with this itemId to set the status to expire.
    
                    '-- bind the repeater again
                    BindItems()
                    Exit For
                End If
                TryCast(item.FindControl("lblDuration"), Label).Text = New DateTime(duration.Ticks).ToString("HH:mm:ss")
            Next
        End Sub
        Private Sub BindItems()
            Using con1 As New SqlConnection(_start)
    
    
                Dim sql1 As String = "SELECT Item.ItemID,Item.Price,Auction.StartingPrice,Item.Name,Item.Image1, Item.Description, Item.Price,Auction.EndDate,Auction.AuctionID,Auction.Status FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID Where Auction.Status='Valid' AND Auction.EndDate>=@endate "
                Dim myCommand1 As New SqlCommand(sql1, con1)
                myCommand1.Parameters.AddWithValue("@endate", DateTime.Now)
    
                myCommand1.CommandType = CommandType.Text
    
                con1.Open()
    
                Dim category As SqlDataReader
                category = myCommand1.ExecuteReader()
    
                'binding data from category table to ddlcategory
                repItems.DataSource = category
                repItems.DataBind()
    
            End Using
        End Sub
    End Class

    hope it helps./.

    Sunday, January 12, 2014 6:10 AM
  • User669521406 posted

    thank it help but still i''m bit confused of the format of the date.. how it work .. i want it to show all total hours and minutes and seconds

    Sunday, January 12, 2014 1:08 PM
  • User-1509636757 posted

    i want it to show all total hours and minutes and seconds

    This will serve the same purpose.

    TryCast(item.FindControl("lblDuration"), Label).Text = New DateTime(duration.Ticks).ToString("HH:mm:ss")

    I mean to say this line of code will show the Ticks in total hours, minutes and seconds left duration till EndDate.

    Sunday, January 12, 2014 1:40 PM
  • User669521406 posted

    yes take it like this there is an end date which is 23 feburary 2014 and the duration say 11:01:06:58, dd/hh/mm/ss but there is no eleven day between now and 23 feb

    Sunday, January 12, 2014 1:54 PM
  • User-1509636757 posted

    take it like this there is an end date which is 23 feburary 2014 and the duration say 11:01:06:58, dd/hh/mm/ss but there is no eleven day between now and 23 feb

    Do you mean you wanted to show date (dd only) as well?

    Sunday, January 12, 2014 2:04 PM
  • User669521406 posted

    no what i mean is this 

    Have you seen ebay? auction, it show the end date first but when the auction arrive at the last day, it is converted into hh/mm/ss 

    this one i'm asking

    Sunday, January 12, 2014 2:21 PM
  • User-1509636757 posted

    it show the end date first but when the auction arrive at the last day, it is converted into hh/mm/ss 

    For this you can update UpdateDuration function to check if the item is having "last day" of auction and if yes, then display the hh/mm/ss otherwise show the end date. You can use duration.TotalDays (TimeSpen.TotalDays) to check if the duration is more than one day or not. Here is the example:

        private void UpdateDuration()
        {
            foreach (RepeaterItem item in repItems.Items)
            {
                //--calculate the duration
                TimeSpan duration = Convert.ToDateTime((item.FindControl("lblEndDate") as Label).Text) - DateTime.Now;
    
                //--Here is the important part: if any duration is expired then bind the grid again
                if (duration.TotalDays < 1 && duration.Ticks < 0)
                {
                    (item.FindControl("lblDuration") as Label).Text = new DateTime(duration.Ticks).ToString("HH:mm:ss");
                    //-- you can also write code to update database item status to = expired
                    //-- get the item id
                    Int64 itemId = Convert.ToInt64((item.FindControl("hdItemID") as HiddenField).Value);
                    //-- write code here to update item with this itemId to set the status to expire.
    
                    //-- bind the repeater again
                    BindItems();
                    break;
                }
                else
                {
                    //-- showing end date as last days has not arrived
                    (item.FindControl("lblDuration") as Label).Text = (item.FindControl("lblEndDate") as Label).Text;
                }
            }
        }

    Monday, January 13, 2014 12:17 AM
  • User669521406 posted

    Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
    Parameter name: ticks

    having this error

    Monday, January 13, 2014 2:45 AM
  • User-1509636757 posted

    Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.

    Do you still receive the same error if you write reverse condition statement like:

    if (duration.Ticks < 0 && duration.TotalDays < 1)

    Please check what value you are getting in duration.Tick when you have this error. That might be minus value. So, better to check for duration.Ticks < 0 first.

    Monday, January 13, 2014 2:57 AM
  • User669521406 posted

    now it is ok but in durtation it show the endate not the duration when it is last day

    Monday, January 13, 2014 3:04 AM
  • User669521406 posted
    here the the html and asp.net code
     <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Bid.aspx.vb" Inherits="Bid" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="SM1" runat="server">
            </asp:ScriptManager>
    
    
       <asp:repeater id="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">
          <HeaderTemplate>
                <ul>
          </HeaderTemplate>
          <ItemTemplate>
    
              <li> <a href="ProductDetails.aspx?ItemID=<%# Eval("ItemID") %>"> <asp:Image id="Image1" runat="server" ImageUrl="~/Seller/audio.gif" /></a>
             
                <div class="product-info">
                   <h3>
                       <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name")%>'></asp:Label> </h3>
                  <div class="product-desc">
                    <h4>Bid</h4>
                      <asp:TextBox ID="txtbid" runat="server"></asp:TextBox>
                    <asp:HiddenField ID="HiddenField1" Value='<%#Eval("EndDate")%>' runat="server" />
                      <asp:Label ID="lblauction" Visible="false" Text='<%#Eval("AuctionID")%>' runat="server" />
                        <asp:Label ID="Label4" Visible="false" Text='<%#Eval("Price")%>' runat="server" />
                      <asp:Label ID="Label2" Visible="false" Text='<%#Eval("Status")%>' runat="server" />
                   
                      <a href='<%#GenerateURL(Eval("AuctionID"))%>' style="border: 0 none white"><img id="Img1" src="~/Styles/Images/add_to_cart.gif" runat="server" alt="" style="border-width: 0" /></a><br />
                    <br/> <p>
                     <asp:Label ID="Label3" runat="server" ></asp:Label>
    
                         </li>
                          <asp:UpdatePanel id="updPnl" runat="server" UpdateMode="Conditional">
                           <ContentTemplate>
                         <asp:Label ID="lblTimer"   runat="server"></asp:Label>
                          <asp:Button ID="btnSave" runat="server" CommandName="Save" Text="btnSave" />
                           </ContentTemplate>
        
         <triggers>
            <asp:asyncpostbacktrigger ControlID="timer1" eventname="tick" />
          </triggers>
            </asp:UpdatePanel>
              </ItemTemplate>
          <FooterTemplate>
            
                </ul>
          </FooterTemplate>
         </asp:repeater>
    
        
           
          
          <asp:Timer ID="timer1" Interval="2000" OnTick="timer1_tick" runat ="server">
                       </asp:Timer>
                       
           
            
        </div>
        </form>
    </body>
    </html>
    


    Kaushalparik i have a problem with the code when i put a picture it is as if it look as a watch, for each second the image appear and disapear 

    but i have a way to avoid it but i don't how to rebind the data to the repeater when one item is expired can you help please

    here are the code 

    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Configuration
    Imports System.Net.Mail
    Imports System.Web.Configuration
    Imports System.Web.Script.Services
    Imports System.Web.Services
    Partial Class Bid
        Inherits System.Web.UI.Page
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("VirgoPlaza").ToString())
        Dim EndDate As DateTime
        Dim auction As Integer
    
        Private ReadOnly _start As String
        Public Sub New()
            _start = WebConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
        End Sub
       
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not Page.IsPostBack Then
    
                Using con1 As New SqlConnection(_start)
    
    
                    Dim sql1 As String = "SELECT Item.ItemID,Item.Price,Item.Name, Item.Description, Item.Price,Auction.EndDate,Auction.AuctionID,Auction.Status FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID Where Auction.Status='Valid' AND Auction.EndDate>=@endate "
                    Dim myCommand1 As New SqlCommand(sql1, con1)
                    myCommand1.Parameters.AddWithValue("@endate", DateTime.Now)
    
                    myCommand1.CommandType = CommandType.Text
    
                    con1.Open()
    
                    Dim category As SqlDataReader
                    category = myCommand1.ExecuteReader()
    
                    'binding data from category table to ddlcategory
                    Repeater1.DataSource = category
                    Repeater1.DataBind()
    
                End Using
    
            End If
    
    
        End Sub
        Public Function GenerateURL(ByVal Auction As Object) As String
    
            'Create URL for each product link
            Dim strProdUrl As String = "Wishlist.aspx?Pid=" & Auction
    
            Return strProdUrl
        End Function
    
    
        Protected Sub Repeater1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles Repeater1.ItemCommand
            If e.CommandName.Equals("Save") Then
    
                Dim BiddingPrice As String
                Dim dataToSave As String = TryCast(e.Item.FindControl("txtbid"), TextBox).Text
                Dim auction As String = TryCast(e.Item.FindControl("lblauction"), Label).Text
                Dim price As String = TryCast(e.Item.FindControl("Label4"), Label).Text
                Dim labelMsg As String = TryCast(e.Item.FindControl("Label3"), Label).Text
                Dim constr As String = ConfigurationManager.ConnectionStrings("VirgoPlaza").ConnectionString
                Dim con As New SqlConnection(constr)
    
    
                
                Dim Price1 As Integer = Convert.ToInt32(price)
                Dim Biddingcmd As New SqlCommand("SELECT BID.BiddingPrice FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID INNER JOIN BID ON Auction.AuctionID = BID.AuctionID WHERE (Auction.AuctionID = @auction)")
                Biddingcmd.CommandType = CommandType.Text
                Biddingcmd.Parameters.AddWithValue("@auction", auction)
                con.Open()
                Biddingcmd.Connection = con
                Dim result21 As String = Biddingcmd.ExecuteScalar()
                BiddingPrice = result21
    
    
    
    
    
    
                Dim txtbid As Integer = Convert.ToInt32(dataToSave)
    
                Dim auction1 As Integer = Convert.ToInt32(auction)
    
    
    
                Dim con1 As New SqlConnection(constr)
                con1.Open()
                If (txtbid > Price1) And (BiddingPrice = "") Then
    
                    Dim cmd As New SqlCommand("INSERT INTO BID VALUES(@Date, @BiddingPrice,@Status,@AuctionID,@BuyerID,@WinningPrice)")
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@Date", DateTime.Now)
                    cmd.Parameters.AddWithValue("@BiddingPrice", dataToSave)
                    cmd.Parameters.AddWithValue("@Status", "Available")
                    cmd.Parameters.AddWithValue("@AuctionID", auction1)
                    cmd.Parameters.AddWithValue("@BuyerID", 1)
                    cmd.Parameters.AddWithValue("@WinningPrice", "")
                    cmd.Connection = con1
                    cmd.ExecuteNonQuery()
    
                    MsgBox("SAVE")
    
    
    
    
                ElseIf (txtbid > price) And (txtbid > Convert.ToInt32(BiddingPrice)) And BiddingPrice <> "" Then
    
                    Dim cmd As New SqlCommand("INSERT INTO BID VALUES(@Date, @BiddingPrice,@Status,@AuctionID,@BuyerID,@WinningPrice)")
                    cmd.CommandType = CommandType.Text
                    cmd.Parameters.AddWithValue("@Date", DateTime.Now)
                    cmd.Parameters.AddWithValue("@BiddingPrice", dataToSave)
                    cmd.Parameters.AddWithValue("@Status", "Available")
                    cmd.Parameters.AddWithValue("@AuctionID", auction1)
                    cmd.Parameters.AddWithValue("@BuyerID", 1)
                    cmd.Parameters.AddWithValue("@WinningPrice", "")
    
                    cmd.Connection = con1
    
                    cmd.ExecuteNonQuery()
                    MsgBox("Save")
    
                Else
    
                    MsgBox("bID IS TOO LOW")
    
    
    
                End If
    
            End If
    
    
    
        End Sub
    
        Protected Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
            For Each item As RepeaterItem In Repeater1.Items
    
                Dim hidden = TryCast(item.FindControl("HiddenField1"), HiddenField)
    
                Dim label2 = TryCast(item.FindControl("lblTimer"), Label)
                'Grab your DateTime object (checking that it exists)'
    
                'Store your TimeSpan Difference'
                Dim ts = (DateTime.Now - DateTime.Parse(hidden.Value))
                Dim travelTime As New TimeSpan(0, 0, 0, 0)
                Dim result2 As Integer = TimeSpan.Compare(ts, travelTime)
                If result2 <= 0 Then
    
                    'Now you can output your time as you please (excluding any decimal points)'
                    label2.Text = String.Format("Time Left: {0:dd\:hh\:mm\:ss}", ts)
    
                Else
    
                    timer1.Enabled = True
                    Using con1 As New SqlConnection(_start)
    
                        Dim sql12 As String = "SELECT Auction.AuctionID FROM Item INNER JOIN Auction ON Item.ItemID = Auction.ItemID Where Auction.Status='Valid' AND Auction.EndDate<=@endate "
    
                        Dim cmd12 As New SqlCommand(sql12, con1)
                        con1.Open()
                        cmd12.Parameters.AddWithValue("@endate", DateTime.Now)
                        Dim query As Integer = cmd12.ExecuteScalar
    
    
    
    
    
    
    
                        Dim sql123 As String = "UPDATE  Auction  SET Status ='Expired' WHERE AuctionID =@auction"
                        Dim cmd21 As New SqlCommand(sql123, con1)
                        cmd21.Parameters.AddWithValue("@auction", query)
    
                        cmd21.ExecuteNonQuery()
    
                        ''query = Convert.ToInt32(HiddenItem.Value).ToString look in finalcoursework
    
    
                    End Using
    
                End If
            Next
           
        End Sub
    
    
        Function CalculateWinningPrice(ByVal query As Integer) As Integer
            Dim price As Integer
            Using con1 As New SqlConnection(_start)
                con1.Open()
    
    
                Dim sql1 As String = "SELECT MAX(BiddingPrice) AS Expr1 FROM BID WHERE (AuctionID = @auction)"
                Dim cmd1 As New SqlCommand(sql1, con1)
                cmd1.Parameters.AddWithValue("@auction", query)
                Dim max As Double = Convert.ToDouble(cmd1.ExecuteScalar)
                Dim cmd2 As New SqlCommand("SELECT MAX(BiddingPrice) AS Expr1 FROM BID WHERE (BiddingPrice <( SELECT MAX(BiddingPrice) AS Expr2 FROM BID AS BID_1 WHERE (AuctionID = @auction)))", con1)
                cmd2.Parameters.AddWithValue("@auction", query)
                Dim second As Double = Convert.ToDouble(cmd2.ExecuteScalar)
                Dim cmd3 As New SqlCommand("SELECT BuyerID FROM BID WHERE(BiddingPrice =(SELECT MAX(BiddingPrice) AS Expr1 FROM BID AS BID_1 WHERE(AuctionID = @auction)))", con1)
                cmd3.Parameters.AddWithValue("@auction", query)
                Dim Buyer As Integer = Convert.ToInt32(cmd3.ExecuteScalar)
    
    
                If max - second = 1 Then
                    price = second
    
                Else
                    If max - second > 10 Then
                        price = second + 1
                    Else
                        If max - second > 100 Then
                            price = second + 10
                        Else
                            If max - second > 1000 Then
                                price = second + 1000
                            End If
                        End If
                    End If
    
                End If
    
                Dim cmd As New SqlCommand("INSERT INTO BID VALUES(@Date, @BiddingPrice,@Status,@AuctionID,@BuyerID,@WinningPrice)")
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@Date", DateTime.Now)
                cmd.Parameters.AddWithValue("@BiddingPrice", max)
                cmd.Parameters.AddWithValue("@Status", "Won")
                cmd.Parameters.AddWithValue("@AuctionID", query)
                cmd.Parameters.AddWithValue("@BuyerID", Buyer)
                cmd.Parameters.AddWithValue("@WinningPrice", price)
    
                cmd.Connection = con1
    
                cmd.ExecuteNonQuery()
    
    
    
    
    
            End Using
            Return price
    
    
        End Function
    
    End Class

    Saturday, January 18, 2014 3:19 PM
  • User-1509636757 posted

    i don't how to rebind the data to the repeater when one item is expired

    I have already defined that in one of my previous post. Notice the comments in the code:

    private void UpdateDuration()
        {
            foreach (RepeaterItem item in repItems.Items)
            {
                //--calculate the duration
                TimeSpan duration = Convert.ToDateTime((item.FindControl("lblEndDate") as Label).Text) - DateTime.Now;
    
                //--Here is the important part: if any duration is expired then bind the grid again
                if (duration.TotalDays < 1 && duration.Ticks < 0)
                {
                    (item.FindControl("lblDuration") as Label).Text = new DateTime(duration.Ticks).ToString("HH:mm:ss");
                    //-- you can also write code to update database item status to = expired
                    //-- get the item id
                    Int64 itemId = Convert.ToInt64((item.FindControl("hdItemID") as HiddenField).Value);
                    //-- write code here to update item with this itemId to set the status to expire.
    
                    //-- bind the repeater again
                    BindItems();
                    break;
                }
                else
                {
                    //-- showing end date as last days has not arrived
                    (item.FindControl("lblDuration") as Label).Text = (item.FindControl("lblEndDate") as Label).Text;
                }
            }
        }

    Saturday, January 18, 2014 3:26 PM
  • User669521406 posted

    yes i know but when i insert an image in the repeater, it is appearing and disappearing each 1 seconds :(

    Saturday, January 18, 2014 3:29 PM
  • User669521406 posted

    and if you check my previous post you wiill notice that when arriving at the expired date, the timer refresh the page, which i don''t want, :(, hopefully you could find a solution for this please 

    Saturday, January 18, 2014 3:33 PM
  • User-1509636757 posted

    Jdaniel13

    i know but when i insert an image in the repeater, it is appearing and disappearing each 1 seconds :(

    Les all know what should happen? I guess you are hijacking the original thread here.

    Saturday, January 18, 2014 3:47 PM
  • User669521406 posted

    Ok so

    (i) first of all i need to display each item in a repeater and calculate his time duration 

    (i) when the item expired i need that the item remove from the page without refresh

    (ii) but the problem with the first code we write first is that all is working good :)

    but when i insert the item image in the repeater

    it appear and disapear everyone second, which cause a problem and i can insert any value in a textbox due to refresh every second

    you see now? sorry i didn't want to cause any offence 

    Saturday, January 18, 2014 3:56 PM