<center>
<h1 style="color:#CCCCCC">Infrastructure Admin</h1>
<br />
<table>
<tr>
<td style="width:100px">
<asp:Label ID="Label1" Font-Bold="true" Font-Size="Larger" ForeColor="#CCCCCC" runat="server" Text="Image 1"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" ForeColor="#CCCCCC" runat="server" />
<br /><br /></td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" Font-Bold="true" Font-Size="Larger" ForeColor="#CCCCCC" runat="server" Text="Image 2"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload3" ForeColor="#CCCCCC" runat="server" />
<br /><br /></td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" Font-Bold="true" Font-Size="Larger" ForeColor="#CCCCCC" runat="server" Text=" Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" Height="20px" Width="150px" runat="server"></asp:TextBox>
<br /><br /> </td>
</tr>
<tr>
<td>
</td>
<td >
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<br /><br /></td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="id" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Width="100px" Text="Image 1"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label15" runat="server" Visible="false" Text='<%#Eval("image1") %>'></asp:Label>
<asp:Image ID="Image1" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image1") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Visible="false" Text='<%#Eval("image1") %>'></asp:Label>
<asp:Image ID="Image2" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image1") %>' /><br />
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Width="100px" Text="Image 2"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label156" runat="server" Visible="false" Text='<%#Eval("image2") %>'></asp:Label>
<asp:Image ID="Image3" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image2") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label110" runat="server" Visible="false" Text='<%#Eval("image2") %>'></asp:Label>
<asp:Image ID="Image4" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image2") %>' /><br />
<asp:FileUpload ID="FileUpload4" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label115" runat="server" Width="100px" Text="Package Name"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#Eval("c_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" Text='<%#Eval("c_name") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Height="30px">
<HeaderTemplate>
<asp:Label ID="Label13" runat="server" Width="190px" Text="Operations"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<br />
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</center>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
if (Session["username"] == null)
{
Response.Redirect("adminlogin.aspx");
}
datashow();
}
}
catch (Exception ex)
{
}
}
public void datashow()
{
try
{
da = new SqlDataAdapter("select * from infrastructure", con);
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string duplicate = "image" + "/" + FileUpload1.FileName;
string completePath = Server.MapPath(duplicate); // or string completePath = Server.MapPath("images/Desert.jpg");
if (System.IO.File.Exists(completePath))
{
Response.Write("<script language=javascript>alert('Image Name Already Exist') </script>");
}
else
{
cmd = new SqlCommand("insert into infrastructure(image1,image2,c_name) values(@image1,@image2,@c_name)", con);
if (FileUpload1.HasFile)
{
string im = "franchimag" + "/" + FileUpload1.FileName;
cmd.Parameters.AddWithValue("@image1", im);
string s = Server.MapPath("franchimag");
s = s + "/" + FileUpload1.FileName;
FileUpload1.SaveAs(s);
}
if (FileUpload3.HasFile)
{
string im = "franchimag" + "/" + FileUpload3.FileName;
cmd.Parameters.AddWithValue("@image2", im);
string s = Server.MapPath("franchimag");
s = s + "/" + FileUpload3.FileName;
FileUpload3.SaveAs(s);
}
cmd.Parameters.Add("@c_name", TextBox1.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
datashow();
}
}
catch (Exception)
{
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
GridView1.EditIndex = -1;
datashow();
}
catch (Exception)
{
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
cmd = new SqlCommand("delete from infrastructure where id=@id", con);
cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// Below code for deleting image from server folder
Label txt = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label15")));
string del = txt.Text;
string completePath = Server.MapPath(del); // or string completePath = Server.MapPath("images/Desert.jpg");
if (System.IO.File.Exists(completePath))
{
System.IO.File.Delete(completePath);
}
datashow();
}
catch (Exception)
{
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
GridView1.EditIndex = e.NewEditIndex;
datashow();
}
catch (Exception)
{
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
cmd = new SqlCommand("update infrastructure set image1=@image1,image2=@image2,c_name=@c_name where id=@id", con);
cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
FileUpload f2 = ((FileUpload)(GridView1.Rows[e.RowIndex].FindControl("FileUpload2")));
if (f2.HasFile)
{
string ss = "franchimag" + "/" + f2.FileName;
cmd.Parameters.Add("@image1", SqlDbType.NVarChar).Value = ss;
string s = Server.MapPath("franchimag");
s = s + "/" + f2.FileName;
f2.SaveAs(s);
}
else
{
string lb = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text;
cmd.Parameters.Add("@image1", SqlDbType.NVarChar).Value = lb;
}
FileUpload f3 = ((FileUpload)(GridView1.Rows[e.RowIndex].FindControl("FileUpload4")));
if (f3.HasFile)
{
string ss = "franchimag" + "/" + f3.FileName;
cmd.Parameters.Add("@image2", SqlDbType.NVarChar).Value = ss;
string s = Server.MapPath("franchimag");
s = s + "/" + f3.FileName;
f3.SaveAs(s);
}
else
{
string lb = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label110"))).Text;
cmd.Parameters.Add("@image2", SqlDbType.NVarChar).Value = lb;
}
cmd.Parameters.Add("@c_name", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox5"))).Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
datashow();
}
catch (Exception)
{
}
}
}
<h1 style="color:#CCCCCC">Infrastructure Admin</h1>
<br />
<table>
<tr>
<td style="width:100px">
<asp:Label ID="Label1" Font-Bold="true" Font-Size="Larger" ForeColor="#CCCCCC" runat="server" Text="Image 1"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload1" ForeColor="#CCCCCC" runat="server" />
<br /><br /></td>
</tr>
<tr>
<td>
<asp:Label ID="Label7" Font-Bold="true" Font-Size="Larger" ForeColor="#CCCCCC" runat="server" Text="Image 2"></asp:Label>
</td>
<td>
<asp:FileUpload ID="FileUpload3" ForeColor="#CCCCCC" runat="server" />
<br /><br /></td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" Font-Bold="true" Font-Size="Larger" ForeColor="#CCCCCC" runat="server" Text=" Name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" Height="20px" Width="150px" runat="server"></asp:TextBox>
<br /><br /> </td>
</tr>
<tr>
<td>
</td>
<td >
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<br /><br /></td>
</tr>
</table>
<br />
<asp:GridView ID="GridView1" AutoGenerateColumns="False" DataKeyNames="id" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None" onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField >
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Width="100px" Text="Image 1"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label15" runat="server" Visible="false" Text='<%#Eval("image1") %>'></asp:Label>
<asp:Image ID="Image1" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image1") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Visible="false" Text='<%#Eval("image1") %>'></asp:Label>
<asp:Image ID="Image2" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image1") %>' /><br />
<asp:FileUpload ID="FileUpload2" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label4" runat="server" Width="100px" Text="Image 2"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label156" runat="server" Visible="false" Text='<%#Eval("image2") %>'></asp:Label>
<asp:Image ID="Image3" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image2") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label110" runat="server" Visible="false" Text='<%#Eval("image2") %>'></asp:Label>
<asp:Image ID="Image4" Height="80px" Width="100px" runat="server" ImageUrl='<%#Eval("image2") %>' /><br />
<asp:FileUpload ID="FileUpload4" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="Label115" runat="server" Width="100px" Text="Package Name"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%#Eval("c_name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox5" Text='<%#Eval("c_name") %>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Height="30px">
<HeaderTemplate>
<asp:Label ID="Label13" runat="server" Width="190px" Text="Operations"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<br />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">Edit</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="delete">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<br />
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CommandName="cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</center>
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
public partial class Default2 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter da;
SqlCommand cmd;
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
if (Session["username"] == null)
{
Response.Redirect("adminlogin.aspx");
}
datashow();
}
}
catch (Exception ex)
{
}
}
public void datashow()
{
try
{
da = new SqlDataAdapter("select * from infrastructure", con);
dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
catch (Exception)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
string duplicate = "image" + "/" + FileUpload1.FileName;
string completePath = Server.MapPath(duplicate); // or string completePath = Server.MapPath("images/Desert.jpg");
if (System.IO.File.Exists(completePath))
{
Response.Write("<script language=javascript>alert('Image Name Already Exist') </script>");
}
else
{
cmd = new SqlCommand("insert into infrastructure(image1,image2,c_name) values(@image1,@image2,@c_name)", con);
if (FileUpload1.HasFile)
{
string im = "franchimag" + "/" + FileUpload1.FileName;
cmd.Parameters.AddWithValue("@image1", im);
string s = Server.MapPath("franchimag");
s = s + "/" + FileUpload1.FileName;
FileUpload1.SaveAs(s);
}
if (FileUpload3.HasFile)
{
string im = "franchimag" + "/" + FileUpload3.FileName;
cmd.Parameters.AddWithValue("@image2", im);
string s = Server.MapPath("franchimag");
s = s + "/" + FileUpload3.FileName;
FileUpload3.SaveAs(s);
}
cmd.Parameters.Add("@c_name", TextBox1.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
datashow();
}
}
catch (Exception)
{
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
try
{
GridView1.EditIndex = -1;
datashow();
}
catch (Exception)
{
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
cmd = new SqlCommand("delete from infrastructure where id=@id", con);
cmd.Parameters.Add("@id", SqlDbType.BigInt).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
// Below code for deleting image from server folder
Label txt = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label15")));
string del = txt.Text;
string completePath = Server.MapPath(del); // or string completePath = Server.MapPath("images/Desert.jpg");
if (System.IO.File.Exists(completePath))
{
System.IO.File.Delete(completePath);
}
datashow();
}
catch (Exception)
{
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
GridView1.EditIndex = e.NewEditIndex;
datashow();
}
catch (Exception)
{
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
cmd = new SqlCommand("update infrastructure set image1=@image1,image2=@image2,c_name=@c_name where id=@id", con);
cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
FileUpload f2 = ((FileUpload)(GridView1.Rows[e.RowIndex].FindControl("FileUpload2")));
if (f2.HasFile)
{
string ss = "franchimag" + "/" + f2.FileName;
cmd.Parameters.Add("@image1", SqlDbType.NVarChar).Value = ss;
string s = Server.MapPath("franchimag");
s = s + "/" + f2.FileName;
f2.SaveAs(s);
}
else
{
string lb = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label1"))).Text;
cmd.Parameters.Add("@image1", SqlDbType.NVarChar).Value = lb;
}
FileUpload f3 = ((FileUpload)(GridView1.Rows[e.RowIndex].FindControl("FileUpload4")));
if (f3.HasFile)
{
string ss = "franchimag" + "/" + f3.FileName;
cmd.Parameters.Add("@image2", SqlDbType.NVarChar).Value = ss;
string s = Server.MapPath("franchimag");
s = s + "/" + f3.FileName;
f3.SaveAs(s);
}
else
{
string lb = ((Label)(GridView1.Rows[e.RowIndex].FindControl("Label110"))).Text;
cmd.Parameters.Add("@image2", SqlDbType.NVarChar).Value = lb;
}
cmd.Parameters.Add("@c_name", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox5"))).Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
datashow();
}
catch (Exception)
{
}
}
}
No comments:
Post a Comment