Here I have created sample that inserts EmployeeId and his/her Leaving date.
I hope this will help you out.
HTML
<form id="form1" runat="server">
<div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Label Text="Employee Id" runat="server" />
</td>
<td>
<asp:TextBox ID="txtEmployeeId" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Label Text="Leave Date" runat="server" />
</td>
<td>
<asp:TextBox ID="txtLeaveDate" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button Text="Save" ID="btnSave" runat="server" Width="72px" OnClick="SaveEmployeeDetails" />
</td>
</tr>
</table>
</div>
</form>
Namespaces
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.txtLeaveDate.Text = DateTime.Now.ToString();
}
}
private void SaveEmployeeDetails()
{
string constr = ConfigurationManager.ConnectionStrings["constrSample"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Employee_Leaves(EmpId,LeaveDate) VALUES(@EmpId, @LeaveDate)", con))
{
cmd.Parameters.AddWithValue("@EmpId", int.Parse(this.txtEmployeeId.Text.Trim()));
cmd.Parameters.AddWithValue("@LeaveDate", Convert.ToDateTime(this.txtLeaveDate.Text).ToString("yyyy-MM-dd"));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
protected void SaveEmployeeDetails(object sender, EventArgs e)
{
this.SaveEmployeeDetails();
}
SQL
CREATE TABLE [dbo].[Employee_Leaves](
[EmpId] [int] NULL,
[Name] [varchar](50) NULL,
[LeaveStatus] [int] NULL,
[LeaveDate] [datetime] NULL
) ON [PRIMARY]
GO