Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,I have used this code to upload file, but I cannot get the result in Database.
C#
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;database=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:
C#
<asp:ScriptManager ID="ScriptManager1" runat="server">  
     </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <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" />






Can any one help me?
Posted
Updated 4-Dec-13 23:30pm
v4
Comments
asbis 5-Dec-13 5:40am    
what error you are getting while uploading image?
Member 10419145 5-Dec-13 6:29am    
I am not getting any error at the same time image is not stored in folder and data is not stored in DB
Debugging is a very good idea.

I have posted the code of C# and can u try the same way in VB

C#
ScriptManager _scriptMan = ScriptManager.GetCurrent(this);
            _scriptMan.AsyncPostBackTimeout = 36000;
            ScriptManager1.RegisterPostBackControl(Upload_button);
 
Share this answer
 
Comments
Member 10419145 5-Dec-13 6:35am    
Where I should include this code?
thatraja 5-Dec-13 7:31am    
In Page_Load()
Member 10419145 5-Dec-13 8:02am    
Thanks for your help. But by doing so the whole page is being refreshed, I donot want that to happen.
Ganesh Raja 5-Dec-13 8:20am    
When ever u r using the page response's like upload, download, redirect things in update panel surly it will fail so that i have posted the code will help to disable the update panel for given time in(timeout).

OR

try another method to user the <Triggers>
.........................................

<UpdatePanel runat="server">
<contenttemplate>
...
<asp:Button runat="server" ID="myButton" />
...

<Triggers>
<asp:PostBackTrigger ControlID="myButton" />
</Triggers>
Long time back, I have faced similar thing & solved using Triggers
Using FileUpload Control inside ASP.Net AJAX UpdatePanel Control[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900