Imports System
Imports System.Data
Imports System.Data.SqlClient

Partial Class UserControl_ArticleRightBanner
    Inherits System.Web.UI.UserControl
    Dim pdt As New DataTable
    Dim rcon As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'imgbtnGo.ImageUrl = Page.ResolveUrl("~/Images/go.jpg")
        If Not IsPostBack Then
            'imgbtnGo.ImageUrl = Page.ResolveUrl("~/Images/go.jpg")
            Try
                Dim str As String
                If Session("MailId") <> "" Or Session("three") <> Nothing Then
                    ' PrAlert.Visible = False
                    ' str = "SELECT top(2) Product_Kid,CatName,ProdName,Product_Name,Product_Image,Brand_Name,SUBSTRING(ISNULL(Product_Description, N'N.A.'), 1, 50) as Product_Description,Product_MarketPrice  from ProductDisplay ORDER BY Product_RegDate desc"
                    If Not IsNothing(Session("dt1")) Then
                        pdt = Session("dt1")
                        DataList1.DataSource = pdt
                        DataList1.DataBind()
                    Else
                        ' str = "SELECT Product_Information.Product_Kid, Product_Information.Product_Code, Product_Information.Product_Name, Product_Information.Product_ModelNo, Product_Information.Product_AtcoPrice, Product_Information.Brand_Name, Product_Information.Product_Image, Product_Information.Product_BrandId, Product_Information.Product_Approved, SUBSTRING(ISNULL(Product_Information.Product_Description, N'N.A.'), 1, 100) + '' + SUBSTRING(ISNULL(Product_Information.Product_Description, N'N.A.'), 101, CHARINDEX('.', SUBSTRING(ISNULL(Product_Information.Product_Description, N'N.A.'), 101, 400))) AS Product_Description, REPLACE(REPLACE(REPLACE(LOWER(RTRIM(LTRIM(Category_Master.Category_Name))), ' ', '-'), '  ', '-'), '&', '-') AS CatName, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LOWER(RTRIM(LTRIM(Product_Information.Product_Name))), ' ', '-'), '  ', '-'), '&', '-'),'/','-'),'\','-') AS ProdName, Product_Information.Product_CategoryId FROM Product_Information INNER JOIN Category_Master ON Product_Information.Product_CategoryId = Category_Master.Category_Kid WHERE     (Product_Information.Product_Approved = 'Y') AND (Product_Information.Product_Code='ABT001')" ','ABT002'))" 'AHP006', 'AJP001'))"
                        'TOP (2) Product_Kid,Product_Master.Product_Name, Product_Master.Product_BrandId, Brand_Master.Brand_Name, Product_Master.Product_Image FROM  Product_Master INNER JOIN  Brand_Master ON Product_Master.Product_BrandId = Brand_Master.Brand_Kid  where Product_Master.Product_Approved='Y' and Product_Master.Product_Isdeleted='0'     ORDER BY Product_Master.Product_RegDate desc"
                        Dim dt_products As DataTable = selectItems("2")
                        Session("dt1") = dt_products
                        DataList1.DataSource = dt_products
                        DataList1.DataBind()
                    End If
                    rcon.Close()
                Else
                    ' PrAlert.Visible = True
                    If Not IsNothing(Session("dt")) Then
                        pdt = Session("dt")
                        DataList1.DataSource = pdt
                        DataList1.DataBind()
                    Else
                        ' str = "SELECT Product_Information.Product_Kid, Product_Information.Product_Code, Product_Information.Product_Name, Product_Information.Product_ModelNo, Product_Information.Product_AtcoPrice, Product_Information.Brand_Name, Product_Information.Product_Image, Product_Information.Product_BrandId, Product_Information.Product_Approved, SUBSTRING(ISNULL(Product_Information.Product_Description, N'N.A.'), 1, 100) + '' + SUBSTRING(ISNULL(Product_Information.Product_Description, N'N.A.'), 101, CHARINDEX('.', SUBSTRING(ISNULL(Product_Information.Product_Description, N'N.A.'), 101, 400))) AS Product_Description, REPLACE(REPLACE(REPLACE(LOWER(RTRIM(LTRIM(Category_Master.Category_Name))), ' ', '-'), '  ', '-'), '&', '-') AS CatName, REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LOWER(RTRIM(LTRIM(Product_Information.Product_Name))), ' ', '-'), '  ', '-'), '&', '-'),'/','-'),'\','-') AS ProdName, Product_Information.Product_CategoryId FROM Product_Information INNER JOIN Category_Master ON Product_Information.Product_CategoryId = Category_Master.Category_Kid WHERE     (Product_Information.Product_Approved = 'Y') AND (Product_Information.Product_Code='ABT001')" ','ABT002'))" 'AHP006', 'AJP001'))"
                        'TOP (2) Product_Kid,Product_Master.Product_Name, Product_Master.Product_BrandId, Brand_Master.Brand_Name, Product_Master.Product_Image FROM  Product_Master INNER JOIN  Brand_Master ON Product_Master.Product_BrandId = Brand_Master.Brand_Kid  where Product_Master.Product_Approved='Y' and Product_Master.Product_Isdeleted='0'     ORDER BY Product_Master.Product_RegDate desc"
                        Dim dt_products1 As DataTable = selectItems("1")
                        Session("dt") = dt_products1
                        DataList1.DataSource = dt_products1
                        DataList1.DataBind()
                    End If
                    rcon.Close()
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub
    Public Function selectItems(ByVal value As String) As DataTable

        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataTable
        cn.Open()
        Try
            objAdapter = New SqlDataAdapter("SelectProductDisplay_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@Count_var", SqlDbType.NVarChar, 100))
            objAdapter.SelectCommand.Parameters("@Count_var").Value = value

            ObjDS = New DataTable
            objAdapter.Fill(ObjDS)
            Return ObjDS

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Function
    'Protected Sub imgbtnGo_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgbtnGo.Click
    '    If txtEmailId.Text <> "" Then
    '        Session("backcatid") = Nothing
    '        Dim csortid As String
    '        csortid = Request.QueryString("csid")
    '        Session("catid") = csortid
    '        Session("MailId") = txtEmailId.Text
    '        Response.Redirect("ShowCategory.aspx?mail=" & Session("MailId") & "")
    '    End If
    'End Sub
   
    Protected Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        '  If e.Item.ItemType = ListItemType.Item Then
        Try


            Dim lblprice As Label = CType(e.Item.FindControl("lblprice"), Label)
            Dim lnkprice As LinkButton = CType(e.Item.FindControl("lnkprice"), LinkButton)
            Dim pimg As Image = CType(e.Item.FindControl("Image1"), Image)
            CType(e.Item.FindControl("lblimg"), Label).Visible = True
            pimg.ImageUrl = Page.ResolveUrl("~/Upload Image/Product Image/UploadImages/" & CType(e.Item.FindControl("lblimg"), Label).Text.ToString)
            If CType(e.Item.FindControl("lblimg"), Label).Text.ToString <> "" Then
                Dim imgpath As String
                imgpath = Server.MapPath(pimg.ImageUrl)
                KeepAspectRatioNew(pimg, imgpath, 80, 80)
            End If
            CType(e.Item.FindControl("lblimg"), Label).Visible = False
            lblprice.Text = "Rs. " & Producttotalprice(lnkprice.CommandArgument.ToString)
            If (lblprice.Text.ToString() <> "") Then
                If Convert.ToDouble(lblprice.Text.Replace("Rs. ", "").Trim) = 0 Then
                    lblprice.Visible = False
                    lnkprice.Visible = True
                Else
                    lblprice.Visible = True
                    lnkprice.Visible = False
                End If
            Else
                lblprice.Visible = False
                lnkprice.Visible = True
            End If
            Dim ID As String
            'Dim lnk As LinkButton
            'lnk = CType(e.Item.FindControl("LinkButton1"), LinkButton)
            ID = lnkprice.CommandArgument.ToString 'lnk.CommandArgument
            CType(e.Item.FindControl("lnkprice"), LinkButton).Attributes.Add("onclick", "return openform('" & EncodeURL(ID) & "');")
            '  End If
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim str As String
        Dim lnk As LinkButton
        lnk = CType(sender, LinkButton)
        str = lnk.CommandArgument
        Dim Wishtabel As New DataTable
        Wishtabel.Columns.Add("ProductId")
        If Session("BuyId") = Nothing Then
            Dim wsrow As DataRow = Wishtabel.NewRow
            wsrow("ProductId") = str
            Wishtabel.Rows.Add(wsrow)
            Session("WishTabel") = Nothing
            Session("WishTabel") = Wishtabel
            Session("Checkstatus") = "WishLsit"
            Response.Redirect("~/Buyer/buyer-home.aspx")
        Else
            WishListProcedure("Insert", str)
            Response.Redirect("~/Buyer/WishList.aspx")
        End If

    End Sub

    Protected Sub WishListProcedure(ByVal StrMode As String, ByVal prodid As String)
        Try
            Dim wishlistid As String = NewPrimaryKey(Me.Page)
            wishlistid = RemoveLiterals(wishlistid)
            Dim wishlistCode As String = Generate_Code("select count(*) from AddWishList where AddWishList_Code='", "WL")
            Dim con As New SqlConnection                        'Define connection variable for connecting to SQL-Server
            con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("ConnectionString")    'Read the ConnectionString from web.config File
            Dim cmd As New SqlCommand
            con.Open()
            Dim str As String = "select AddWishList_Code from AddWishList where AddWishList_ProductId='" & prodid & "' and AddWishList_BuyerUserId='" & Session("BuyId") & "'"
            Dim da As New SqlDataAdapter(str, con)
            Dim ds As New DataSet
            da.Fill(ds)
            If ds.Tables(0).Rows.Count > 0 Then

            Else
                Dim sqlcol As Data.SqlClient.SqlParameterCollection = cmd.Parameters
                sqlcol.Add("@AddWishList_Kid", SqlDbType.NVarChar, 10).Value = wishlistid.Trim
                sqlcol.Add("@AddWishList_Code", SqlDbType.NVarChar, 25).Value = wishlistCode.Trim
                sqlcol.Add("@AddWishList_ProductId", SqlDbType.NVarChar, 10).Value = prodid
                sqlcol.Add("@AddWishList_BuyerUserId", SqlDbType.NVarChar, 25).Value = Session("BuyId")
                sqlcol.Add("@AddWishList_FinancialYearId", SqlDbType.NVarChar, 10).Value = "2007"
                sqlcol.Add("@AddWishList_CompanyId", SqlDbType.NVarChar, 10).Value = Session("companyId")
                sqlcol.Add("@AddWishList_UserId", SqlDbType.NVarChar, 10).Value = Session("UserId")
                sqlcol.Add("@FormName", SqlDbType.NVarChar, 50).Value = Me.Page.ToString
                sqlcol.Add("@Mode", SqlDbType.VarChar, 10).Value = StrMode.Trim
                ExecQuery("AddWishList_Proc", sqlcol).StartsWith("True")
                sqlcol = Nothing
                cmd = Nothing
                ' Dim gvRow As GridViewRow
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
