0

I have almost finished a site that I've been working on but have one last thing that I don't know where to add. I have a bulk insert sql query that I want to insert into the .vb code behind and have no idea where to put it. Basically, the code is attached to a serverclick event that's to be triggered when a user clicks a submit button. Here is the .vb file as it is currently configured:

Partial Class _Default
    Inherits System.Web.UI.Page


    Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick

        If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
            Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
            Dim SaveLocation As String = Server.MapPath("Data") & "\" & fn
            Dim fileName As String = Server.HtmlEncode(File1.FileName)
            ' Get the extension of the uploaded file.
            Dim extension As String = System.IO.Path.GetExtension(fileName)
            ' Allow only files with .txt extensions' to be uploaded.
            Try
                File1.PostedFile.SaveAs(SaveLocation)
                Response.Write(<center>Thank you for your submission.</center>)
                Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
                conn = New SqlConnection(connection)
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)


            End Try
        Else
            Response.Write(<center>Please select a file to upload.</center>)
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class

here is the sql bulk insert (please note that I won't need to create a new table as the table is already inserted. This sql query has been tested for the purposes of what I'm trying to do and works, so I need this query inserted into the .vb code behind)

bulk insert dialerresults
from '\\MSBWEB3\data\test.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Select * 
from dialerresults
go

I'm assuming that this goes after the TRY statement but as I'm still learning asp.net, I have no idea how to call the command. Any assistance would be helpful.

Thank you

Doug

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by rohand
0

I have almost finished a site that I've been working on but have one last thing that I don't know where to add. I have a bulk insert sql query that I want to insert into the .vb code behind and have no idea where to put it. Basically, the code is attached to a serverclick event that's to be triggered when a user clicks a submit button. Here is the .vb file as it is currently configured:

Partial Class _Default
    Inherits System.Web.UI.Page


    Private Sub Submit1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Submit1.ServerClick

        If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
            Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
            Dim SaveLocation As String = Server.MapPath("Data") & "\" & fn
            Dim fileName As String = Server.HtmlEncode(File1.FileName)
            ' Get the extension of the uploaded file.
            Dim extension As String = System.IO.Path.GetExtension(fileName)
            ' Allow only files with .txt extensions' to be uploaded.
            Try
                File1.PostedFile.SaveAs(SaveLocation)
                Response.Write(<center>Thank you for your submission.</center>)
                Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
                conn = New SqlConnection(connection)
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)


            End Try
        Else
            Response.Write(<center>Please select a file to upload.</center>)
        End If

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class

here is the sql bulk insert (please note that I won't need to create a new table as the table is already inserted. This sql query has been tested for the purposes of what I'm trying to do and works, so I need this query inserted into the .vb code behind)

bulk insert dialerresults
from '\\MSBWEB3\data\test.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Select * 
from dialerresults
go

I'm assuming that this goes after the TRY statement but as I'm still learning asp.net, I have no idea how to call the command. Any assistance would be helpful.

Thank you

Doug

I guess your bulk insert code should go after this

conn = New SqlConnection(connection)..

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.