Merhaba
arkadaşlar bu makalemizde GridView nesnemize resim ekleyeceğiz. FileUpload ile
seçtiğimiz resimleri, Uploaded klasörüne kopyalayacağız. GridView de
eklediğimiz resim ve açıklamasını Edit kısmından güncelleyebileceğiz veya
eklemiş olduğumuz resimi silebileceğiz.
Screenshot

Şekil
1
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection("Data
Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\image.mdf;Integrated
Security=True");
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
DataSet ds = new DataSet();
String fname, fpath, desc;
String spath;
int id;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGrid();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
//Bu kısımda dosya mevcut mu, değilse uploaded klasörüne
kopyalıyoruz.
fname = FileUpload1.FileName;
spath = @"~\Uploaded\" + FileUpload1.FileName;
fpath = Server.MapPath("Uploaded");
fpath = fpath + @"\" + FileUpload1.FileName;
desc = TextBox1.Text;
if
(System.IO.File.Exists(fpath))
{
//Aynı dosyayı önceden uploaded klasörüne
kopyalamışsak Label de uyarımesajı veriyoruz.
Label1.Text = "Dosya adı
bulunmaktadır! ";
return;
}
else
{
FileUpload1.SaveAs(fpath);
}
SqlDetails();
TextBox1.Text = "";
BindGrid();
}
else
{
Label1.Text = "Lütfen dosya
seçiniz!";
}
}
void SqlDetails()
{
sqlcon.Open();
sqlcmd = new SqlCommand("Select * From
[dbo].[Table]",
sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
SqlDataReader dr;
dr = sqlcmd.ExecuteReader();
while (dr.Read())
{
// en son kayıt id no'sunu alıyoruz.
id = Convert.ToInt32(dr["Id"].ToString());
}
id = id + 1;
sqlcon.Close();
String query;
query = "insert into [dbo].[Table](Id,fname,fpath,description)
values('"
+ id +"','" + fname + "','" + spath + "','" + desc + "')";
sqlcon.Open();
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
BindGrid();
}
void BindGrid()
{
sqlcon.Open();
sqlcmd = new SqlCommand("Select * From
[dbo].[Table]",
sqlcon);
da = new SqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.DataBind();
}
sqlcon.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String ID;
ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
sqlcmd = new SqlCommand("Select * From
[dbo].[Table] Where Id='" + ID + "'",
sqlcon);
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
if
(System.IO.File.Exists(Server.MapPath(dt.Rows[0][2].ToString())))
{
System.IO.File.Delete(Server.MapPath(dt.Rows[0][2].ToString()));
}
}
sqlcmd = new SqlCommand("Delete From
[dbo].[Table] Where Id='" + ID + "'",
sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row =
GridView1.Rows[e.RowIndex];
string ID;
ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
FileUpload flname = (FileUpload)row.FindControl("FileUpload2");
if (flname.HasFile)
{
fname = flname.FileName;
spath = @"~\Uploaded\" + flname.FileName;
fpath = Server.MapPath("Uploaded");
fpath = fpath + @"\" + flname.FileName;
if
(System.IO.File.Exists(fpath))
{
Label1.Text = "Dosya adı
bulunmaktadır!";
return;
}
else
{
flname.SaveAs(fpath);
}
}
else
{
Label1.Text = "Lütfen dosya
seçiniz!";
}
TextBox desc1 = (TextBox)row.FindControl("txtdesc");
string query;
query = "update [dbo].[Table] set fname='" + fname + "',fpath='" + spath + "',description='" + desc1.Text + "' where Id='" + ID + "'";
sqlcon.Open();
sqlcmd = new SqlCommand(query, sqlcon);
sqlcmd.CommandType = CommandType.Text;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
BindGrid();
}
}
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head
runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Resim
Seçiniz:"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Resim
Açıklaması:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server" Width="314px" DataKeyNames="ID" AutoGenerateColumns="false" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Dosya Adı">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%#Eval("fpath")%>' Width="150" Height="150" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Açıklama">
<ItemTemplate>
<%#Eval("description")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdesc" runat="server" Text='<%#Eval("description")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit" ShowEditButton="true" EditText="Edit">
<ControlStyle Width="50" />
</asp:CommandField>
<asp:TemplateField HeaderText="Sil">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" CommandName="Delete" runat="server" OnClientClick="return confirm('Bu
kayıtı silmekten emin misiniz?');">Sil</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede
görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder