Insert name of file upload into database

Thread is closed for posting
7 posts, 1 answers
  1. Marta
    Marta avatar
    31 posts
    Member since:
    Apr 2009

    Posted 06 May 2009 Link to this post

    Hi, I'm having a problem with the update control. I have some textfields that the user fills in to create a new content, and one of them is an upload field. I have a submit button where I have my insert query and that is working fine, but now I have the code for the upload as well. I would like to be able to use the variable "imagem" as a parameter for my query, but the problem is that I think that the insert is being executed before that variable has the value in it from the upload, and also that the variable is not available inside the procedure. I also tried using "targetFolder" instead of "imagem" but it also doesn't work...
    Help please?

    namespace Telerik.Web.Examples.Upload.UsingTargetFolder
    {

        public partial class CriarConteudo : System.Web.UI.Page
        {
            string targetFolder = string.Empty;
            protected void Page_Load(object sender, EventArgs e)
            {
               
            }

            protected void RadUploadImagem_FileExists(object sender, Telerik.Web.UI.Upload.UploadedFileEventArgs e)
            {
                UploadedFile imagem = e.UploadedFile;
                string targetFolder = Server.MapPath(RadUploadImagem.TargetFolder);
                string targetFileName = Path.Combine(targetFolder, imagem.GetNameWithoutExtension() + imagem.GetExtension());
                imagem.SaveAs(targetFileName);
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                SqlConnection conn1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["lms_bibliotecaConnectionString1"].ConnectionString);
                SqlCommand cmd1 = new SqlCommand("SELECT IDTipo FROM NL_LMS_BIB_tipos WHERE NL_LMS_BIB_tipos.Tipo = @Param1", conn1);
                cmd1.CommandType = CommandType.Text;
                cmd1.Parameters.Add(new SqlParameter("@Param1", RadioBtnListTipo.SelectedValue));
                Guid idtipo;

                    conn1.Open();
                    cmd1.ExecuteNonQuery();
                    SqlDataReader objDR;
                    objDR = cmd1.ExecuteReader();
                    objDR.Read();
                    idtipo = (Guid)objDR.GetValue(0);

                    conn1.Close();
                    cmd1.Dispose();
                    conn1.Dispose();

                SqlConnection conn2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["lms_bibliotecaConnectionString1"].ConnectionString);
                SqlCommand cmd2 = new SqlCommand("SELECT IDTema FROM NL_LMS_BIB_temas WHERE NL_LMS_BIB_temas.Tema = @Param1", conn2);
                cmd2.CommandType = CommandType.Text;
                cmd2.Parameters.Add(new SqlParameter("@Param1", RadComboTemas.SelectedValue));
                Guid idtema;

                    conn2.Open();
                    cmd2.ExecuteNonQuery();
                    SqlDataReader objDR2;
                    objDR2 = cmd2.ExecuteReader();
                    objDR2.Read();
                    idtema = (Guid)objDR2.GetValue(0);

                    conn1.Close();
                    cmd1.Dispose();
                    conn1.Dispose();

                SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["lms_bibliotecaConnectionString1"].ConnectionString);
                string insertcmd = "INSERT INTO NL_LMS_BIB_conteudos (IDConteudo, Titulo, Resumo, Notas, IDTipo, IDTema, DataPublicacao, Imagem) VALUES (NEWID(), @titulo, @resumo, @notas, @idtipo, @idtema, @datahora, @imagem)";
                SqlCommand cmd = new SqlCommand(insertcmd, conn);
                cmd.Parameters.Add(new SqlParameter("@titulo", RadTxtBoxTitulo.Text));
                cmd.Parameters.Add(new SqlParameter("@resumo", RadTxtBoxResumo.Text));
                cmd.Parameters.Add(new SqlParameter("@idtipo", idtipo));
                cmd.Parameters.Add(new SqlParameter("@idtema", idtema));
                cmd.Parameters.Add(new SqlParameter("@datahora", RadDateTimePicker.SelectedDate));
                cmd.Parameters.Add(new SqlParameter("@notas", RadTxtBoxNotas.Text));
                cmd.Parameters.Add(new SqlParameter("@imagem", targetFolder));

                    conn.Open();
                    cmd.ExecuteNonQuery();

                    conn.Close();
                    cmd.Dispose();
                    conn.Dispose();

            }
        }
    }



  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 06 May 2009 Link to this post

    You can access the RadUpload control in your "OnClick handler" directly.
    To retrive the file (if it is only one) just access it via idOfUploadControl.UploadedFiles[0]
    So your code could be something like
    UploadedFile imagem = idOfUploadControl.UploadedFiles[0];
    ...

    Regards

    Manfred
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marta
    Marta avatar
    31 posts
    Member since:
    Apr 2009

    Posted 07 May 2009 Link to this post

    Hi,

    thanks for your reply, but I don't really understand your suggestion, since I am new to asp .net.... I tried that line of code but it didn't work. Any other suggestions?

    Thanks a lot!
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 07 May 2009 Link to this post

    Sorry for that - I'll try to explain it better.
    First of all - what do you want to store in your database?

    Currently you set the variable targetFolder to the value of the Folder that you get from
    Server.MapPath(RadUploadImagem.TargetFolder);

    Here is your first error (sry I saw it just now).
    You have a class variable with this name:
        string targetFolder = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
    ...
    In your handler you define one more variable with local scope.
    So the code
     string targetFolder = Server.MapPath(RadUploadImagem.TargetFolder);
    does not set the variable you defined before - it creates a new one - which will be gone when you leave the function.

    Anyhow - I assume that you want to (why ever) store this path in you database.
    Simply change the code in the button handler:
    Current code:
     cmd.Parameters.Add(new SqlParameter("@imagem", targetFolder));
    Change this to
     cmd.Parameters.Add(new SqlParameter("@imagem", Server.MapPath(RadUploadImagem.TargetFolder)));

    This would do what I guess you wanted to do -- or in other words "would have done if your code would have worked".

    If this is NOT what you want - please explain WHAT you want to store in that database field so I can help you to make it going.

    Regards

    Manfred
  6. Marta
    Marta avatar
    31 posts
    Member since:
    Apr 2009

    Posted 07 May 2009 Link to this post

    Hi Manfred,

    Thanks again for your quick reply. That's almost it, the only thing is that what I want to save is not the name of the targetfolder (eg: images) but the name of the actual file uploaded (eg: image.png) but ONLY the name of the file with the extension and NOT the entire path. I tried several things but couldn't get it.

    Thanks,
    Marta
  7. Answer
    ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 07 May 2009 Link to this post

    OK,

    that's easy - again the same codeline:
    cmd.Parameters.Add(new SqlParameter("@imagem", targetFolder));
    Change it to:
    cmd.Parameters.Add(new SqlParameter("@imagem", RadUploadImagem.UploadedFiles[0].GetName()));

    This code does no error handling or things like this.
    UploadedFiles could be empty (if the user uploads a wrong (not allowed extension - to big..) file.
    You should check this with something like
    if(RadUploadImagem.UploadedFiles.Count!=1)    {
    //error handling here
    }
    else {
    cmd.Parameters.Add(new SqlParameter("@imagem", RadUploadImagem.UploadedFiles[0].GetName()));
    ....
    }

    Regards

    Manfred
  8. Marta
    Marta avatar
    31 posts
    Member since:
    Apr 2009

    Posted 07 May 2009 Link to this post

    Thank you Manfred, that was exactly what I needed!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017