Friday, December 12, 2008

Code Help to Upload Image In Binary Format in SQL SERVER DB

Hi...

The coming post is about to How we can store image in to the database in a binary foramt.
we are very thankful to Mr. Shameem Pundeer for his kind supports ..And I hope it will be carry on in the future..


Color1 is Datagird name. On Updatecommand of datagrid we write code to upload vedio and image in sql-server database.

private void color1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
int getItemID = Convert.ToInt32(color1.DataKeys[e.Item.ItemIndex]);
System.Web.UI.HtmlControls.HtmlInputFile File1 = (System.Web.UI.HtmlControls.HtmlInputFile)e.Item.Cells[0].Controls[1];
System.Web.UI.HtmlControls.HtmlInputFile File2 = (System.Web.UI.HtmlControls.HtmlInputFile)e.Item.Cells[1].Controls[1];
TextBox txt1 = (TextBox)e.Item.Cells[2].Controls[1];
TextBox txt2 = (TextBox)e.Item.Cells[3].Controls[1];


if(File1.Value != "")// to upload vedio file on sql-server database
{

string fpath = File1.PostedFile.FileName;
string fName = System.IO.Path.GetFileName(fpath);
string currPath = Request.PhysicalApplicationPath;
File1.PostedFile.SaveAs(currPath+ "/_video/" + fName);

string SqlUpdFName = "update item set fileName='" + fName + "'" + " where itemID=" + getItemID ;
int AffrowUpd = Convert.ToInt32(oDBManager.ExecuteNonQuery(SqlUpdFName));
}

if(File2.Value != "")// Upload image file to sql-server database in binary format
{
////////////////////Deleting Last Image Which has to Update////////////////////////
string SqlDelImg = "Delete from image where itemID =" + getItemID;
int rowAff3 = Convert.ToInt32(oDBManager.ExecuteNonQuery(SqlDelImg));

////////////////////////////////////////////////////////////////////////////////////

int len = File2.PostedFile.ContentLength;
double k = Convert.ToDouble(len);
byte[] pic = new byte[len];
File2.PostedFile.InputStream.Read (pic, 0, len);
string imgContentType = File2.PostedFile.ContentType;
string imgName1 = File2.Value;
SqlConnection Conn = new SqlConnection(@ConfigurationSettings.AppSettings["ConnectionString"]);

try
{
Conn.Open ();
SqlCommand cmd1 = new SqlCommand ("insert into image" + "(imageContent, imageType, imageName, imageSize, itemID) values (@imageContent,@imageType, @imageName, @imageSize, @itemID )", Conn);
cmd1.Parameters.Add ("@imageContent",pic );
cmd1.Parameters.Add ("@imageType", imgContentType);
cmd1.Parameters.Add ("@imageName", imgName1);
cmd1.Parameters.Add ("@imageSize", k);
cmd1.Parameters.Add("@itemID",getItemID);



cmd1.ExecuteNonQuery ();
}
finally
{
Conn.Close ();
}



}

if(txt1.Text != "")
{
string SqlUpdIName = "update item set itemName='" + txt1.Text + "'" + " where itemID=" + getItemID;
int affUpdRow1 = Convert.ToInt32(oDBManager.ExecuteNonQuery(SqlUpdIName));

}

if(txt2.Text != "")
{
string SqlUpdPrice = "update item set itemPrice=" + txt2.Text + " where itemID=" + getItemID;
int affUpdRow2 = Convert.ToInt32(oDBManager.ExecuteNonQuery(SqlUpdPrice));

}


color1.EditItemIndex = -1;
BindDataGrid();



}

Thanks:
Sanjeev Kumar
HelpOnDesk Team

No comments: