Tuesday, 4 February 2014

Inserting Images into database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using Connection;
using System.Web;
using System.Data.Sql;
using System.Web.UI.WebControls;
namespace QlxEmail
{
    public partial class AddContent : System.Web.UI.Page
    {
         //string Constr="server=ADMIN-PC\\SQLEXPRESS;Database=Content;User Id=sa;Password=123;";
         SqlConnection conn = new SqlConnection("server=ADMIN-PC\\SQLEXPRESS;Database=Content;User Id=sa;Password=123;");
     

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (ImgHeadUpload.HasFile & ImgSideUpload.HasFile)
                {
                    int length = ImgHeadUpload.PostedFile.ContentLength;
                    //create a byte array to store the binary image data
                    byte[] imgHeader = new byte[length];
                    //store the currently selected file in memeory
                    HttpPostedFile imgHead = ImgHeadUpload.PostedFile;
                    //set the binary data
                    imgHead.InputStream.Read(imgHeader, 0, length);

                    int lengthside = ImgSideUpload.PostedFile.ContentLength;
                    //create a byte array to store the binary image data
                    byte[] imgSider = new byte[length];
                    //store the currently selected file in memeory
                    HttpPostedFile imgside = ImgSideUpload.PostedFile;
                    //set the binary data
                    imgside.InputStream.Read(imgSider, 0, length);


                    string s = "insert into tbl_Content(ToName,ContentName,Contentdata,ContentHeaderImage,ContentSideImage)Values(@ToName,@ContentName,@Contentdata,@ContentHeaderImage,@ContentSideImage)";
                    SqlCommand cmd = new SqlCommand(s, conn);
                    cmd.Parameters.AddWithValue("@ToName", txtToName.Text);
                    cmd.Parameters.AddWithValue("@ContentName", txtContentName.Text);
                    cmd.Parameters.AddWithValue("@Contentdata", txtContentData.Text);
                    cmd.Parameters.AddWithValue("@ContentHeaderImage", SqlDbType.Image).Value = imgHeader;
                    cmd.Parameters.AddWithValue("@ContentSideImage", SqlDbType.Image).Value = imgSider;
                    conn.Open();

                    int i = Convert.ToInt32(cmd.ExecuteNonQuery());
                    if (i > 0)
                    {
                        lblmsg.Text = "Content Added Successfully.";
                        lblmsg.ForeColor = System.Drawing.Color.Green;
                        txtContentName.Text = "";
                        txtContentData.Text = "";
                        txtToName.Text = "";
                        ImgHeadUpload = new FileUpload();
                        ImgSideUpload = new FileUpload();

                    }
                }
                else
                {
                    lblmsg.Text = "Please Try again.";
                    lblmsg.ForeColor = System.Drawing.Color.Red;
                    txtContentName.Text = "";
                    txtContentData.Text = "";
                    txtToName.Text = "";
                    ImgHeadUpload = new FileUpload();
                    ImgSideUpload = new FileUpload();
                }
            }
            catch (Exception ex)
            {
                lblmsg.Text = "Please Try again.";
                lblmsg.ForeColor = System.Drawing.Color.Red;
                txtContentName.Text = "";
                txtContentData.Text = "";
                txtToName.Text = "";
                ImgHeadUpload = new FileUpload();
                ImgSideUpload = new FileUpload();

            }
        }
    }
}

         
   

No comments:

Post a Comment