Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9050711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:43:22+00:00 2026-06-16T12:43:22+00:00

I want to retrieve Binary Data from SQL Database with asp.net .but the data

  • 0

I want to retrieve Binary Data from SQL Database with asp.net .but the data that is shown in the output does not match with the data that i have inserted.
this is my code:

string EQuery = "SELECT * FROM Ph_Tbl_Contacts WHERE (Contact_ID =" + Contact_ID + ")";

DataSet DSs = new DataSet();
DataTable dt = new DataTable();
DataRow dr = dt.NewRow();

DSs = DB.ExecuteQueryData(EQuery);
dt = DSs.Tables[0];

// dr = dt.NewRow();
dr = dt.Rows[0];
byte[] pic;
byte[] raw = (byte[])dr["Contact_CardImage"];
//  Session[OpenDialog.STORED_IMAGE] = raw ;

and this is insert part:

byte[] IMAGEbYTE ;
IMAGEbYTE = (byte[])(Session["SessionImage"]);
string Query = "INSERT INTO Ph_Tbl_Contacts (Contact_Name, Contact_LName, " + 
               "Contact_Company, Contact_Email, Contact_Tel, " + 
               "Contact_Mobile,Contact_CardImage,Is_Public,User_ID,Save_Date)" + 
               "VALUES (N'" + Txt_Name.Text + "', N'" + Txt_LastName.Text + "', N'" + 
               Txt_CompanyName.Text + "', N'" + Txt_Mail.Text + "', N'" + 
               Txt_Telephone.Text + "', N'" + Txt_Mobile.Text + "','" + 
               IMAGEbYTE + "','" + CheckValue + "'," + 
               Session["User_ID"] + ", N'" + DateStr + "')";

DB.ExecuteQueryNoData(Query) ; 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:43:23+00:00Added an answer on June 16, 2026 at 12:43 pm

    Alright, let’s start with cleaning your code. The first thing is to fix your INSERT code, because right now it is vulnerable to SQL injection. You need to use parametrized queries:

    using (var conn = new SqlConnection("Your ConnectionString comes here"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText =
        @"INSERT INTO Ph_Tbl_Contacts 
              (Contact_Name, 
               Contact_LName, 
               Contact_Company, 
               Contact_Email, 
               Contact_Tel, 
               Contact_Mobile, 
               Contact_CardImage, 
               Is_Public, 
               User_ID, 
               Save_Date)
          VALUES 
              (@Contact_Name, 
               @Contact_LName, 
               @Contact_Company, 
               @Contact_Email, 
               @Contact_Tel, 
               @Contact_Mobile, 
               @Contact_CardImage, 
               @Is_Public, 
               @User_ID, 
               @Save_Date)
        ";
        cmd.Parameters.AddWithValue("@Contact_Name", Txt_Name.Text);
        cmd.Parameters.AddWithValue("@Contact_LName", Txt_LastName.Text);
        cmd.Parameters.AddWithValue("@Contact_Company", Txt_CompanyName.Text);
        cmd.Parameters.AddWithValue("@Contact_Email", Txt_Mail.Text);
        cmd.Parameters.AddWithValue("@Contact_Tel", Txt_Telephone.Text);
        cmd.Parameters.AddWithValue("@Contact_Mobile", Txt_Mobile.Text);
        cmd.Parameters.AddWithValue("@Contact_CardImage", IMAGEbYTE);
        cmd.Parameters.AddWithValue("@Is_Public", CheckValue);
        cmd.Parameters.AddWithValue("@User_ID", Session["User_ID"]);
        cmd.Parameters.AddWithValue("@Save_Date", DateStr); 
        cmd.ExecuteNonQuery();
    }
    

    Here’s it’s worth mentioning that if the Save_Date column in your database is a datetime you should pass an instance of DateTime for the parameter and not be attempting to convert it to string => DateStr must be a DateTime.

    Alright, now that you have correctly inserted the record into the database you could read it:

    using (var conn = new SqlConnection("Your ConnectionString comes here"))
    using (var cmd = conn.CreateCommand())
    {
        conn.Open();
        cmd.CommandText = 
        @"SELECT
              Contact_CardImage
          FROM 
              Ph_Tbl_Contacts 
          WHERE 
              Contact_ID = @Contact_ID
        ";
        cmd.Parameters.AddWithValue("@Contact_ID", Txt_Name.Text); 
        using (var reader = cmd.ExecuteReader())
        {
            if (reader.Read())
            {
                byte[] raw = (byte[])reader.Items["Contact_CardImage"];
                // TODO: do something with the raw data
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching for how to retrieve binary data from the database. My
I want to retrieve some performance counters for the 'ASP.NET Applications' category from within
I didn't make the image into binary form, but want to get that image
i want to retrieve the binary values of the data in the file. so
I want to write (and later retrieve) data in binary format. I am trying
I want to retrieve binary stream object from Collections, to be stored in a
I have a html template that I want to retrieve from the resources file
I want to create a custom dictionary that does not copy its keys (just
I want to retrieve data from two different tables depending of the value of
I am processing one binary file in which I want to retrieve first 4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.