Hi nedash,
Use FileUpload control to select the file.
Check this example. Now please take its reference and correct your code.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Restore" runat="server" OnClick="OnRestore" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
protected void Restore(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
string data = System.IO.File.ReadAllText(Server.MapPath("~/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
SqlCommand cmd = new SqlCommand(data, con);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
File.Delete(Server.MapPath("~/") + Path.GetFileName(FileUpload1.PostedFile.FileName));
}
}
VB.Net
Protected Sub Restore(ByVal sender As Object, ByVal e As EventArgs)
If FileUpload1.HasFile Then
FileUpload1.SaveAs(Server.MapPath("~/") & Path.GetFileName(FileUpload1.PostedFile.FileName))
Dim data As String = System.IO.File.ReadAllText(Server.MapPath("~/") & Path.GetFileName(FileUpload1.PostedFile.FileName))
Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Dim cmd As SqlCommand = New SqlCommand(data, con)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
File.Delete(Server.MapPath("~/") & Path.GetFileName(FileUpload1.PostedFile.FileName))
End If
End Sub