Solved

asp.net, C# Insert date into database table on Page_Load

Posted on 2012-04-10
1
751 Views
Last Modified: 2012-04-12
Hello,
I have a page that contains a lable which displays the current date and time.
I'm attempting to insert the date and time displayed on this label to a database table on Page_Load.

My *.aspx code follows:
----------------------
<table>
<tr>
<td colspan="2" style="width:100%; vertical-align:top; text-align:left;">&nbsp;
<asp:Label ID="lblHits" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2" style="width:100%; vertical-align:top; text-align:left;">&nbsp;
<asp:Label ID="lblHitCounterDate" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
----------------------------------

My codebehind follows:

---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Configuration;
using System.Collections;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net.Mail;
using System.IO;

  string strConnStringHitCounterNew = ConfigurationSettings.AppSettings["HitCounterNew"];

 protected void Page_Load(object sender, EventArgs e)
        {

            //objects we will need to work with the db    
            SqlConnection conn;
            SqlCommand cmd;    

            lblHitCounterDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ");

            if (!Page.IsPostBack)
            {
                //connect to the db        
                conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["HitCounterNew"].ConnectionString);

                //the sql command to increment hits by 1        
                cmd = new SqlCommand("UPDATE HitCounterNew SET HitCounter = HitCounter+1 WHERE PageName=@PageName", conn);

                cmd.CommandType = CommandType.Text;
                //update where Name is 'Default' which corresponds to this page        
                cmd.Parameters.AddWithValue("@PageName", "warrants_search_results");
               
                using (conn)
                {
                    //open the connection            
                    conn.Open();

                    string insertSQL = "INSERT INTO HitCounterNew (";
                    insertSQL += "HitCounterDate)";
                    insertSQL += "VALUES (";
                    insertSQL += "@HitCounterDate)";
                    cmd.Parameters.AddWithValue("@HitCounterDate", lblHitCounterDate.Text);

                    //send the query            
                    cmd.ExecuteNonQuery();
                }

            }

            //DISPLAY HITS IN THE LABEL    
            //connect to the db    
            conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["HitCounterNew"].ConnectionString);
            //the sql command to select the row of hits corresponding to this page    
            cmd = new SqlCommand("SELECT * FROM HitCounterNew WHERE PageName=@PageName", conn);
            cmd.CommandType = CommandType.Text;
            //select where Name is 'Default' which corresponds to this page    
            cmd.Parameters.AddWithValue("@PageName", "warrants_search_results");
            using (conn)
            {   //open the connection        
                conn.Open();
                //send the query and store the results in a sqldatareader        
                SqlDataReader rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    //set the text of our label to the current # of hits            
                    lblHits.Text = "Counter Pageviews: - " + rdr["HitCounter"].ToString();
                    lblHitCounterDate.Text = "HitCounter Date: - " + rdr["HitCounterDate"];
                }
            }
   
                     
       
            }
        }

------------------------
database table ( HitCounterNew)
database table structure as follows:

PageName (nvarchar(50))
HitCounter (int)
HitCounterDate (nvarchar(50))

---------------

ANY help in getting the date and time to write to the table "HitCounterNew" would be greatly appreciated.
0
Comment
Question by:Codeaddict7423
1 Comment
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37830091
using (conn)
                {
                    //open the connection            
                    conn.Open();

                    //send the query  -- To Update Hit Counter
                    cmd.ExecuteNonQuery();

                    //Another command To Store Date
                    cmd=new SQLCommand("Insert Into HitCounterNew (HitCounterDate) Values(@HitCounterDate)",conn);
                    cmd.Parameters.Add("@HitCounterDate",lblHitCounterDate.Text);
                    cmd.ExecuteNonQuery();

                }

Open in new window

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now