Wednesday, April 1, 2009
Upload image in grid with byte conversion
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgrid();
}
}
protected void Button1_Click1(object sender, EventArgs e)
{
Stream imgStream = fuImage.PostedFile.InputStream;
int imgLen = fuImage.PostedFile.ContentLength;
string imgName = txtImageName.Text;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData, 0, imgLen);
//use the web.config to store the connection string
SqlConnection connection = new SqlConnection("Data Source=.; Initial Catalog =master; Uid=sa; Pwd=;");
SqlCommand command = new SqlCommand("INSERT INTO imagetogrid (imagename,image) VALUES ( @img_name, @img_data)", connection);
SqlParameter param0 = new SqlParameter("@img_name", SqlDbType.VarChar, 50);
param0.Value = imgName;
command.Parameters.Add(param0);
SqlParameter param1 = new SqlParameter("@img_data", SqlDbType.Image);
param1.Value = imgBinaryData;
command.Parameters.Add(param1);
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
fillgrid();
}
public void fillgrid()
{
SqlConnection connection = new SqlConnection("Data Source= .; Initial Catalog =master; Uid=sa; Pwd=;");
SqlCommand command = new SqlCommand("SELECT imagename,ImageID from [imagetogrid]", connection);
SqlDataAdapter ada = new SqlDataAdapter(command);
ada.Fill(dt);
gvImages.DataSource = dt;
gvImages.DataBind();
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment