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
private void metroButton4_Click(object sender, System.EventArgs e)
{
openFileDialog1.Filter = "Mp4|*.mp4";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
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
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);
}
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
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.