Hi Waghmare,
Add two connection string for both the application. Create common method for Upload File with connectionstring as parameter.
Then call two times for both the server like below example.
ConnectionString
<connectionStrings>
<add name="constr" connectionString="Data Source=myServerName\myInstanceName;Initial Catalog=DataBaseName;Integrated Security=true"/>
<add name="constr1" connectionString="Data Source=myServerName\myInstanceName;Initial Catalog=DataBaseName;Integrated Security=true"/>
</connectionStrings>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Files", conn))
{
DataTable dt = new DataTable();
sda.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
}
}
}
protected void Upload(object sender, EventArgs e)
{
// Upload to server 1.
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
UploadFile(constr);
// Upload to server 2.
string constr1 = ConfigurationManager.ConnectionStrings["constr1"].ConnectionString;
UploadFile(constr1);
Response.Redirect(Request.Url.AbsoluteUri);
}
private void UploadFile(string constr)
{
//Extract Image File Name.
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//Set the Image File Path.
string filePath = "~/Uploads/" + fileName;
//Save the Image File in Folder.
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO Files VALUES(@Name, @Path)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", fileName);
cmd.Parameters.AddWithValue("@Path", filePath);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using conn As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM Files", conn)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
gvImages.DataSource = dt
gvImages.DataBind()
End Using
End Using
End If
End Sub
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
' Upload to server 1.
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
UploadFile(constr)
' Upload to server 2.
Dim constr1 As String = ConfigurationManager.ConnectionStrings("constr1").ConnectionString
UploadFile(constr1)
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Private Sub UploadFile(constr As String)
'Extract Image File Name.
Dim fileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
'Set the Image File Path.
Dim filePath As String = "~/Uploads/" & fileName
'Save the Image File in Folder.
FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath))
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO Files VALUES(@Name, @Path)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", fileName)
cmd.Parameters.AddWithValue("@Path", filePath)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Sub
Note: If anything difference between them then make it as parameter and pass.