Imports System.IO
Public Class addorder
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lnkClose As System.Web.UI.WebControls.LinkButton
    Protected WithEvents lblError As System.Web.UI.WebControls.Label
    Protected WithEvents Label17 As System.Web.UI.WebControls.Label
    Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
    Protected WithEvents Submit1 As System.Web.UI.HtmlControls.HtmlInputButton

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        lnkClose.Attributes.Add("onclick", "javascript:window.opener.document.location=window.opener.document.location.href;javascript:window.close();")
    End Sub
    Public Sub readcsv()
        Dim filetoread As String
        filetoread = File1.Value
        'filetoread = "d:/Order.csv"

        Dim myfile As StreamReader
        Dim strArry As String
        Dim mysplit As String

        myfile = File.OpenText(filetoread)
        While myfile.Peek() <> -1
            Dim readcontents As String

            readcontents = myfile.ReadLine
            Dim textDelimeter As String
            textDelimeter = ","
            Dim splitout = Split(readcontents, textDelimeter)
            AddRec(splitout)
        End While
        myfile.Close()
    End Sub
    Public Sub AddRec(ByVal strAdd As System.Array)
        Dim status As String
        Dim objDBClass As New Acgg.dbClass
        Dim strQuery As String
        Dim strTime As String
        Dim PropId As Integer
        Dim TitleCoUserId As String

        strTime = "00:00 AM"
        status = "OPEN"
        PropId = GetPropID(strAdd(1))
        TitleCoUserId = GetUserID(strAdd(2))

        If (PropId > 0) And (TitleCoUserId <> "") Then
            strQuery = "INSERT INTO NewOrders(FileNo,PropId,TitleCoUserId,LoanAmt,PurPrice,Borrower,DueDate,PrjCLDate,Status,OpenDate,CLTime) VALUES('" & Replace(strAdd(0), "'", "") & "'," & PropId & ",'" & Replace(TitleCoUserId, "'", "") & "'," & strAdd(3) & "," & strAdd(4) & ",'" & strAdd(5) & "','" & Replace(strAdd(6), "'", "") & "','" & Replace(strAdd(7), "'", "") & "','" & Replace(status, "'", "") & "',Date(),'" & strTime & "')"
            Try
                objDBClass.OpenDB()
                If objDBClass.boolManipulateData(strQuery) Then
                    lblError.Text = "Updation successful..."
                End If
                objDBClass.CloseDB()
            Catch Exc As Exception
                lblError.Text = "Error: " & Exc.Message
            Finally
                objDBClass = Nothing
            End Try
        Else
            objDBClass = Nothing
            Exit Sub
        End If
    End Sub

    Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick
        If Not (File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0) Then

            Dim objDBClass As New Acgg.dbClass
            Dim objRead As OleDb.OleDbDataReader
            Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)

            '-------------************** It should be changed *****************--------------
            Dim SaveLocation As String = Server.MapPath("Acggdoc\Docs") & "\" & fn
            ''Dim SaveLocation As String = "d:\Orders" & "\" & fn
            'Dim SaveLocation As String = "d:" & "\" & fn
            '-------------************** It should be changed *****************--------------

            'Dim SaveLocation As String = Server.MapPath(Request("FileNo")) & "\" & fn
            Dim strSQL As String
            Dim OrdId As Integer

            Try

                File1.PostedFile.SaveAs(SaveLocation)
                lblError.ForeColor = System.Drawing.Color.Green
                lblError.Text = "The following file has been uploaded - " & fn
                'Read the uploaded file and add a new order
                readcsv()
            Catch Exc As Exception
                lblError.ForeColor = System.Drawing.Color.Red
                lblError.Text = lblError.Text & "Error: " & Exc.Message
            Finally

            End Try
        Else
            lblError.ForeColor = System.Drawing.Color.Red
            lblError.Text = "Please select a file to upload.Also provide some file description"
        End If
    End Sub
    Public Function GetPropID(ByVal FieldValue As String) As Integer

        Dim objDBClass As New Acgg.dbClass
        Dim objRead As OleDb.OleDbDataReader
        Dim strQuery As String
        Dim FieldId As Integer

        strQuery = "Select PropId From Property Where PropDesc = " & "'" & FieldValue & "'"
        Try
            objDBClass.OpenDB()
            objRead = objDBClass.retReader(strQuery)

            If objRead.HasRows Then
                Do While objRead.Read
                    FieldId = objRead("PropId")
                Loop
            End If

            objRead.Close()
            objDBClass.CloseDB()
            GetPropID = FieldId

        Catch Exc As Exception
            lblError.Text = "Error: " & Exc.Message
        Finally
            objRead = Nothing
            objDBClass = Nothing
        End Try

    End Function
    Public Function GetUserID(ByVal FieldValue As String) As String

        Dim objDBClass As New Acgg.dbClass
        Dim objRead As OleDb.OleDbDataReader
        Dim strQuery As String
        Dim FieldId As String

        strQuery = "Select UserId From Users Where Name = " & "'" & FieldValue & "'"
        Try
            objDBClass.OpenDB()
            objRead = objDBClass.retReader(strQuery)

            If objRead.HasRows Then
                Do While objRead.Read
                    FieldId = objRead("UserId")
                Loop
            End If

            objRead.Close()
            objDBClass.CloseDB()
            GetUserID = FieldId

        Catch Exc As Exception
            lblError.Text = "Error: " & Exc.Message
        Finally
            objRead = Nothing
            objDBClass = Nothing
        End Try

    End Function

End Class
