Solved

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

Posted on 2012-04-10
1
754 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 45
SQL Server Generate Scripts Fails 5 34
Inserting oldest record into new table. 5 22
optimize stored procedure 6 23
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

805 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