Saturday 7 July 2012

Image insert and retrieval with database

//Insert Image
protected void btnAdd_Click(object sender, EventArgs e)
        {
            //Condition to check if the file uploaded or not
                if (FileUpload1.HasFile)
                {
                //getting length of uploaded file
                int length = FileUpload1.PostedFile.ContentLength;

                //create a byte array to store the binary image data
                byte[] imgbyte = new byte[length];

                //store the currently selected file in memeory
                HttpPostedFile img = FileUpload1.PostedFile;

                //set the binary data
                img.InputStream.Read(imgbyte, 0, length);

                //use the web.config to store the connection string
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Product VALUES (@ImageName,@Image,@imgCost)", connection);
                cmd.Parameters.Add("@ImageName", SqlDbType.VarChar, 50).Value = txtImageName.Text;
                cmd.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
                cmd.Parameters.Add("@imgCost", SqlDbType.NVarChar, 50).Value = txtImageCost.Text;
                int count = cmd.ExecuteNonQuery();
                connection.Close();
                if (count == 1)
                {
                txtImageName.Text = string.Empty;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + txtImageName.Text + " image inserted successfully')", true);
                Response.Redirect("~/AdminPanel/Product.aspx");
                }
                }
                }

//Retrive Image

1) Designer Code for dispaly Image in grid view

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource1" Width="460px" AllowPaging="True"
        PageSize="1" CellPadding="4" ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="ImageId" HeaderText="Image ID" InsertVisible="False"
                ReadOnly="True" SortExpression="ImageId" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="ImageName" HeaderText="Image Name"
                SortExpression="ImageName" ItemStyle-HorizontalAlign="Center"/>
            <asp:BoundField DataField="imgCost" HeaderText="Image Cost"
                SortExpression="imgCost" ItemStyle-HorizontalAlign="Center"/>
                <asp:TemplateField HeaderText="Product Image">
                <ItemTemplate>
                    <asp:Image ID="Image2" runat="server" ImageUrl='<%# "~/ImageHandler.ashx?ImID="+ Eval("ImageId") %>'
                        Height="150px" Width="150px" ImageAlign="Right"/>
                </ItemTemplate>
            </asp:TemplateField>

        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

<HeaderStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True"></HeaderStyle>
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>


 

No comments:

Post a Comment