locked
Asp İmage insert and Read ? RRS feed

  • Question

  • User-1628313597 posted

    Hi,

    I can not  insert and read. I want your help :(

    Asp C#

     

    Saturday, December 26, 2015 7:30 PM

All replies

  • User303363814 posted

    Have you written any code?  Which is the first line of your code that does not do what you want it to do?  Fix that line of code and rerun your program.

    Sunday, December 27, 2015 6:06 AM
  • User-1628313597 posted

    I did not write

    Sunday, December 27, 2015 6:39 PM
  • User1724605321 posted

    Hi sergenaksu,

    Welcome to ASP.NET Forum .

    Asp İmage insert and Read ?

    You could use a file upload control to upload the image which you want to save to database , below code is for your reference :

     //check to ensure a file is selected before attempting to save it
        if (FileUpload1.PostedFile != null && !string.IsNullOrEmpty(FileUpload1.PostedFile.FileName))
        {
            //create byte array with size corresponding to the currently selected file
            byte[] imgBin = new byte[FileUpload1.PostedFile.ContentLength];
            //store the currently selected file in memory
            HttpPostedFile img = FileUpload1.PostedFile;
            //store the image binary data of the selected file in the imgBin byte array
            img.InputStream.Read(imgBin, 0, (int)FileUpload1.PostedFile.ContentLength);
     
            //connect to the db
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
     
            //sql command to send all of our img data to the db
            SqlCommand cmd = new SqlCommand("INSERT INTO Images(ImgBin, ImgType, ImgSize) VALUES (@ImgBin, @ImgType, @ImgSize)", conn);
            cmd.CommandType = CommandType.Text;
     
            //add the image binary data to the sql command
            cmd.Parameters.Add("@ImgBin", SqlDbType.Image, imgBin.Length).Value = imgBin;
            //add the image type to the sql command
            cmd.Parameters.AddWithValue("@ImgType", FileUpload1.PostedFile.ContentType);
            //add the image size to the sql command
            cmd.Parameters.AddWithValue("@ImgSize", FileUpload1.PostedFile.ContentLength);
     
            using (conn)
            {
                //open the connection
                conn.Open();
                //send the sql query to store the data
                cmd.ExecuteNonQuery();
            }
        }

    Then read the image from database :

    //connect to the db
        SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
     
        //sql command to select the image with id of 1
        SqlCommand cmd = new SqlCommand("SELECT * FROM Images WHERE ImgId=@ImgId", conn);
        cmd.CommandType = CommandType.Text;
     
        //select the image with ImgId of 1
        cmd.Parameters.AddWithValue("@ImgId", 1);
     
        using (conn)
        {
            //open the connection
            conn.Open();
            //send the sql query to select the image and store the results in a sqldatareader
            SqlDataReader rdr = cmd.ExecuteReader();
            //read the data
            if (rdr.Read())
            {
                //store the image binary data in a byte array
                Byte[] imgData = (byte[])rdr["ImgBin"];
                //setup the image type to be displayed
                Response.ContentType = (rdr["ImgType"].ToString());
                //output the image to the page
                Response.OutputStream.Write(imgData, 0, imgData.Length);
            }
        }
    

    Please refer to below articles for details and demo :

    http://www.aspnettutorials.com/tutorials/database/store-img-bins-asp4-cs/ .

    http://www.aspsnippets.com/Articles/How-to-save-insert-Image-in-Database-in-ASPNet-using-C-and-VBNet.aspx

    Best Regards,

    Nan Yu

    Monday, December 28, 2015 3:31 AM
  • User-1628313597 posted

    Thank you Nan Yu.

    Thank you for your interest, I've done with this method but I could not do the second insert. (FileUpload2)

    What should I do ? How do I edit this code ?

    Byte[] bytDizi = FileUpload1.FileBytes;
    MemoryStream ms = new MemoryStream(bytDizi);
    ms.Read(bytDizi, 0, bytDizi.Length);
    ms.Close();
    SqlParameter parametreDosya = new SqlParameter("@resim", SqlDbType.VarBinary, bytDizi.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytDizi);
    
    komut = new SqlCommand("insert into Res_Tab(resim)values(@resim)", baglanti);
    komut.Parameters.Add(parametreDosya);
    komut.Parameters.AddWithValue("@REFNR", TextBox1.Text); 
    komut.ExecuteNonQuery();
    baglanti.Close();
    komut.Dispose();

    Monday, December 28, 2015 7:07 PM
  • User1724605321 posted

    Hi sergenaksu,

    Thank you for your interest, I've done with this method but I could not do the second insert. (FileUpload2)

    Please explain more about your requirement .

    Best Regards,

    Nan Yu

    Tuesday, December 29, 2015 5:33 AM
  • User-1628313597 posted

    This insert works

    Byte[] bytDizi = FileUpload1.FileBytes;
    MemoryStream ms = new MemoryStream(bytDizi);
    ms.Read(bytDizi, 0, bytDizi.Length);
    ms.Close();
    SqlParameter parametreDosya = new SqlParameter("@resim", SqlDbType.VarBinary, bytDizi.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytDizi);
    
    komut = new SqlCommand("insert into Res_Tab(resim)values(@resim)", baglanti);
    komut.Parameters.Add(parametreDosya);
    komut.Parameters.AddWithValue("@REFNR", TextBox1.Text); 
    komut.ExecuteNonQuery();
    baglanti.Close();
    komut.Dispose();

    This insert does not work

    Byte[] bytDizi2 = FileUpload2.FileBytes;
    MemoryStream ms2 = new MemoryStream(bytDizi2);
    ms2.Read(bytDizi2, 0, bytDizi2.Length);
    ms2.Close();
    SqlParameter parametreDosya2 = new SqlParameter("@resim2", SqlDbType.VarBinary, bytDizi2.Length, ParameterDirection.Input, false,0, 0, null, DataRowVersion.Current, bytDizi2);
    
    komut2 = new SqlCommand("insert into Res_Tab(resim2)values(@resim2)", baglanti);
    komut2.Parameters.Add(parametreDosya2);
    komut2.Parameters.AddWithValue("@REFNR", TextBox1.Text); 
    komut2.ExecuteNonQuery();
    baglanti.Close();
    komu2t.Dispose();



    Tuesday, December 29, 2015 7:38 AM
  • User1724605321 posted

    Hi sergenaksu,

    What is the error message .

    Best Regards,

    Nan Yu

    Tuesday, December 29, 2015 7:50 AM
  • User-1628313597 posted

    Error Message. but I have to resim2

    Must declare the scalar variable "@resim2".

    Tuesday, December 29, 2015 11:28 AM