Dear dharmendr, thank you for reply
the html code is like below :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddEmployee.aspx.cs" Inherits="Admin_AddEmployee" %>
<!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="form1" runat="server">
<div>
<br />
<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:RadioButton ID="rbMale" runat="server" Text="Male" GroupName="Gender" OnCheckedChanged="rbMale_CheckedChanged" /></td>
<td class="auto-style3">
<asp:RadioButton ID="rdFemale" runat="server" GroupName="Gender" Text="Female" OnCheckedChanged="rdFemale_CheckedChanged" /></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="Image1" 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_Click" Text="Upload" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style3"> </td>
</tr>
<tr>
<td class="auto-style2">
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add Employee" />
</td>
<td class="auto-style3">
<asp:Button ID="btnEdit" runat="server" Text="Edit Employee" />
</td>
</tr>
</table>
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<br />
<br />
</div>
</form>
</body>
</html>
the code behind the page is like below :
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.IO;
using System.Data;
public partial class Admin_AddEmployee : System.Web.UI.Page
{
#region Connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test_Connection"].ConnectionString);
#endregion Connection
#region PageLoad
protected void Page_Load(object sender, EventArgs e)
{
}
#endregion PageLoad
#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
#region uploadphoto
protected void btnUpload_Click(object sender, EventArgs e)
{
if(fuPhoto.HasFile)
{
EnsureDirectoriesExist();
//string filepath = Server.MapPath(@"~/img/emp_photo/" + fuPhoto.FileName);
//Session["Photo_Path"] = filepath;
//Image1.ImageUrl = @"~/img/emp_photo/" + fuPhoto.FileName;
//fuPhoto.SaveAs(filepath);
Session["Photo_Path"] = fuPhoto.FileName;
Image1.ImageUrl = @"~/img/emp_photo/" + fuPhoto.FileName;
}
else
{
lblmsg.Text = "not jpg image uploaded";
}
}
#endregion uploadphoto
string Emp_Gender;
protected void btnAdd_Click(object sender, EventArgs e)
{
if (Session["Photo_Path"] != null)
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SP_AddEmp";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Emp_Name", txtFullName.Text);
cmd.Parameters.AddWithValue("@Emp_Gender",Emp_Gender);
cmd.Parameters.AddWithValue("@Emp_POB", ddlPOB.SelectedValue );
cmd.Parameters.AddWithValue("@Emp_Photo", Session["Photo_Path"].ToString());
con.Open();
SqlDataReader V = cmd.ExecuteReader();
con.Close();
lblmsg.Text = "employee added!!";
}
else
{
lblmsg.Text = "employee Not added!!";
}
}
protected void rbMale_CheckedChanged(object sender, EventArgs e)
{
Emp_Gender = "Male";
}
protected void rdFemale_CheckedChanged(object sender, EventArgs e)
{
Emp_Gender = "Female";
}
}
and the stored procedure
USE [TestDB]
GO
/****** Object: StoredProcedure [dbo].[SP_AddEmp] Script Date: 02/12/2016 18:56:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[SP_AddEmp]
@Emp_Name nvarchar(50),
@Emp_Gender nvarchar(10),
@Emp_POB int,
@Emp_Photo nvarchar(100)
AS
BEGIN
DECLARE @Emp_ID int
SET @Emp_ID = (select ISNULL(MAX(Emp_ID),0) + 1 from tbl_Employee)
insert into tbl_Employee (Emp_ID,Emp_Name,Emp_Gender,Emp_POB,Emp_Photo)
values (@Emp_ID,@Emp_Name,@Emp_Gender,@Emp_POB,@Emp_Photo)
END
now when i run this page i get this error :
Server Error in '/' Application.
Procedure or function 'SP_AddEmp' expects parameter '@Emp_Gender', which was not supplied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Procedure or function 'SP_AddEmp' expects parameter '@Emp_Gender', which was not supplied.
Source Error:
Line 85: cmd.Parameters.AddWithValue("@Emp_Photo", Session["Photo_Path"].ToString());
Line 86: con.Open();
Line 87: SqlDataReader V = cmd.ExecuteReader();
Line 88: con.Close();
Line 89: lblmsg.Text = "employee added!!";
Source File: e:\MyTestWebSite\Admin\AddEmployee.aspx.cs Line: 87
Stack Trace: