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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can someone check this code please

Status
Not open for further replies.

ozzroo

Technical User
Feb 28, 2003
182
0
0
AU
Hi

Could someone please check this code. It works fine other than its adding the new registration twice and also if a user doesnt supply a file to upload it comes up with an error along the lines that i was looking for the insert fields in the code

Thanks in advance

Code:
<script runat="server">
	
Sub doUploadCV
Dim strFileExtension As String
Dim strFileType As String
Dim intFileLen As Integer
Dim objStream As Stream
Dim conUploadCV As SqlConnection
Dim strInsert As String
Dim cmdInsert As SqlCommand

If Not IsNothing(UploadedFile.PostedFile ) Then
'Determine File Type
strFileExtension = RIGHT( UploadedFile.PostedFile.FileName, 4 )
SELECT Case strFileExtension.ToLower
CASE ".doc"
	strFileType = "doc"
CASE ".pdf"
	strFileType = "pdf"
End Select

'Grab the contents of uploaded file
intFileLen = UploadedFile.PostedFile.ContentLength
Dim arrFile( intFileLen ) As Byte
objStream = UploadedFile.PostedFile.InputStream
objStream.Read( arrFile, 0, intFileLen )

'Add uploaded file to database
conUploadCV = New SqlConnection("Server=xxx;UID=xxx;PWD=xxx;database=xxx" )
strInsert = "Insert dbo.CV (Title, AssociateRef, DocumentType, DocumentData ) Values (@Title, @AssociateRef, @filetype, @DocumentData )"
cmdInsert = New SqlCommand( strInsert, conUploadCV )
cmdInsert.Parameters.Add("@Title", Forename.text )
cmdInsert.Parameters.Add("@AssociateRef", AssociateRef)
cmdInsert.Parameters.Add("@fileType", strFileType )
cmdInsert.Parameters.Add("@DocumentData", arrFile )
conUploadCV.Open()
cmdInsert.ExecuteNonQuery()
conUploadCV.Close()
End If
End Sub
	
	Sub Page_Load
	btnSubmit.enabled = false
	End Sub
	
	Private Sub RegisterButtonEnabled (sender As Object, e As System.EventArgs)
	If TermsBox.Checked = True Then
	btnSubmit.enabled = True
	Else
	btnSubmit.enabled = false
	End If	
	End Sub
	
    Sub btnReset_Click(s As Object, e As EventArgs)
    Title.SelectedItem.Text = ""
	Forename.Text = ""
	Surname.Text = ""
	AddressLine1.Text = ""
	AddressLine2.Text = ""
	AddressLine3.Text = ""
	AddressLine4.Text = ""
	County.SelectedItem.Text = ""
	Postcode.Text = ""
	Region.SelectedItem.Text = ""
	Company.Text = ""
	Email_Address.Text = ""
	HomePhone.Text = ""
	BusinessPhone.Text = ""
	MobilePhone.Text = ""
	
    End Sub
	   
    
    Dim Password As String = Membership.GeneratePassword(7, 1)
	Dim conn As SqlConnection
	Dim cmd As SqlCommand
	Dim strconnection, strsqlinsert As String
	Dim AssociateRef As String
	
	Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
  	strconnection = "Server=xxx;UID=sa;PWD=xxxx;database=xxx"
  	strsqlinsert = "Insert into dbo.Associates ( "
  	strsqlinsert + = "Title, Forename, Surname, AddressLine1, AddressLine2, AddressLine3, AddressLine4, County, Postcode, Region, Company, Email, HomePhone, BusinessPhone, MobilePhone, Password"
  	strsqlinsert + = ")"
  	strsqlinsert + = " values ("
  	strsqlinsert + = "@Title, @Forename, @Surname, @AddressLine1, @AddressLine2, @AddressLine3, @AddressLine4, @County, @Postcode, @Region, @Company, @EmailAddress, @HomePhone, @BusinessPhone, @MobilePhone, @Password"
  	strsqlinsert + = ")"
  	strsqlinsert + = "; SELECT SCOPE_IDENTITY() ; "
  	conn = New SqlConnection(strconnection)
  	cmd = New SqlCommand(strsqlinsert, conn)
  	cmd.Parameters.Add( "@Title", Title.SelectedItem.Text)
	cmd.Parameters.Add( "@Forename", Forename.Text)
    cmd.Parameters.Add( "@Surname", Surname.Text)
	cmd.Parameters.Add( "@AddressLine1", AddressLine1.Text)
	cmd.Parameters.Add( "@AddressLine2", AddressLine2.Text)
	cmd.Parameters.Add( "@AddressLine3", AddressLine3.Text)
	cmd.Parameters.Add( "@AddressLine4", AddressLine4.Text)
	cmd.Parameters.Add( "@County", County.SelectedItem.Text)
	cmd.Parameters.Add( "@Postcode", Postcode.Text.toUpper())
	cmd.Parameters.Add( "@Region", Region.SelectedItem.Text)
	cmd.Parameters.Add( "@Company", Company.Text)
	cmd.Parameters.Add( "@EmailAddress", Email_Address.Text)
	cmd.Parameters.Add( "@HomePhone", HomePhone.Text)
	cmd.Parameters.Add( "@BusinessPhone", BusinessPhone.Text)
	cmd.Parameters.Add( "@MobilePhone", MobilePhone.Text)
	cmd.Parameters.Add( "@Password", Password)
  	cmd.Connection.Open()
  	AssociateRef = cmd.ExecuteScalar
  	cmd.Connection.Close()
	doUploadCV
	panelSendEmail.Visible = false
	panelMailSent.Visible = true
	Response.AddHeader("Refresh","5;url=index.html")
End Sub
</script>
 
You should you the code behind page for your code instead of incorporating the code in the HTML of your page. That is classic ASP style coding. If you use the code behind, it will be easier to debug, which is what you need to do here to find what is wrong with your code.
 
Hi,

Quick fix might be to put a Try Catch block around doUploadCV

Try
doUploadCV
Catch ex As Exception
End Try

Also, change Response.AddHeader("Refresh","5;url=index.html")
to
Response.Redirect("index.html")

HTH, Jon

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top