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:
publicpartialclass_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;
protectedvoid 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();
}
}
protectedvoid closeAccessConnection()
{
// If condition to check the access database connection state
// If it is open then close it.
if (myAccessConnection.State == ConnectionState.Open)
{
myAccessConnection.Close();
}
}
protectedvoid Page_Load(object sender,EventArgs e)
{
myAccessConnection = newOleDbConnection(connStr);
if (!IsPostBack)
{
displayImages();
}
}
protectedvoid 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 = newbyte[imageSize];
int intStatus;
intStatus = imageStream.Read(imageContent, 0, imageSize);
OleDbCommand myCommand = newOleDbCommand("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 = newOleDbParameter("@img_title", OleDbType.VarChar);
img_title.Value = txtImgTitle.Text;
myCommand.Parameters.Add(img_title);
OleDbParameter img_stream = newOleDbParameter("@img_stream", OleDbType.Binary);
img_stream.Value = imageContent;
myCommand.Parameters.Add(img_stream);
OleDbParameter img_Type = newOleDbParameter("@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());
}
}
publicvoid displayImages()
{
try
{
openAccessConnection();
OleDbCommand myCommand = newOleDbCommand("select * from tblImg", myAccessConnection);
// Mark the Command as a Text
myCommand.CommandType = CommandType.Text;
OleDbDataAdapter myAdapter = newOleDbDataAdapter(myCommand);
DataSet myDataSet = newDataSet();
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());
}
}
publicstring imageURL(string img_id)
{
return ("retrieveImages.aspx?id="+ img_id);
}
}
------------------------------------------------------------
<div>
Image Title:<asp:TextBoxID="txtImgTitle"runat="server"></asp:TextBox>
</div>
<div>
Browse Image:<asp:FileUploadID="fileImgUpload"runat="server"/>
</div>
<div>
<asp:ButtonID="btnUpload"runat="server"Text="Save"OnClick="btnUpload_Click"/></div>
</div>
<br/>
<br/>
<div>
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"CellPadding="5"
GridLines="None"ShowHeader="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageID="imgSaved"runat="server" ImageUrl='<%# imageURL(DataBinder.Eval(Container.DataItem, "img_id").ToString())%>'
AlternateText='<%#DataBinder.Eval(Container.DataItem,"img_title")%>'Height="80px" Width="100px"/>
<asp:HyperLinkID="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:AccessDataSourceID="AccessDataSource1"runat="server"
DataFile="~/App_Data/db1.mdb"SelectCommand="SELECT * FROM [tblImg]">
</asp:AccessDataSource>
</form>