Hi dnnyobi,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Data;
using System.IO;
VB.Net
Imports System.Data
Imports System.IO
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("PhoneNumber") });
dt.Rows.Add(1, "Jude", "123456789");
dt.Rows.Add(2, "Paul", "233343546");
dt.Rows.Add(3, "Joy", "635342568");
dt.Rows.Add(4, "Peter", "070323453");
foreach (DataRow dr in dt.Rows)
{
string name = dr["Name"].ToString();
string[] filePaths = Directory.GetFiles(Server.MapPath("~/Staff"));
foreach (string file in filePaths)
{
if (name == Path.GetFileNameWithoutExtension(file))
{
File.Copy(file, Server.MapPath("~/Temp/" + Path.GetFileName(file)));
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("PhoneNumber")})
dt.Rows.Add(1, "Jude", "123456789")
dt.Rows.Add(2, "Paul", "233343546")
dt.Rows.Add(3, "Joy", "635342568")
dt.Rows.Add(4, "Peter", "070323453")
For Each dr As DataRow In dt.Rows
Dim name As String = dr("Name").ToString()
Dim filePaths As String() = Directory.GetFiles(Server.MapPath("~/Staff"))
For Each filePath As String In filePaths
If name = Path.GetFileNameWithoutExtension(filePath) Then
File.Copy(filePath, Server.MapPath("~/Temp/" & Path.GetFileName(filePath)))
End If
Next
Next
End If
End Sub