locked
Delete image from access db. RRS feed

  • Question

  • User2036854097 posted

    Hello. In my upload gallery i have (mdb access), i upload and insert image in. 

    How can i have a "delete" button, when i show the data in gridview?

    My code is:

    public partial class _upload : System.Web.UI.Page

    {

        // Access Database oledb connection string

        // Using Provider Microsoft.Jet.OLEDB.4.0

        String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("App_Data/db1.mdb");

        // Object created for Oledb Connection

        OleDbConnection myAccessConnection;

        protected void openAccessConnection()

        {

            // If condition that can be used to check the access database connection

            // whether it is already open or not.

            if(myAccessConnection.State == ConnectionState.Closed)

            {

                myAccessConnection.Open();

            }

        }

        protected void closeAccessConnection()

        {

            // If condition to check the access database connection state

            // If it is open then close it.

            if (myAccessConnection.State == ConnectionState.Open)

            {

                myAccessConnection.Close();

            }

        }

        protected void Page_Load(object sender, EventArgs e)

        {

            myAccessConnection = new OleDbConnection(connStr);

            if (!IsPostBack)

            {

                displayImages();

            }

        }

        protected void btnUpload_Click(object sender, EventArgs e)

        {

            int imageSize;

            string imageType;

            Stream imageStream;

            // Gets the Size of the Image

            imageSize = fileImgUpload.PostedFile.ContentLength;

            // Gets the Image Type

            imageType = fileImgUpload.PostedFile.ContentType;

            // Reads the Image stream

            imageStream = fileImgUpload.PostedFile.InputStream;

            byte[] imageContent = new byte[imageSize];

            int intStatus;

            intStatus = imageStream.Read(imageContent, 0, imageSize);

            

            OleDbCommand myCommand = new OleDbCommand("insert into tblImg(img_title,img_stream,img_type) values(@img_title,@img_stream,@img_type)", myAccessConnection);

            // Mark the Command as a Text

            myCommand.CommandType = CommandType.Text;

            // Add Parameters to Command

            OleDbParameter img_title = new OleDbParameter("@img_title", OleDbType.VarChar);

            img_title.Value = txtImgTitle.Text;

            myCommand.Parameters.Add(img_title);

            OleDbParameter img_stream = new OleDbParameter("@img_stream", OleDbType.Binary);

            img_stream.Value = imageContent;

            myCommand.Parameters.Add(img_stream);

            OleDbParameter img_Type = new OleDbParameter("@img_type", OleDbType.VarChar);

            img_Type.Value = imageType;

            myCommand.Parameters.Add(img_Type);

            try

            {

                openAccessConnection();

                myCommand.ExecuteNonQuery();

                closeAccessConnection();

                Response.Redirect("upl.aspx");

            }

            catch (Exception exc)

            {

                Response.Write("Insert Failure. Error Details : " + exc.Message.ToString());

            }

            

        }

        public void displayImages()

        {

            try

            {

                openAccessConnection();

                OleDbCommand myCommand = new OleDbCommand("select * from tblImg", myAccessConnection);

                // Mark the Command as a Text

                myCommand.CommandType = CommandType.Text;

                OleDbDataAdapter myAdapter = new OleDbDataAdapter(myCommand);

                DataSet myDataSet = new DataSet();

                myAdapter.Fill(myDataSet);

                if (myDataSet.Tables[0].Rows.Count>0)

                {

                    GridView1.DataSource = myDataSet;

                    GridView1.DataBind();

                }

                closeAccessConnection();

            }

            catch (Exception exc)

            {

                Response.Write("Data Retrival Failure. Error Details : " + exc.Message.ToString());

            }

        }

        public string imageURL(string img_id)

        {

            return ("retrieveImages.aspx?id=" + img_id);

        }

    }

    ------------------------------------------------------------

     <div>

                    Image Title:<asp:TextBox ID="txtImgTitle" runat="server"></asp:TextBox>

                </div>

                <div>

                    Browse Image:<asp:FileUpload ID="fileImgUpload" runat="server" />

                </div>

                <div>

                    <asp:Button ID="btnUpload" runat="server" Text="Save" OnClick="btnUpload_Click" /></div>

            </div>

            <br />

            <br />

            <div>

                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="5"

                    GridLines="None" ShowHeader="False">

                    <Columns>

                        <asp:TemplateField>

                            <ItemTemplate>

                                <asp:Image ID="imgSaved" runat="server"   ImageUrl='<%# imageURL(DataBinder.Eval(Container.DataItem, "img_id").ToString()) %>'

                                    AlternateText='<%#DataBinder.Eval(Container.DataItem,"img_title") %>' Height="80px"  Width="100px" />

                                <asp:HyperLink ID="HyperLink11" rel="lyteshow[map]" runat="server" 

                                    Width="160px" Text='<%# Eval("img_title") %>'    > </asp:HyperLink>

                   

                           

                            </ItemTemplate>

                        

                        

                        </asp:TemplateField>

                 </Columns>

                </asp:GridView>

            </div>

            

            

            <div>

                

                

                <br />

            </div>

            </div>

        <asp:AccessDataSource ID="AccessDataSource1" runat="server" 

            DataFile="~/App_Data/db1.mdb" SelectCommand="SELECT * FROM [tblImg]">

        </asp:AccessDataSource>

        </form>

    Thursday, October 31, 2013 4:43 PM

Answers

  • User1508394307 posted

    Take a look at the following example at http://www.codeproject.com/Tips/257174/edit-Update-Cancel-Delete-rows-in-Gridview-using-M

    You would need to add 

    <asp:GridView OnRowDeleting="GridView1_RowDeleting"  AutoGenerateDeleteButton="True"...

    then add GridView1_RowDeleting() method 

    Example

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
    Label lbldeleteID = (Label)row.FindControl("lblid");
    conn.Open();
    OleDbCommand cmd = new OleDbCommand("delete from emp where rowid=" + lbldeleteID.Text + "", conn);
    //SqlCommand cmd = new SqlCommand("delete emp where rowid=" + lbldeleteID.Text + "", conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    bind();
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 31, 2013 5:27 PM