You must Sign In to post a response.
  • Category: ASP.NET

    Export Binry Data from Database to gridview

    Hello Friends

    I have binary data in database which is in .xls or .xlsx format.

    Now i have to import data in gridview or in dataset through code

    please remind data is in binary format as it has been uploaded in sql table

  • #768890

    To Export data from excel to SQL you have to connect with OLEDB Provider and insert the data into database like below

    private void InsertIntoDB()
    int count=0;

    SqlConnection con = new SqlConnection("DataBase=ENGSOFT;User Id=sa;Password=P@ssword9");
    SqlCommand cmd;
    DataTable dt = Session["Table"] as DataTable;

    if (dt.Rows.Count > 0)
    //Read each and everyrow of datatable and insert each and every row to SQL DataBase.
    foreach (DataRow dr in dt.Rows)
    string Ename = dr["ENAME"].ToString();
    string Job = dr["JOB"].ToString();
    string MGR = dr["MGR"].ToString();
    string Sal = dr["SAL"].ToString();
    string comm = dr["COMM"].ToString();

    //normal insert query.

    cmd = new SqlCommand("INSERT INTO EMP(ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES(@ename,@job,@mgr,@hiredate,@sal,@comm,@deptno)", con);
    cmd.Parameters.AddWithValue("@ename", Ename);
    cmd.Parameters.AddWithValue("@job", Job);
    cmd.Parameters.AddWithValue("@mgr", MGR);
    cmd.Parameters.AddWithValue("@hiredate", DateTime.Now);
    cmd.Parameters.AddWithValue("@sal", Sal);
    cmd.Parameters.AddWithValue("@comm", comm);
    cmd.Parameters.AddWithValue("@deptno", "1");

    int n = cmd.ExecuteNonQuery();
    count = count + n;

    catch (Exception ex)
    lblResult.Text = count + " Records Inserted Successfully...!!!";

    for binary format have you look at the below link may be this might be helpful to you.

    Hope this helps you...

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #768896
    Hai Anita,
    First you need to get the binary data and then convert it and then you can use it.
    Below is the link which will give you some idea of how to do that:

    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #768935
    You can use this code snippet to Export Binary Data from Database to GridView in your source code
     if (cell.Value.GetType() == typeof(Bitmap))
    string imagString = "bitmap1.bmp";
    Excel.Range oRange = (Excel.Range)xlWorkSheet.Cells[i + 1, j + 1];
    float Left =(float) ((double)oRange.Left);
    float Top =(float) ((double)oRange.Top);
    const float ImageSize=32;
    xlWorkSheet1.Shapes.AddPicture(imagString, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
    oRange.RowHeight = ImageSize + 2;
    xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;

  • Sign In to post your comments