Package runs twice via ASP (VB.NET) code
Hello all,
I am facing a very very unusual problem. I created a package in SSIS studio. Basically, it takes an excel file and enters the value in SQL Server database table. The package runs quite fine in standalone mode (double click on dtsx file).
Now when I upload the excel file via web interface (written in vb.net) then it seems that package runs twice. I see 8 lines of data instead of 4 (which is originally in excel file). I am banging my head for 2 days, and I have no idea what is making
it to run twice.
Here is the code, and if you can point out my mistake:
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Dts.Runtime
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub EntityUploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EntityUploadButton.Click
'Checking if File Control has file, and if the file is .xls or .xlsx. Also getting misc. information about file
If EntityUpload.HasFile Then
Dim fileName As String = Server.HtmlEncode(EntityUpload.FileName)
Dim extension As String = System.IO.Path.GetExtension(fileName)
If (extension = ".xls" Or extension = ".xlsx") Then
EntityUpload.SaveAs(Server.MapPath("~/upload/" & _
EntityUpload.FileName))
EntityUploadStatus.Text = "File name: " & _
EntityUpload.PostedFile.FileName & "<br>" & _
"File Size: " & _
EntityUpload.PostedFile.ContentLength & " b<br>" & _
"Content type: " & _
EntityUpload.PostedFile.ContentType
'Display Data in Grid
Dim FileConnection = New OleDbConnection( _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Server.MapPath("~/upload/" + EntityUpload.FileName) & ";" & _
"Extended Properties=""Excel 12.0;HDR=Yes""")
FileConnection.Open()
Dim SQLReadString As String = "Select * from [Sheet1$]"
Dim DBCommand = New OleDbCommand(SQLReadString, FileConnection)
Dim DBReader As IDataReader = DBCommand.ExecuteReader()
grid.DataSource = DBReader
grid.DataBind()
DBReader.Close()
FileConnection.Close()
'Insert Data in database
Dim pkg As New Package
Dim app As Application = New Application
Dim pkgResults As DTSExecResult
pkg = app.LoadPackage(Server.MapPath("~//upload//Products.dtsx"), Nothing)
pkg.Connections("ExcelConnectionManager").ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~//upload//Products.xlsx") + ";Extended Properties=""Excel 12.0;HDR=YES"""
'pkg.ImportConfigurationFile(Server.MapPath("~//upload//Products.dtsConfig"))
'vars("ExcelSource").Value = (Server.MapPath("~/upload/" & _ EntityUpload.FileName))
pkgResults = pkg.Execute()
pkg.Dispose()
pkg = Nothing
Else
EntityUploadStatus.Text = "Error: Only Excel files are allowed"
End If
Else
EntityUploadStatus.Text = "You have not specified a file."
End If
End Sub
End Class
Ad here is Web interface code:
<%@ Page Title="Home Page" Language="VB" AutoEventWireup="false"
CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Excel File Uploader</title>
<body>
<br /><br />
<form id="UploadForm" runat="server" enctype="multipart/form-data">
<table>
<tbody>
<tr>
<td><asp:fileUpload id="EntityUpload" runat="server" /></td>
<td> </td>
<td><asp:button id="EntityUploadButton" text="Upload" runat="server" height = "23px" onclick="EntityUploadButton_Click" /></td>
<td><asp:label id="EntityUploadStatus" runat="server" /></td>
<td><asp:label id="EntityUploadStatus1" runat="server" /></td>
</tr>
</tbody>
</table>
<asp:GridView runat="server" id="grid" />
</form>
</body>
</html>
February 14th, 2011 5:22pm
yes, you are right. It seems that form has been submitted twice when I press upload, and it is not pressed twice
How may I prvent this? via some html control in forum declaration? (<form id= .......>) or in button code?
Thanks :-)
Free Windows Admin Tool Kit Click here and download it now
February 14th, 2011 5:59pm
Thanks..I found the solution finally :-)
Here it is: http://geekswithblogs.net/TimH/archive/2006/10/23/94874.aspx
"
This seems to be a very common problem where by your event handler method gets run twice.
It is caused by VS.NET inserting 2 wireup of the event handler:
- once in the aspx (HTML) eg:
<asp:Button ... OnClick="btnTest_Click" />
- and once in the VS.NET generated section (InitializeComponent) eg:
this.btnTest.Click += new System.EventHandler(this.btnTest_Click);
The easiest solution, imo, is to simply remove the "OnClick ..." HTML markup from the .aspx page.
Eg change this:
<asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />
To this:
<asp:Button ID="btnTest" runat="server" Text="Test" />
Rebuild, and bingo! - 1 event per click. "
February 16th, 2011 4:21pm