Monday 30 January 2012

insert Date Using spilt() Function and retrive Date in different Date,Month and Year Format

Source Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dateand spilt.aspx.cs" Inherits="Dateand_spilt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Roll"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label2" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label3" runat="server" Text="DOB"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
   
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" />
        <asp:Button ID="Button2" runat="server" Text="Year/Month/Date"
            onclick="Button2_Click" />
        <asp:Button ID="Button3" runat="server" onclick="Button3_Click"
            Text="Date/Month/Year" />
        <asp:Button ID="Button4" runat="server" onclick="Button4_Click"
            Text="Month/Day/Year/Time" />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <br />
   
    </div>
  
    </form>
</body>
</html>

Page Design

C# Coading
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.Collections;

public partial class Dateand_spilt : System.Web.UI.Page
{
    Class1 obj;
    SqlConnection con = new SqlConnection("server=Satya\\12;database=Linku;uid=li;pwd=satya2011");
    SqlDataAdapter da;
    SqlCommand cmd;
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            con.Open();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int x = 0;
        string[] date = TextBox3.Text.Split(new char[] { '/' });//Sending date after Split
        string Newdate = date[1] + "/" + date[0] + "/" + date[2];
        string query = "Insert into Demo1 values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+Newdate+"')";
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        cmd = new SqlCommand(query,con);
        cmd.ExecuteNonQuery();
     
        if (x < 0)
        {
            Response.Write("Data inserted Successfully");
        }
        else
        {
            Response.Write("Sorry Not Successfully");
        }

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string sel = "select Roll,Name,CONVERT(varchar(20), DOB, 111) AS VoucherDate from Demo1";//Example date retrive [Year/Month/Date]
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(sel,con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string sel = "select Roll,Name,CONVERT(varchar(20), DOB, 103) AS VoucherDate from Demo1";//Example date retrive [Day/Month/Year]
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(sel, con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        string sel = "select * from Demo1";//Example date retrive [Month/Day/Year/Time]
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter(sel, con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}

Table Design

No comments:

Post a Comment