Thursday, 23 May 2013

Imp Work


hi every body
I use formview control and i want to insert date only (yyyy/mm/dd) without time by using calendar controls but in database it was inserted like this (yyyy/mm/dd 00:00:00)
could u please help me how to do that?
and code it is very long so i doesn't write it.
Re: how to insert format dates(yyy/mm/dd)in formview
Posted 23 January 2011 - 07:52 AM

1. Why would you store a date in the database as a varchar?

2. If you are using SQL Server 2008, you can use the Date type, which will only store the date and not the time.

3. If you can't change the SQL datatype, then when you save the data to the database,
 do this...



string dateVariable = calendar.Value.ToString("yyyy/MM/dd");





, then save the variable's data.


DateTime (example: 5/22/2013 7:34:10 AM)
DateTime.ToString("d")5/22/2013
DateTime.ToString("D")Wednesday, May 22, 2013
DateTime.ToString("t")7:34 AM
DateTime.ToString("T")7:34:10 AM
DateTime.ToString("f")Wednesday, May 22, 2013 7:34 AM
DateTime.ToString("F")Wednesday, May 22, 2013 7:34:10 AM
DateTime.ToString("g")5/22/2013 7:34 AM
DateTime.ToString("G")5/22/2013 7:34:10 AM
DateTime.ToString("M")May 22
DateTime.ToString("m")May 22
DateTime.ToString("R")Wed, 22 May 2013 07:34:10 GMT
DateTime.ToString("r")Wed, 22 May 2013 07:34:10 GMT
DateTime.ToString("s")2013-05-22T07:34:10
DateTime.ToString("u")2013-05-22 07:34:10Z
DateTime.ToString("U")Wednesday, May 22, 2013 11:34:10 AM
DateTime.ToString("Y")May, 2013
DateTime.ToString("y")May, 2013
DateTime.ToString("dd")22
DateTime.ToString("ddd")Wed
DateTime.ToString("dddd")Wednesday
DateTime.ToString("fff")748
DateTime.ToString("gg")A.D.
DateTime.ToString("hh")07
DateTime.ToString("HH")07
DateTime.ToString("mm")34
DateTime.ToString("MM")05
DateTime.ToString("MMM")May
DateTime.ToString("MMMM")May
DateTime.ToString("ss")10
DateTime.ToString("tt")AM
DateTime.ToString("yy")13
DateTime.ToString("yyyy")2013
DateTime.ToString("zz")-04
DateTime.ToString("zzz")-04:00 Double (example: 19.99)
Double.ToString("c")$19.99
Double.ToString("e")1.999000e+001
Double.ToString("f")19.99
Double.ToString("g")19.99
Double.ToString("n")19.99
Double.ToString("r")19.99
Double.ToString("0")20
Double.ToString("#")20
Double.ToString(".")20
Double.ToString(",.")20
Double.ToString("%")% Integer (example: 12400)
Int32.ToString("d")12400
Int32.ToString("x")3070


http://www.devmanuals.com/tutorials/ms/aspdotnet/insertdataindatabse.html

http://jwcooney.com/2012/09/10/asp-net-choosing-parameters-add-or-parameters-addwithvalue/

http://www.aspforums.net/Threads/187904/Show-only-date-and-remove-the-time-part-in-BoundField-or-ItemTemplate-Eval-of-ASPNet-GridView/

http://njrathod.wordpress.com/2009/05/07/aspnet-gridview-display-only-date-and-only-time-liek-1115-pm/

http://www.c-sharpcorner.com/forums/thread/152144/how-to-dynamic-increment-with-character-for-example-e001.aspx



Create table employee(empid varchar(50),empname varchar(50),sal int)


Default.aspx code



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>



<!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>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

 

        <asp:Label ID="Label1" runat="server" Text="EmpId" Font-Bold="True"

            Width="100px"></asp:Label>

    <asp:TextBox ID="txt_empid" runat="server"></asp:TextBox><br />

    <asp:Label ID="Label2" runat="server" Text="EmpName" Font-Bold="True" Width="100px"></asp:Label>

    <asp:TextBox ID="txt_empname" runat="server"></asp:TextBox><br />

    <asp:Label ID="Label3" runat="server" Text="Salary" Font-Bold="True" Width="100px"></asp:Label>

    <asp:TextBox ID="txt_sal" runat="server"></asp:TextBox><br />

    </div>

 

    <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click"

        Text="Insert Data" /><br />

    <asp:Label ID="Label4" runat="server"></asp:Label>

 

    </form>

</body>

</html>


Default.aspx.cs code



using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;

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

{

    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    string str;

    SqlCommand com;

    int count;

    protected void Page_Load(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection(strConnString);

        str = "select count(*) from employee";

        com = new SqlCommand(str, con);

        con.Open();

        count = Convert.ToInt16(com.ExecuteScalar()) + 1;

        txt_empid.Text = "E000" + count;

        con.Close();

    }

    protected void btn_insert_Click(object sender, EventArgs e)

    {

        SqlConnection con = new SqlConnection(strConnString);

        con.Open();

        str = "insert into employee values('" + txt_empid.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";

        com = new SqlCommand(str, con);

        com.ExecuteNonQuery();

        con.Close();

        Label4.Text = "Records successfully Inserted";

    }

}

Thanks
If this post helps you mark it as answer

No comments:

Post a Comment