NOTE:-
CODE:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CRUD_ASP.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 600px;
border: 1px solid #800000;
background-color: #FFFFCC;
}
.auto-style2 {
width: 150px;
}
.auto-style4 {
width: 150px;
height: 31px;
}
.auto-style7 {
width: 509px;
}
.auto-style8 {
width: 509px;
height: 31px;
}
.auto-style9 {
width: 438px;
}
.auto-style10 {
width: 438px;
height: 31px;
}
.auto-style11 {
position: relative;
top: 1px;
left: 4px;
z-index: 1;
}
</style>
<script>
function isDataInserted()
{
confirm("Data Inserted successfully");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Text="First Name" Width="150px"></asp:Label>
</td>
<td class="auto-style9">
<asp:TextBox ID="FirstNameTextBox" runat="server" Width="200px"></asp:TextBox>
</td>
<td class="auto-style7">
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label2" runat="server" Text="Last Name" Width="150px"></asp:Label>
</td>
<td class="auto-style9">
<asp:TextBox ID="LastNameTextBox" runat="server" Width="200px"></asp:TextBox>
</td>
<td class="auto-style7">
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label3" runat="server" Text="Email" Width="150px"></asp:Label>
</td>
<td class="auto-style9">
<asp:TextBox ID="EmailTextBox" runat="server" Width="200px"></asp:TextBox>
</td>
<td class="auto-style7">
</td>
</tr>
<tr>
<td class="auto-style4">
<asp:Label ID="LanguagesLabel" runat="server" Text="Languages" Width="150px"></asp:Label>
</td>
<td class="auto-style10">
<asp:CheckBoxList ID="LanguagesCheckBoxList" runat="server" OnSelectedIndexChanged="SubmitButton_Click" RepeatDirection="Horizontal" Width="200px">
<asp:ListItem>VB.NET</asp:ListItem>
<asp:ListItem>Java</asp:ListItem>
<asp:ListItem>C#</asp:ListItem>
</asp:CheckBoxList>
</td>
<td class="auto-style8">
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label4" runat="server" Text="Gender" Width="150px"></asp:Label>
</td>
<td class="auto-style9">
<asp:RadioButton ID="MaleRadioButton" runat="server" GroupName="Sex" Text="Male" />
<asp:RadioButton ID="FemaleRadioButton" runat="server" GroupName="Sex" Text="Female" />
</td>
<td class="auto-style7">
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label5" runat="server" Text="Date of Birth" Width="150px"></asp:Label>
</td>
<td class="auto-style9">
<asp:TextBox ID="DobTextBox" runat="server" Width="200px"></asp:TextBox>
</td>
<td class="auto-style7">
</td>
</tr>
<tr>
<td class="auto-style2" style="vertical-align: top;">
</td>
<td class="auto-style9">
<asp:Calendar ID="Calendar1" runat="server" BackColor="White" BorderColor="#999999" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px" SelectedDate="<%# DateTime.Today %>" CellPadding="4" OnSelectionChanged="Calendar1_SelectionChanged">
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<NextPrevStyle VerticalAlign="Bottom" />
<OtherMonthDayStyle ForeColor="#808080" />
<SelectedDayStyle BackColor="#666666" Font-Bold="True" ForeColor="White" />
<SelectorStyle BackColor="#CCCCCC" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<WeekendDayStyle BackColor="#FFFFCC" />
</asp:Calendar>
</td>
<td class="auto-style7">
</td>
</tr>
<tr>
<td class="auto-style2" style="vertical-align: top;">
<asp:Label ID="Label6" runat="server" Text="Time of Birth" Width="150px"></asp:Label>
</td>
<td class="auto-style9">
Hr:<asp:DropDownList ID="DropDownListHr" runat="server">
</asp:DropDownList>
Min:<asp:DropDownList ID="DropDownListMin" runat="server">
</asp:DropDownList>
<asp:DropDownList ID="DropDownListAMPM" runat="server" CssClass="auto-style11">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="auto-style2" style="vertical-align: top;">
</td>
<td class="auto-style9">
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td class="auto-style9">
<asp:Button ID="SubmitButton" runat="server" Text="Submit" Width="208px" BackColor="Aqua" ForeColor="Maroon" OnClick="SubmitButton_Click" UseSubmitBehavior="False" />
</td>
<td class="auto-style7">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
NOTE:-
CODE:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
namespace CRUD_ASP
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Calendar1.Visible = false;
Response.Write("<h2>iNSERT DATA: DropDown control<h2>");
List<int> hours = new List<int>();
DropDownListHr.DataSource = hours;
for (int i = 1; i <= 12; i++)
{
DropDownListHr.Items.Add(i.ToString());
}
List<int> minutes = new List<int>();
for (int i = 0; i < 60; i++)
{
DropDownListMin.Items.Add(i.ToString());
}
DropDownListAMPM.Items.Add("AM");
DropDownListAMPM.Items.Add("PM");
DropDownListAMPM.SelectedIndex = 0;
}
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
//no validation of textbox etc.
string ampm = DropDownListAMPM.SelectedValue;
int hour = Convert.ToInt32( DropDownListHr.SelectedValue);
if (ampm=="PM")
{
hour += 12;
}
int minute = Convert.ToInt32(DropDownListMin.SelectedValue);
DateTime dt = new DateTime(9999, 1, 1);
DateTime Tob = dt.AddHours(hour).AddMinutes(minute).AddSeconds(0);
string firstname = FirstNameTextBox.Text;
string lastname = LastNameTextBox.Text;
string email = EmailTextBox.Text;
string languages = "";
DateTime Dob = Convert.ToDateTime(DobTextBox.Text);
foreach (System.Web.UI.WebControls.ListItem item in LanguagesCheckBoxList.Items)
{
if (item.Selected)
{
languages += item.ToString();
}
}
bool IsVB = (languages.IndexOf("VB.NET") != -1) ? true : false;
bool IsJava = (languages.IndexOf("Java") != -1) ? true : false;
bool IsCSharp = (languages.IndexOf("C#") != -1) ? true : false;
int Gender = MaleRadioButton.Checked ? 1 : (FemaleRadioButton.Checked ? 2 : 0);
if (Gender == 0)
{
string scriptValue = "alert('Please select Gender.');";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ScriptKeyName", scriptValue, true);
}
else
{
string constring = ConfigurationManager.ConnectionStrings["csx"].ConnectionString;
try
{
using (SqlConnection conn = new SqlConnection(constring))
{
string query = "INSERT INTO tbl_Employee (FirstName,LastName,Email, IsVB, IsJava, IsCSharp, Gender, DOB,TOB) VALUES( @FirstName, @LastName, @Email,@IsVB, @IsJava, @IsCSharp,@Gender,@Dob,@Tob)";
using (SqlCommand command = new SqlCommand(query, conn))
{
command.Parameters.AddWithValue("@FirstName", firstname);
command.Parameters.AddWithValue("@LastName", lastname);
command.Parameters.AddWithValue("@Email", email);
command.Parameters.AddWithValue("@IsVB", IsVB);
command.Parameters.AddWithValue("@IsJava", IsJava);
command.Parameters.AddWithValue("@IsCSharp", IsCSharp);
command.Parameters.AddWithValue("@Gender", Gender);
command.Parameters.AddWithValue("@Dob", Dob);
command.Parameters.AddWithValue("@Tob", Tob);
conn.Open();
command.ExecuteNonQuery();
string script = "window.onload = function(){ alert('Details inserted successfully.')};";
ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
DateTime selected_date = Calendar1.SelectedDate;
string date = selected_date.Date.Date.ToShortDateString();
DobTextBox.Text = date;
}
protected void ImageCalendarButton_Click(object sender, ImageClickEventArgs e)
{
Calendar1.Visible = !Calendar1.Visible;
}
}
}
© अजीत कुमार, सर्वाधिकार सुरक्षित।
इस आलेख को उद्धृत करते हुए इस लेख के लिंक का भी विवरण दें। इस आलेख को कॉपीराइट सूचना के साथ यथावत साझा करने की अनुमति है। कृपया इसे ऐसे स्थान पर साझा न करें जहाँ इसे देखने के लिए शुल्क देना पडे।
No comments:
Post a Comment