MANU BAR

banner image
banner image

Working with Date in asp.net using sql server

 Working with Date in asp.net using sql server 

Create Table: 


Design From:-

A) Add 1 Label and 3 Textbox and 2 Button

B) Add gridview

C) Add Custom Validation Control


Code:-

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Globalization;

using System.Configuration;

using System.Data.SqlClient;

using System.Data;

namespace WorkingWithDateTime

{

    public partial class DateExamples : System.Web.UI.Page

    {

        static Boolean validdate;

        static Boolean validdate1;

     

 

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)

{

DateTime d;

args.IsValid = DateTime.TryParseExact(args.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None,out d);

validdate = DateTime.TryParseExact(args.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);

}   

protected void Button1_Click(object sender, EventArgs e)

{

if (validdate ==true)

{

string constring = ConfigurationManager.ConnectionStrings["dbcss"].ConnectionString;

SqlConnection con = new SqlConnection(constring);

SqlCommand cmd = new SqlCommand("insert into StdTbl (Name,DOJ) values (@Name,Convert(date,@DOJ,103))", con);

 //  cmd.CommandType = CommandType.StoredProcedure;

 cmd.Parameters.AddWithValue("@Name", TextBox2.Text);

 cmd.Parameters.AddWithValue("@DOJ",TextBox1.Text);

                try

                {

                    con.Open();

                    int a = cmd.ExecuteNonQuery();

                    con.Close();

                    if (a > 0)

                    {

    Response.Write("Record Inserted Successfully");

                    }

                    else

                    {

                        Response.Write("Record not Inserted.....");

                    }

                }

                catch (Exception ex)

                {

Response.Write("Please Type Date Like dd/MM/yyyy Format"+ ex.Message);

                }

          

            }

        }

 

        protected void Button2_Click(object sender, EventArgs e)

        {

            if (validdate1 == true)

            {

 

string constring = ConfigurationManager.ConnectionStrings["dbcss"].ConnectionString;

SqlConnection con = new SqlConnection(constring);

SqlCommand cmd = new SqlCommand("select Name, Convert(varchar,DOJ,105)as DOJ from StdTbl where DOJ=Convert(date,@DOJ,103)", con);

                //  cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@DOJ", TextBox3.Text);

                try

                {

                    con.Open();              

                    SqlDataAdapter da = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    da.Fill(dt);

                    GridView1.DataSource = dt;

                    GridView1.DataBind();

                    con.Close();

 

                    if (dt.Rows.Count>0)

                    {

                        Label1.Text="Record Search Successfully";

 

                    }

                    else

                    {

                       Label1.Text="Record not Found.....";

                    }

                }

                catch (Exception ex)

                {

                    Label1.Text = ex.Message;

                }

            }

        }

 

protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)

        {

            DateTime d;

args.IsValid = DateTime.TryParseExact(args.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);

validdate1 = DateTime.TryParseExact(args.Value, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out d);

        }

    }

}


AddCode: Web.Config File:-


 <connectionStrings>

    <add name="dbcs" connectionString="Data Source=CSLODHI-PC\SQLEXPRESS;Initial Catalog=WorkingDate;Integrated Security=True" providerName="System.Data.SqlClient" />

  </connectionStrings>

 

 Form Design Code:-


    

    </div>

    <asp:GridView ID="GridView1" runat="server">

    </asp:GridView>

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    <br />

    <br />

    Start Date:

    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

&nbsp; End Date&nbsp;

    <asp:TextBox ID="TextBox5" runat="server" style="height: 22px"></asp:TextBox>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Search" />

    <br />

    <asp:CustomValidator ID="CustomValidator3" runat="server" 

        ControlToValidate="TextBox4" ErrorMessage="dd/MM/yyyy Format" 

        onservervalidate="CustomValidator3_ServerValidate"></asp:CustomValidator>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <asp:CustomValidator ID="CustomValidator4" runat="server" 

        ControlToValidate="TextBox5" ErrorMessage="dd/MM/yyyy Format" 

        onservervalidate="CustomValidator4_ServerValidate"></asp:CustomValidator>

    <br />

    <br />

    <asp:GridView ID="GridView2" runat="server">

    </asp:GridView>

    <br />

    <br />

    <br />

    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>

    </form>


Working with Date in asp.net using sql server Working with Date in asp.net using sql server Reviewed by JOB ORIENTED STUDY ACADEMY on February 02, 2021 Rating: 5

No comments:

Powered by Blogger.