1

Hy master I have some problem when I convert Image varbinary(MAX) I have a procedure like this

Create Procedure SelectBarang   
as     
Begin    
    Select * from Barang;    
End

and i have class model like this

public class ShopDB
{
    string cs = ConfigurationManager.ConnectionStrings["ShopEntities"].ConnectionString;

    public List<Barang> ListAll()
    {
        List<Barang> lst = new List<Barang>();
        SqlConnection con = new SqlConnection(cs);
        //using(SqlConnection con=new SqlConnection(cs))
        {
            con.Open();
            SqlCommand com = new SqlCommand("SelectBarang", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr = com.ExecuteReader();
            while (rdr.Read())
            {
                lst.Add(new Barang
                {
                    IdBarang = Convert.ToInt32(rdr["IdBarang"]),
                    NamaBarang = rdr["NamaBarang"].ToString(),
                    Harga = Convert.ToInt32(rdr["Harga"]),
                    CategoriId = Convert.ToInt32(rdr["CategoriId"]),
                    GambarBarang = Convert  ??                            
                });
            }
            return lst;
        }
    }
}

How I can change varbinary to image for Add my data?

5
  • 2
    Possible duplicate of Read Image from SQL Server VarBinary(Max) Commented May 30, 2017 at 3:36
  • i not dulicate that @NightOwl888 Commented May 30, 2017 at 3:42
  • 1
    What is the data type for GambarBarang? Commented May 30, 2017 at 3:49
  • Hi you can take as byte array and convert to image document.getElementById("ItemPreview").src = "data:image/png;base64," + YourByte; Commented May 30, 2017 at 4:00
  • @JujurSitanggang - You may not have intentionally copied the other question, and that is understandable. But the purpose of StackOverflow is to try to provide a canonical question and answer for a given question. It is your responsibility to make sure the question has not been previously asked and adequately answered on StackOverflow before asking it again. Commented May 30, 2017 at 4:43

1 Answer 1

1

Hi you can do something like

  1. you can read data using:

     byte[] myImage = (byte[])reader["MyImageColumn"];
    
  2. Then use this in mvc view

    @{
     var base64 = Convert.ToBase64String(Model.ByteArray);
     var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.