Can anyone correct my code?
I will like to add a url to my database and upload my image to web server.
txtAddTitle will become my title name
txtFileName allow user to type in image name that will upload to webserver and at the meantime will insert into database as url references
value for @.chapterid will get from drop down list(ddlLesson) and the index value will store in database.
now i am able to upload my images with the file name that i have given to web server, but I am unable to insert data to my database.
Sub DoUpload(ByVal Sender As Object, ByVal e As System.EventArgs)
Dim sPath As String
Dim sFile As String
Dim sFullPath As String
Dim sSplit() As String
Dim sPathFriendly As String
'Upload to same path as script. Internet Anonymous User must have write permissions
sPath = Server.MapPath("../Tutorial")
'sPath = Server.MapPath(".")
If Right(sPath, 1) <> "\" Then
sPathFriendly = sPath 'Friendly path name for display
sPath = sPath & "\"
Else
sPathFriendly = Left(sPath, Len(sPath) - 1)
End If
'Save as same file name being posted
'The code below resolves the file name
'(removes path info)
sFile = txtFileName.Text
'sFile = txtUpload.PostedFile.FileName
sSplit = Split(sFile, "\")
sFile = sSplit(UBound(sSplit))
sFullPath = sPath & sFile
Try
txtUpload.PostedFile.SaveAs(sFullPath)
lblResults.Text = "<br>Upload of File " & sFile & " to " & sPathFriendly & " succeeded"
Catch Ex As Exception
lblResults.Text = "<br>Upload of File " & sFile & " to " & sPathFriendly & " failed for the following reason: " & Ex.Message
Finally
lblResults.Font.Bold = True
lblResults.Visible = True
End Try
Dim mycommand As SqlCommand
Dim myConnection As SqlConnection
Message.InnerHtml = ""
'to all the valur to database
If IsValid Then
myConnection = New SqlConnection("Server=localhost;UID=sa;pwd=;database=u")
mycommand = New SqlCommand("INSERT INTO t_linkTitle(link_chapterid,link_name,link_url) VALUES (@.chapterid,@.titleName,@.titleUrl)", myConnection)
mycommand.Parameters.Add("@.chapterid", SqlDbType.VarChar, 50).Value = ddlLesson.SelectedItem.Value
mycommand.Parameters.Add("@.titleName", SqlDbType.VarChar, 50).Value = txtAddTitle.Text
mycommand.Parameters.Add("@.titleUrl", SqlDbType.VarChar, 100).Value = txtFileName.Text
mycommand.Connection.Open()
msgErrorTitle.Style("color") = "OrangeRed"
Try
mycommand.ExecuteNonQuery()
msgErrorTitle.InnerHtml = "New title <b>" + txtAddTitle.Text + "</b> Added to " + "<b>" + ddlLesson.SelectedItem.Text + "</b><br>" + mycommand.ToString()
Catch Exp As SqlException
If Exp.Number = 2627 Then
msgErrorTitle.InnerHtml = "ERROR: Title already exists. Please use another title"
Else
msgErrorTitle.InnerHtml = "ERROR: Could not add record, please ensure the fields are correctly filled out"
End If
End Try
mycommand.Connection.Close()
End If
LoadTitle()
End Sub
So what sql server error number are you getting when you attempt an insert?|||
emulti wrote:
So what sql server error number are you getting when you attempt an insert?
It show this"ERROR: Could not add record, please ensure the fields are correctly filled out"
No comments:
Post a Comment