Fileupload and Database and url's

Last post 01-22-2007 12:27 AM by Jessica Cao - MSFT. 2 replies.

Sort Posts:

  • Fileupload and Database and url's

    01-20-2007, 4:47 PM
    • Loading...
    • RuanRossouw
    • Joined on 12-09-2006, 10:22 AM
    • South Africa
    • Posts 10

    Hi,

     

    I am a novice programmer. Doing this merely for fun as I am actually in the finances sector.

     

    I am hope you guys/ladies can help me. I have a fileupload control on one of my pages to upload pictures. It works perfectly. I do want to save some of the data, like the title and description, into a database with the url of the picture on the webserver. What I would like to know is how can I link my submit/upload button for the pictures with the insert button of the database in formview i.e. the submit/upload button replaces the insert button/link in formview. And also how do I go about that when I press the upload button the url will be automatically saved into the database field piture_url?

     

    Thank you in advance for your help.

     

    Kind Regards,

     

    Ruan Rossouw 

  • Re: Fileupload and Database and url's

    01-22-2007, 12:24 AM
    Hi,
    You can do it reversely, that's to say.
    Upload the file to the server when the insert button is clicked. So you can take advantage of invoking the corresponding insert action automatically in datasource object.

    For instance, add the following code to the FormVIew_ItemInserting event:

    protected void fvOrder_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
    FileUpload1.PostedFile.SaveAs("savefilepath");
    SqlDataSource1.InsertParameters["fileURL"] = "the url of the image";
    }
    Hope it helps.

  • Re: Fileupload and Database and url's

    01-22-2007, 12:27 AM
    Answer

    hi,

    here is the code example

     protected void Upload_Click(object sender, EventArgs e)
        {
            string picUrl;
            if (FileUpload1.HasFile)
            {
                picUrl = FileUpload1.PostedFile.FileName;
                FileUpload1.SaveAs("c:\\"+FileUpload1.FileName);
                insertUrl(picUrl);

            }

        }
        void insertUrl(string url)
        {
            string co = "Data Source=localhost\\sqlexpress;Initial Catalog=pubs;Integrated Security=True";

            System.Data.SqlClient.SqlConnection sqlconnect = new System.Data.SqlClient.SqlConnection(co);


            System.Data.SqlClient.SqlCommand sqlcommand = new System.Data.SqlClient.SqlCommand();
            sqlcommand.CommandText = "insert into test values(@project,@picUrl)";
            sqlcommand.Connection = sqlconnect;
     

            System.Data.SqlClient.SqlParameter p1 = new System.Data.SqlClient.SqlParameter();
            p1.ParameterName = "@project";
            p1.Value = "1";
            p1.SqlDbType = System.Data.SqlDbType.VarChar;
            sqlcommand.Parameters.Add(p1);

            System.Data.SqlClient.SqlParameter p2 = new System.Data.SqlClient.SqlParameter();
            p2.ParameterName = "@picUrl";
            p2.Value = url;
            p2.SqlDbType = System.Data.SqlDbType.VarChar;
            sqlcommand.Parameters.Add(p2);

            sqlconnect.Open();
            sqlcommand.ExecuteNonQuery();
            sqlconnect.Close();
        }

    Jessica Cao
    Sincerely,
    Microsoft Online Community Support


    “Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter