The app searches postoffices via street address, post code or town and works fine, my problem is that i need to be able to upload csv of the post office information into my db, i cant use bulk insert or dts so need to do this through an aspx page.
My code so far saves a csv file to a directory and then opens the file and reads it, currently it then displayed this to a datagrid but i simply want to insert the read information into my db, can someone please look at my code and help me achieve this as im very lost with it all and have become very confused about how to get the information in there. Ive search many sites and msdn and cant find anything that meets my needs, your help would be greatly appreciated
My csv file does not have headers, my database has 8 columns, ID,AddressLine1,AddressLine2,AddressLine3,AddressL ine4,AddressLine5,AddressLine6,Postcode. The ID is generated each time a new line is inserted but not sure how to achieve this either, please see the code below.
Expand|Select|Wrap|Line Numbers
- <script runat="server">
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
- If (IsPostBack) Then
- Grid1.Visible = True
- Else
- Grid1.Visible = False
- End If
- End Sub
- Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
- 'Save the uploaded file to an "Uploads" directory
- Dim saveDir As String = "\Data\"
- ' Get the physical file system path for the currently
- ' executing application.
- Dim appPath As String = Request.PhysicalApplicationPath
- ' Before attempting to save the file, verify
- ' that the FileUpload control contains a file.
- If (FileUpload1.HasFile) Then
- Dim savePath As String = appPath + saveDir + FileUpload1.FileName
- ' Call the SaveAs method to save the
- ' uploaded file to the specified path.
- FileUpload1.SaveAs(savePath)
- ' Notify the user that the file was uploaded successfully.
- UploadStatusLabel.Text = "Your file was uploaded successfully."
- Else
- ' Notify the user that a file was not uploaded.
- UploadStatusLabel.Text = "You did not specify a file to upload."
- End If
- End Sub
- Sub DisplayButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
- Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\inetpub\wwwroot\MerlinLocalPostOfficeApp \Data;Extended Properties=""text;HDR=NO;FMT=Delimited"""
- Dim objConn As New OleDbConnection(sConnectionString)
- objConn.Open()
- Dim objCmdSelect As New OleDbCommand("SELECT * FROM csv.txt", objConn)
- Dim objAdapter1 As New OleDbDataAdapter()
- objAdapter1.SelectCommand = objCmdSelect
- Dim objDataset1 As New DataSet()
- objAdapter1.Fill(objDataset1, "csv.txt")
- Grid1.DataSource = objDataset1.Tables(0).DefaultView
- Grid1.DataBind()
- objConn.Close()
- End Sub
- </script>