thank you for the reply :
what i want to achive is
|
File Upload control |
photo Column |
Update action |
if |
empty |
empty |
Do Nothing |
if |
empty |
file name |
keep the old file name |
if |
has file |
empty |
Add new file Name |
if |
has file |
has file |
Add new file Name |
the codes are below :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmployeeList.aspx.cs" Inherits="Admin_EmployeeList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 167px;
}
.auto-style3 {
width: 155px;
}
</style>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Panel ID="pnlEmployee" runat="server" Visible="false">
<table class="auto-style1">
<tr>
<td class="auto-style2">Employee Full Name :</td>
<td class="auto-style3">
<asp:TextBox ID="txtFullName" runat="server"></asp:TextBox>
</td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">Gender:</td>
<td class="auto-style3">
<asp:RadioButtonList runat="server" ID="rblGender" AutoPostBack="true">
<asp:ListItem Text="Male" Value="Male"></asp:ListItem>
<asp:ListItem Text="Female" Value="Female"></asp:ListItem>
</asp:RadioButtonList>
</td>
<td class="auto-style3"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">Place Of Birth</td>
<td class="auto-style3">
<asp:DropDownList ID="ddlPOB" runat="server" Height="16px" Width="117px">
</asp:DropDownList>
</td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">Employee Photo</td>
<td class="auto-style3">
<asp:FileUpload ID="fuPhoto" runat="server" />
</td>
<td class="auto-style3" rowspan="4">
<asp:Image ID="Emp_img" runat="server" Height="139px" ImageUrl="~/img/emp_img.png" Width="147px" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3">
<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click1" Text="Upload Photo" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:Button ID="btnEdit" runat="server" Text="Edit Employee" OnClick="btnEdit_Click" />
</td>
<td class="auto-style3">
</td>
</tr>
</table>
</asp:Panel>
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<br />
</div>
<div>
<asp:GridView ID="gvEmpList" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Emp_ID" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="gvEmpList_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="Emp_ID" HeaderText="Emp_ID" ReadOnly="True" SortExpression="Emp_ID" />
<asp:BoundField DataField="Emp_Name" HeaderText="Emp_Name" SortExpression="Emp_Name" />
<asp:BoundField DataField="Emp_Gender" HeaderText="Emp_Gender" SortExpression="Emp_Gender" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#FEDC45" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Test_Connection %>" SelectCommand="SPa_GetEmpLst" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
the code behind page is like below :
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Admin_EmployeeList : System.Web.UI.Page
{
#region Connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test_Connection"].ConnectionString);
#endregion Connection
protected void Page_Load(object sender, EventArgs e)
{
}
#region ClassEnsureDirectory
public void EnsureDirectoriesExist()
{
// if the \pix directory doesn't exist - create it.
if (!System.IO.Directory.Exists(Server.MapPath(@"~/img/emp_photo/")))
{
System.IO.Directory.CreateDirectory(Server.MapPath(@"~/img/emp_photo/"));
}
}
#endregion ClassEnsureDirectory
protected void btnUpload_Click(object sender, EventArgs e)
{
}
protected void gvEmpList_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Grid_ID"] = gvEmpList.SelectedDataKey.Value;
try
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SP_GetEmpDetails";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Emp_ID", Convert.ToInt32(Session["Grid_ID"]));
con.Open();
SqlDataReader My = cmd.ExecuteReader();
if (My.Read())
{
txtFullName.Text = My["Emp_Name"].ToString();
rblGender.SelectedValue = My["Emp_Gender"].ToString();
Emp_img.ImageUrl = @"~/img/emp_photo/" + My["Emp_Photo"].ToString();
pnlEmployee.Visible = true;
}
con.Close();
}
catch
{
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
Session["Grid_ID"] = gvEmpList.SelectedDataKey.Value;
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SP_EditEmpDetails";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Emp_Name", txtFullName.Text);
cmd.Parameters.AddWithValue("@Emp_ID", Convert.ToInt32(Session["Grid_ID"]));
cmd.Parameters.AddWithValue("@Emp_Gender", rblGender.SelectedValue);
//cmd.Parameters.AddWithValue("@Emp_POB", ddlPOB.SelectedValue);
cmd.Parameters.AddWithValue("@Emp_Photo", Session["Photo_Path"] == null ? "" : Session["Photo_Path"]);
con.Open();
//SqlDataReader V = cmd.ExecuteReader();
cmd.ExecuteNonQuery();
lblmsg.Text = "employee updated!!";
con.Close();
gvEmpList.DataBind();
//Insert record here.
//Display success message and clear the form.
string message = "Your details have been saved successfully.";
string script = "window.onload = function(){ alert('";
script += message;
script += "');";
script += "window.location = '";
script += Request.Url.AbsoluteUri;
script += "'; }";
ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
protected void btnUpload_Click1(object sender, EventArgs e)
{
if (fuPhoto.HasFile)
{
EnsureDirectoriesExist();
string filepath = Server.MapPath(@"~/img/emp_photo/" + fuPhoto.FileName);
string filename = Path.GetFileName(fuPhoto.FileName);
fuPhoto.SaveAs(filepath);
//Session["Photo_Path"] = filepath;
//Image1.ImageUrl = @"~/img/emp_photo/" + fuPhoto.FileName;
Session["Photo_Path"] = filename;
//Session["Photo_Path"] = Server.MapPath(fuPhoto.FileName);
Emp_img.ImageUrl = @"~/img/emp_photo/" + fuPhoto.FileName;
}
else
{
lblmsg.Text = "not jpg image uploaded";
}
}
}
and the stored procedure is like below :
USE [TestDB]
GO
/****** Object: StoredProcedure [dbo].[SP_EditEmpDetails] Script Date: 06/12/2016 14:22:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SP_EditEmpDetails]
@emp_id int,
@emp_name nvarchar(50),
@emp_gender nvarchar(10),
@emp_photo nvarchar(100)
as
Begin
update tbl_Employee set Emp_Name= @Emp_Name,Emp_Gender=@emp_gender,Emp_Photo=@emp_photo
where
emp_id=@emp_id
end
here is the screen shoot of the table
regards