.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

saving image

Posted By: aamir     Posted Date: December 27, 2008    Points:2   Category :VB.Net
i want to save the image in to the data base after editing
i m using two tables
one for albums and other for photos
photos are stored in each album
description of tables as follow
Albums
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (20)
[desc] [varchar] (200)
Photos
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50)
[desc] [varchar] (200)
[album_id] [int] NOT NULL ,
[photo] [image] NULL

i open the image for editing ,after modifying the image i click on save button on the toolbar but modified image is not saved back in database
i use following code under click event of the button

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsnbtnSave.Click
Dim str As New IO.MemoryStream

pb.Image.Save(str, Imaging.ImageFormat.Jpeg)
'Dim buffer(CInt(str.Length - 1)) As Byte

' Read the bytes from this stream
'str.Read(buffer, 0, CInt(str.Length))
Dim buffer As Byte() = str.GetBuffer()
' Now we can close the stream
Dim con As New SqlConnection(My.Settings.ConnectionString)
Dim cmd As New SqlCommand("INSERT INTO Photos Values(@name,@desc,@album_id,@photo)", con)
cmd.CommandType = CommandType.Text
Dim param(3) As SqlParameter
With param
param(0) = New SqlParameter()
param(0).ParameterName = "@name"
param(0).SqlDbType = SqlDbType.VarChar
param(0).Value = "name"
cmd.Parameters.Add(param(0))

param(1) = New SqlParameter()
param(1).ParameterName = "@desc"
param(1).SqlDbType = SqlDbType.VarChar
param(1).Value = "desc"
cmd.Parameters.Add(param(1))

param(2) = New SqlParameter()
param(2).ParameterName = "@album_id"
param(2).SqlDbType = SqlDbType.Int
param(2).Value = 1
cmd.Parameters.Add(param(2))

param(3) = New SqlParameter()
param(3).ParameterName = "@photo"
param(3).SqlDbType = SqlDbType.Image
param(3).Value = buffer
cmd.Parameters.Add(param(3))
End With
'cmd.Parameters.Add(param)
con.Open()
cmd.ExecuteNonQuery()
'str.Close()
End Sub


i m trying to do so but i m not succeeding
any body can help me??
thanks

Responses
Author: Amit Mehra             
Posted Date: December 27, 2008     Points: 5   

Hi Aamir,
Please refer this article to save image in Database

http://www.dotnetspark.com/kb/35-read--write-image-into-sql-server-asp-net.aspx

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: aamir             
Posted Date: December 28, 2008     Points: 5   

above refered article is with respect to asp.net
any body can tell me what is my problem with my code

Author: Amit Mehra             
Posted Date: December 28, 2008     Points: 5   

Hi Aamir,
Just check this line..."pb.Image.Save(str, Imaging.ImageFormat.Jpeg)" Are you getting Byte array of image? Or what erroe are you getting..

One more thing the above link which i have given is based on C# itself so it doest matter. Except getting buffer part.

Your code seemd fine to me. But just check the buffer part.

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: aamir             
Posted Date: December 30, 2008     Points: 5   

yes i m getting byte array of image
in above code i m not getting an error but image is not being saved back into database after editing

Author: Athira Sreejith             
Posted Date: December 31, 2008     Points: 5   

Hi..

Refer:

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection connection = null;
try
{
FileUpload img = (FileUpload)imgUpload;
Byte[] imgByte = null;
if (img.HasFile && img.PostedFile != null)
{
//To create a PostedFile
HttpPostedFile File = imgUpload.PostedFile;
//Create byte Array with file len
imgByte = new Byte[File.ContentLength];
//force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength);
}
// Insert the employee name and image into db
string conn = ConfigurationManager.ConnectionStrings["EmployeeConnString"].ConnectionString;
connection = new SqlConnection(conn);

connection.Open();
string sql = "INSERT INTO EmpDetails(empname,empimg) VALUES(@enm, @eimg) SELECT @@IDENTITY";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@enm", txtEName.Text.Trim());
cmd.Parameters.AddWithValue("@eimg", imgByte);
int id = Convert.ToInt32(cmd.ExecuteScalar());
lblResult.Text = String.Format("Employee ID is {0}", id);

// Display the image from the database
Image1.ImageUrl = "~/ShowImage.ashx?id=" + id;
}
catch
{
lblResult.Text = "There was an error";
}
finally
{
connection.Close();
}

}
}

Author: krishana singh             
Posted Date: October 16, 2010     Points: 5   

try{
string ImagePath = "";
string FolderImagePath = "";

if (FileUploadImage.HasFile)
{
string path = @Server.MapPath("~/images/" + ddlcategory.SelectedItem.ToString());

if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
FolderImagePath = path+ "/" + FileUploadImage.FileName;
FileUploadImage.SaveAs(FolderImagePath);
Business.Utility.GenerateThumb(FolderImagePath + "_Thumb.jpg", FolderImagePath, 120, 120);
ImagePath = (ddlcategory.SelectedItem.ToString() + "/" + FileUploadImage.FileName);
}

if ((ddlcategory.SelectedItem.ToString() != "--select--") && (ddlname.ToString() != "--select--"))
{
if (con.State == ConnectionState.Open)
con.Close();
con.Open();
string str = "Insert into tblImg(ImageName,ImagePath,ImageDescription,ImageCost,RefferenceID,SubCategoryID) values('" + txtname.Text.Trim() + "','" + ImagePath + "' ,'" + txtdes.Text + "'," + txtcost.Text + "," + ddlcategory.SelectedValue + "," + ddlname.SelectedValue + ")";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();

txtname.Text = "";
txtcost.Text = "";
txtdes.Text = "";
txtname.Focus();
Label2.Text = "Flower Insert Successfully";
}
else
Label2.Text = "please choose proper category or Subcategory";
}
catch(Exception ex)
{
}
}



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend