﻿Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class Training
    Inherits System.Web.UI.Page
    Dim str As String = "Data Source=.\SQLEXPRESS;Initial Catalog=HospitalData;Integrated Security=True"



    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        
        Try
            Dim dontd As String
            dontd = Date.Now.ToString()
            Dim conn As SqlConnection = New SqlConnection(str)
            Dim cmd As SqlCommand = New SqlCommand
            cmd.CommandType = CommandType.Text

            'insert into usercontact(name,email,fax,phone) values('ali','aaa@yahoo.com','23232','32323')
            cmd.CommandText = "insert into donation(donation_name,deadline,don_cond,dondate) values('" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + dontd.ToString() + "')"
            conn.Open()
            cmd.Connection = conn
            cmd.ExecuteNonQuery()
            conn.Close()
            ''''
            Dim con2 As SqlConnection = New SqlConnection(str)
            Dim cmd2 As SqlCommand = New SqlCommand
            cmd2.CommandType = CommandType.Text

            cmd2.CommandText = "select donation_no from donation where donation_name = '" + TextBox2.Text + "'"
            Label13.Text = " شكرا لقد تم ارسال طلبك بنجاح"

            con2.Open()
            cmd2.Connection = con2
            Dim r As SqlDataReader = cmd2.ExecuteReader

            While (r.Read)


                Try

                    Session("dondoc") = r("donation_no").ToString
                Catch ex As Exception
                    Label6.Text = "no data for this name"
                End Try
            End While

            'r.Close()
            'con2.Close()

            Label12.Text = "تاريخ تقديم العطاء" + dontd.ToString()
            
            Try
                Dim fold As String = "~\donationpic1\" + Session("dondoc")
                Dim ise As Boolean = System.IO.Directory.Exists(Server.MapPath(fold))

                If Not ise Then
                    System.IO.Directory.CreateDirectory(Server.MapPath(fold))
                End If

                Session("foldssdon1") = fold

                Try

                    If (FileUpload1.HasFile) Then

                       

                        FileUpload1.SaveAs(MapPath("~\donationpic1\" + Session("dondoc") + "\" + FileUpload1.FileName))

                        Session("picfile") = FileUpload1.FileName

                        'Else
                        '    Label7.Text = "The file not word."

                        'End If
                        'Else
                        Label13.Text = "لم تقم بارفاق ملف"
                    End If
                Catch ex As Exception

                    Label13.Text = "لايوجد ملف مرفق"

                End Try


                r.Close()
                con2.Close()

                TextBox2.Text = ""
                TextBox3.Text = ""
                TextBox4.Text = ""
                

            Catch ex As Exception
                Label13.Text = "no data for this item"

            End Try

            Try
                Dim con3 As SqlConnection = New SqlConnection(str)
                Dim cmd3 As SqlCommand = New SqlCommand
                cmd3.CommandType = CommandType.Text
                'insert into usercontact(name,email,fax,phone) values('ali','aaa@yahoo.com','23232','32323')
                cmd3.CommandText = "update  donation set don_pic='" + Session("picfile").ToString() + "', foldpic= '" + Session("dondoc").ToString + "' where donation_no = '" + Session("dondoc").ToString() + "' "
                con3.Open()
                cmd3.Connection = con3
                cmd3.ExecuteNonQuery()
                con3.Close()

                Label13.Text = "شكراتم ارسال العطاء بنجاح"

            Catch ex As Exception
                Label7.Text = Session("picfile").ToString
                Label12.Text = "No file received."
            End Try


            'End If

            Label10.Text = Session("picfile").ToString()

            
           
        Catch ex As Exception

            Label13.Text = "لم يتم تقديم الطلب حاول مرة اخرى"

        End Try


        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        


        'Catch ex As Exception
        '    Label12.Text = "no data for this item"
        'End Try


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try

            'File.Delete(Path.Combine(Server.MapPath("~\donationpic1\33"), "Image3.jpg"))
            File.Delete(Path.Combine(Server.MapPath("~\donationpic1\"), Session("picfile").ToString()))
        Catch ex As Exception
            Label13.Text = "no file deleted"
        End Try

    End Sub

    'Protected Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles GridView1.RowDeleted
    '    'Dim x As String = GridView1.DeleteRow.Cells(1).Text
    '    'Dim x As String = GridView1.DeleteRow.cell 
    '    'Try

    '    '    'File.Delete(Path.Combine(Server.MapPath("~\donationpic1\33"), "Image3.jpg"))
    '    '    File.Delete(Path.Combine(Server.MapPath("~\donationpic1\" + Session("dondoc").ToString() + "\"), Session("picfile").ToString()))
    '    'Catch ex As Exception
    '    '    Label13.Text = "no file deleted"
    '    'End Try
    'End Sub

    'Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
    '    Try

    '        'File.Delete(Path.Combine(Server.MapPath("~\donationpic1\33"), "Image3.jpg"))
    '        File.Delete(Path.Combine(Server.MapPath("~\donationpic1\" + Session("dondoc").ToString() + "\"), Session("picfile").ToString()))
    '    Catch ex As Exception
    '        Label13.Text = "no file deleted"
    '    End Try
    'End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub
End Class
