Hi gokuldas,
I have created a sample please take its reference.
Database
This example makes use of a table named tblFiles whose schema is defined as follows.
You can download the database table SQL by clicking the download link below.
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
//dataGridView1.AllowUserToAddRows = false;
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Columns.Add("Path");
dt.Rows.Add(1, "", "");
dt.Rows.Add(2, "", "");
dataGridView1.DataSource = dt;
DataGridViewButtonColumn uploadButton = new DataGridViewButtonColumn();
uploadButton.HeaderText = "Upload";
uploadButton.Text = "Browse...";
uploadButton.UseColumnTextForButtonValue = true;
uploadButton.Width = 75;
dataGridView1.Columns.Add(uploadButton);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
dataGridView1.Rows[i].Cells[0].ReadOnly = true;
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 3)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
dataGridView1[1, e.RowIndex].Value = Path.GetFileName(dialog.FileName);
dataGridView1[2, e.RowIndex].Value = dialog.FileName;
}
}
}
private void btnSubmit_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells["Name"].Value != null)
{
if (!string.IsNullOrEmpty(row.Cells["Name"].Value.ToString()))
{
string name = row.Cells["Name"].Value.ToString();
string path = row.Cells["Path"].Value.ToString();
byte[] bytes = File.ReadAllBytes(path);
string contentType = "";
//Set the contenttype based on File Extension.
switch (Path.GetExtension(name))
{
case ".jpg":
contentType = "image/jpeg";
break;
case ".png":
contentType = "image/png";
break;
case ".gif":
contentType = "image/gif";
break;
case ".bmp":
contentType = "image/bmp";
break;
}
InsertData(name, contentType, bytes);
}
}
}
}
private void InsertData(string name, string type, byte[] bytes)
{
string constr = @"Data Source=.;Initial Catalog=test;uid=sa;pwd=pass@123;";
using (SqlConnection conn = new SqlConnection(constr))
{
string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(name));
cmd.Parameters.AddWithValue("@ContentType", type);
cmd.Parameters.AddWithValue("@Data", bytes);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Id")
dt.Columns.Add("Name")
dt.Columns.Add("Path")
dt.Rows.Add(1, "", "")
dt.Rows.Add(2, "", "")
dataGridView1.DataSource = dt
Dim uploadButton As DataGridViewButtonColumn = New DataGridViewButtonColumn()
uploadButton.HeaderText = "Upload"
uploadButton.Text = "Browse..."
uploadButton.UseColumnTextForButtonValue = True
uploadButton.Width = 75
dataGridView1.Columns.Add(uploadButton)
For i As Integer = 0 To dataGridView1.Rows.Count - 1
dataGridView1.Rows(i).Cells(0).[ReadOnly] = True
Next
End Sub
Private Sub dataGridView1_CellClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dataGridView1.CellClick
If e.ColumnIndex = 3 Then
Dim dialog As OpenFileDialog = New OpenFileDialog()
If dialog.ShowDialog() = DialogResult.OK Then
dataGridView1(1, e.RowIndex).Value = Path.GetFileName(dialog.FileName)
dataGridView1(2, e.RowIndex).Value = dialog.FileName
End If
End If
End Sub
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
For Each row As DataGridViewRow In dataGridView1.Rows
If row.Cells("Name").Value IsNot Nothing Then
If Not String.IsNullOrEmpty(row.Cells("Name").Value.ToString()) Then
Dim name As String = row.Cells("Name").Value.ToString()
Dim path As String = row.Cells("Path").Value.ToString()
Dim bytes As Byte() = File.ReadAllBytes(path)
Dim contentType As String = ""
Select Case System.IO.Path.GetExtension(name)
Case ".jpg"
contentType = "image/jpeg"
Case ".png"
contentType = "image/png"
Case ".gif"
contentType = "image/gif"
Case ".bmp"
contentType = "image/bmp"
End Select
InsertData(name, contentType, bytes)
End If
End If
Next
End Sub
Private Sub InsertData(ByVal name As String, ByVal type As String, ByVal bytes As Byte())
Dim constr As String = "Data Source=.;Initial Catalog=test;uid=sa;pwd=pass@123;"
Using conn As SqlConnection = New SqlConnection(constr)
Dim sql As String = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)"
Using cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@Name", Path.GetFileName(name))
cmd.Parameters.AddWithValue("@ContentType", type)
cmd.Parameters.AddWithValue("@Data", bytes)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
End Using
End Using
End Sub