Archive
Calender
<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011
Blogroll
    Recent comments

    None

     

    In order to show the picture in the asp.net GridView control, you have to preload the image in a separate aspx page and then use the GridView imageField Column to pass the image id to the aspx page.

    Create a getimage.aspx page.

    Create a function GetImage()

     

    Protected void GetImage()
    {
    int imageID = Convert.ToInt32((Request.QueryString["imageID"] ?? "0"));
    SqlConnection con = new SqlConnection();
    con.ConnectionString = ConfigurationManager.ConnectionStrings["youraspdotnetconnectionstring"].ConnectionString;
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText =  yourImageTableQuery;
    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Connection = con;
                SqlParameter ImageID = new SqlParameter("@imageID", System.Data.SqlDbType.Int);
                ImageID.Value = imageID;
                cmd.Parameters.Add(ImageID);
                con.Open();
                SqlDataReader dReader = cmd.ExecuteReader();
                dReader.Read();
                Response.BinaryWrite((byte[])dReader[queryColumn]);
                dReader.Close();
                con.Close();
    }

     

     

     

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

     

     

    Now back to your GridView aspx page. Add an imageField column to your GridView and configure it to point to your getimage.aspx page.

     

    Configure your bound field.

     

    <asp:BoundField DataField=" imageID " HeaderText=" imageID " ReadOnly="True"

                SortExpression=" imageID " Visible="False" />

     

    Configure your ImageField.

     

    <asp:imagefield headertext="Picture" dataimageurlfield=" imageID "

             dataimageurlformatstring='~ getimage.aspx? imageID ={0}’>

             </asp:imagefield>

     

    Hope this helps J

     

     

    Drag a fileUpload control onto your web form.

    Drag the sql  image tab le onto your web form to create the SqlDataSource1 control.

    Click on the SqlDataSource1 object, right click and select properties. On the properties

    Window, click the lightning bolt button at the top to access the available SqlDataSource1 events. Double click on the inserting event.

    in the code behind file, validate the FileUpload1 control...

    if (!this.FileUpload1.HasFile) {

    ErrorLabel.Text = "Select the picture file.";

    return; file

    }  

    protected void SqlDataSource1 _Inserting(object sender, SqlDataSourceCommandEventArgs e)

            {

                byte[] newPicture = FileUpload1.FileBytes;

                   e.Command.Parameters.Clear();

    System.Data.SqlClient.SqlParameter  pictureParam = new System.Data.SqlClient.SqlParameter("@picture", System.Data.SqlDbType.Image);

                pictureParam.Value = newPicture;

                e.Command.Parameters.Add(pictureParam);

            }