Click here to Skip to main content
15,888,031 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am creating a small scale Admin Panel(desktop app) for managing videos(Insert,Update,Delete and Play).So far I am able to insert a single video file inside the database via OpenFileDialog.

Click Here to See

Upload Button Code
C#
   private void metroButton4_Click(object sender, System.EventArgs e)
   {
       openFileDialog1.Filter = "Mp4|*.mp4";
       if (openFileDialog1.ShowDialog() == DialogResult.OK)
       {
           try
           {
//Global Variables => string path = "" ,fileName =""; byte[] videoData = null;
               path = openFileDialog1.FileName;
               fileName = openFileDialog1.SafeFileName;
               metroTextBox1.Text = fileName;
               metroButton4.Text = "Change";
               videoData = File.ReadAllBytes(path);
           }
           catch (Exception ex)
           {
               MetroMessageBox.Show(this,ex.Message);
           }
       }
   }


Insert Button Code
C#
    private void metroButton2_Click(object sender, System.EventArgs e)
    {
        if (videoData != null && metroTextBox1.Text == "")
        {
         MetroMessageBox.Show(this, "Please enter a name for this video", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        if (videoData == null)
        {
         MetroMessageBox.Show(this, "Please ensure that you have choosen a video to upload", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        if (videoData != null && metroTextBox1.Text != "")
        {
            try
            {
              cmd = new SqlCommand("INSERT INTO VideoRecords(Name,Video) VALUES(@name,@video) ", mcObject.getConnection());
                cmd.Parameters.AddWithValue("@name", metroTextBox1.Text);
                cmd.Parameters.AddWithValue("@video", videoData);
                mcObject.openCon();
                cmd.ExecuteNonQuery();
                mcObject.closeCon();
MetroMessageBox.Show(this, metroTextBox1.Text + " inserted successfully!", "Insertion Successful", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                metroButton1_Click(sender, e);   //Refreshes the form
            }
            catch (Exception ex)
            {
    MetroMessageBox.Show(this, ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
       }
    }

Now I need to enhance this code to upload multiple files and insert them into the database.Your assistance would be quite appreciable.

What I have tried:

1. Set the MultiSelect property of OpenFileDialog to true.
2. Set the MultiLine property of metroTextBox1 to true.
3. Modify code for upload button
C#
          if (openFileDialog1.ShowDialog() == DialogResult.OK)
          {
              foreach (string file in openFileDialog1.FileNames)
              {
                  try
                  {
                      path = file;
                      fileName += openFileDialog1.SafeFileName;
                      metroTextBox1.Text += path;
                      metroButton4.Text = "Change";
                      videoData = File.ReadAllBytes(path);
                  }
                  catch (Exception ex)
                  {
                      MetroMessageBox.Show(this, ex.Message);
                  }
}


Result
No luck.

Goal Required
1.Get separate paths for each video file and display in the textbox along with extension(.mp4).
2.Store the video content in the videoData variable for each video.
3.Finally insert these uploaded files in the database.
Posted
Updated 10-Jul-16 3:56am
v2
Comments
Patrice T 10-Jul-16 11:48am    
Short answer: Don't store videos in a database, this is an abuse.
Zujaj 10-Jul-16 12:48pm    
Excuse me?
Michael_Davies 10-Jul-16 14:54pm    
Not a good idea storing video files in a database, mainly due to size, usual practice is to keep a link to the video location.

However:

You say no luck, in what way no luck, what is going wrong and have you used the debugger to verify the content of path at this point;

videoData = File.ReadAllBytes(path);
Zujaj 10-Jul-16 14:57pm    
I don't understand why you are focusing me not to store the video file content in the database, I am just saving it as varbinary(max) and storing it so that i may retrieve the video to play it in axWindowsMediaplayer and yes i have debugged at this point, it allows me to store the binary data inside the byte array.
Michael_Davies 10-Jul-16 15:52pm    
So what is the value of path? and where is the problem.

No luck is not a good guide to help us to help you solve the issue, it explains nothing to anyone. If you have debugged then what is the problem. Remember we We cannot see your PC.

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