Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir...
How can i find and insert the address of the uploaded file in database


C#
protected void btnInsert_Click(object sender, EventArgs e)
    {
        cmd.CommandText = "sp_Admininsert";
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add("@Title", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Author", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Address", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Url", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Reference", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Abstract", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Categories", SqlDbType.NVarChar);
        cmd.Parameters.Add("@Year", SqlDbType.NVarChar);


        cmd.Parameters["@Title"].Value = txtTitle.Text.Trim();
        cmd.Parameters["@Author"].Value = TextAuthor.Text.Trim();
        cmd.Parameters["@Address"].Value = TextAddress.Text.Trim();
        cmd.Parameters["@Reference"].Value = TextReference.Text.Trim();
        cmd.Parameters["@Abstract"].Value = TextAbstract.Text.Trim();
        cmd.Parameters["@Categories"].Value = TextCategories.Text.Trim();
        cmd.Parameters["@Year"].Value = TextYear.Text.Trim();
        cmd.Parameters["@Url"].Value =



        cmd.Parameters.Add("@retid", SqlDbType.Int);
        cmd.Parameters["@retid"].Direction = ParameterDirection.ReturnValue;

        con.Open();
        cmd.ExecuteNonQuery();
        string id = cmd.Parameters["@retid"].Value.ToString();
        cmd.Parameters.Clear();
        con.Close();

        fupPic.SaveAs(MapPath("~/PDF/" + TextYear.Text.Trim() + "/" + id + Path.GetExtension(fupPic.FileName)));
    }
}
Posted
Comments
Sushil Mate 1-Aug-13 6:48am    
MapPath("~/PDF/" + TextYear.Text.Trim() + "/" + id + Path.GetExtension(fupPic.FileName))
Dholakiya Ankit 1-Aug-13 8:00am    
5ed
Sushil Mate 1-Aug-13 23:31pm    
:)
Swinky_Talwar 1-Aug-13 6:53am    
Shows this Error

Error 1 Cannot use local variable 'id' before it is declared
Sushil Mate 1-Aug-13 7:08am    
Use ParameterDirection.ReturnValue instead of id

Hi,
My advice is don't store whole url in db. Just store uploaded file name. Because next time when you retrieve it from db. you can use Server.MapPath("~/PDF/"+yourfilenamefromdb);

If you are uploading multiple type files, then store it with related path.
e.g., In your example store it with "PDF/filename".

Thanks.
 
Share this answer
 
v2
If you are using different folders to save the files, then you should append the Folder Name before the File Name and store that in DataBase as the File Url.

Else just store the File Name.

While showing the file, you can get the File Name from Database (Url field) and then as you know the Folder Name, so you just need to append that with the Folder Name. Then Map the path ussing Server.MapPath. That's it.
 
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