Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am making a website in asp.net.I've made a file upload control to upload doc,txt files. Now I want to save that uploaded file in sql server database table. How to do it???
I am writing the code in C#.
Plz help !
Thank u
Posted

1 solution

Create an Image field in the database - it needs to be image as the size limits are too small on the other datatypes.
At the same time, create a nvarchar(Max) field for the filename.
Handle the button click event of the Upload control.
In the handler, the upload control has two properties you are interested in: FileName (which is just the name, no path information is ever available) and FileBytes which contains the actual file content.
This method does more than you need, but it gives you the idea:

C#
/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Check version - if the file exists in the DB already, then increase version number.
                using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
                    {
                    ver.Parameters.AddWithValue("@FN", filename);
                    object o = ver.ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                        {
                        // Exists already.
                        version = (int) o + 1;
                        }
                    }
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }
 
Share this answer
 
Comments
angel 2 24-Oct-11 8:08am    
Hey thank you...
What code to write under btnSubmit_Click?
OriginalGriff 24-Oct-11 8:25am    
Well, you could try calling the SaveUpload method...:laugh:
angel 2 24-Oct-11 8:32am    
lol...well I know that...!!!
But what to write in place of (FileUpload fl)?
OriginalGriff 24-Oct-11 8:44am    
Oooo! Oooo! Me Sir! Me Sir! I know it! :laugh:
You could always try the name of your FileUpload control? You know, the one you are handling the click event for? Or even the sender parameter, cast to a FileUpload control...
angel 2 24-Oct-11 8:48am    
OMG Its Working!!!
Thank u so much...Ive been trying this from the last 2 days :)

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