Skip to content

While click the link button will automatically insert the current date.

magesh manavalan

Asked Mar 29, 2012, 12:16 AM EDT1 answer
Share
LinkedInX
While click the link button will automatically insert the current date in my table how to do that?




Thanks&Regs

Magesh.A

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi,

    Create the table.

    CREATE TABLE Views
    (
    SNO INT IDENTITY(1,1) PRIMARY KEY
       ,UserID INT
       ,ViewedDate DATETIME DEFAULT GETDATE()
    )


    Design page:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ViewInsert.aspx.cs" Inherits="ViewInsert" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

       
       
       
       
                onclick="lbtnInsert_Click">
       
       


    Code File:

    C#
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    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;
    
    
    public partial class ViewInsert : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
    
        protected void lbtnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection sqlConnection = new SqlConnection();
                sqlConnection.ConnectionString = @"Data Source=SS438\SQLEXPRESS;Initial Catalog=Examples;Integrated Security=true;";
                sqlConnection.Open();
                string sqlQuery = "INSERT INTO Views(UserID) VALUES (@UserID)";
                SqlCommand sqlCommand = new SqlCommand(sqlQuery, sqlConnection);
                sqlCommand.CommandType = CommandType.Text;
                Random random = new Random(1);
                sqlCommand.Parameters.Add("UserID", SqlDbType.VarChar).Value = random.Next(10000);
                sqlCommand.ExecuteNonQuery();
                sqlConnection.Close();
                lblMessage.Text = "Inserted successfully...";
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }


    Hope this will help you...


    0