Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Database connection string attached to an upload onclick

Status
Not open for further replies.

dougancil

IS-IT--Management
Mar 31, 2009
44
US
I have a simple browse and upload web page that users can upload a .csv file to a server and then a bulk insert is ran after the user clicks the "upload" button.

Here is my default.aspx code:

@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
<html xmlns="<head runat="server">
<title>Upload Page</title>
<style type="text/css">
#form1
{
height: 128px;
}
#File1
{
top: 66px;
}
#Submit1
{
top: 103px;
left: 591px;
}
#TextArea1
{
z-index: 1;
left: 303px;
top: 45px;
position: absolute;
}
</style>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Submit1_onclick() {

}

// ]]>
</script>
</head>
<body>
<form id="form1" enctype="multipart/form-data" runat="server">
<br />
                                                                                                                                     
<br />
<input type="File" id="File1" name="File1" runat="server"
style="position: absolute; left: 512px;" />                                                                            
<br />
<input type="Submit" id="Submit1" value="Upload" runat="server"
style="position: absolute; height: 30px;"
onclick="return Submit1_onclick()" />                                                                                                         </p>
</form>
</body>
</html>

Here is my .vb code:

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
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)
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

I want to do a bulk insert without a stored procedure and here is the bulk insert (which has been tested and works)

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

Select *
from dialerresults
go


What I need to know is where is the best place to put the onclick event handler? Is it in the .vb script? Is the database connection string better in the web.config file then the onclick would call that event with the SQL script written into the page? I want to make this as efficient as possible and I've never made a page like this before.

Any help would be greatly appreciated.

Thank you

Doug
 
What I need to know is where is the best place to put the onclick event handler? Is it in the .vb script?
As you have done in your code, any code should go into your code behind page. This is actual VB not VBScript, there is a big difference.

The web.conifg should be put in the web.config.

From your button click event, within the try, you can call another sub or function that contains your bulk insert code.
 
JBenson

First let me apologize for not stating that correctly. It is vb and not vb script.
Secondly, so the connection string to the database should be in the web.config file and within the try I should have the sql query? So if I understand it should look a little like this?:

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
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
I'm not sure what to enter here as far as calling the connection string for the SQL server

bulk insert dialerresults
from 'C:\Inetpub\WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

Select *
from dialerresults
go
End Try
 
You have to first enter the connection string in the web.config, then you can retrive it like this:

Dim myConnString As String = ConfigurationSettings.AppSettings("TheNameInTheWebConfig")
 
J,

So after I call the connection string in the .vb file, I can add the rest of the bulk insert in the TRY correct?
 
Yes, you will have to set up a connection and command object etc.

There are many examples on-line and in the vs help files.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top