By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

code to use fileupload inside update panel

P: 3
Hi all,

I have used this code to upload file, but I cannot get the result in Database.


Protected Sub btnsave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnsave.Click
If FileUpload1.PostedFile IsNot Nothing Then

Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)



'Save files to disk

FileUpload1.SaveAs(Server.MapPath("~/images/" & FileName))

Dim CN As New SqlConnection("Server=pc5268;uid=sa;pwd=klo;databa se=MSK2DB")

Dim qry As String = "insert into tblFiles (QID, Question, QDetail, QFilePath, QFileName) values(@QID, @Question, @QDetail, @QFilePath, @QFileName)"


Dim SqlCom As New SqlCommand(qry, CN)

SqlCom.Parameters.Add(New SqlParameter("@QID", DirectCast(TextBox1.Text, Object)))
SqlCom.Parameters.Add(New SqlParameter("@Question", DirectCast(TextBox2.Text, Object)))
SqlCom.Parameters.Add(New SqlParameter("@QDetail", DirectCast(TextBox3.Text, Object)))
SqlCom.Parameters.AddWithValue("@QFilePath", "~/images/" & FileName)
SqlCom.Parameters.AddWithValue("@QFileName", FileName)
SqlCom.CommandType = CommandType.Text

SqlCom.Connection = CN

Try
CN.Open()
SqlCom.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.Message)
Finally
CN.Close()
CN.Dispose()
End Try

End If
End Sub

Source code:

<asp:UpdatePanel>

<ContentTemplate>

<asp:TextBox ID="TextBox1" runat="server">

<asp:TextBox ID="TextBox2" runat="server">

<asp:TextBox ID="TextBox3" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />

<asp:Button ID="btnsave" runat="server" Text="Save All" Width="95px" />

</ContentTemplate>
</asp:UpdatePanel>


Can any one help me? .
Dec 5 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.