Hi vrindavani,
I have created a sample. please take its reference.
HTML
login.aspx
UserName:<asp:TextBox ID="txtUsername" runat="server" /><br /><br />
Password:<asp:TextBox ID="txtPassword" runat="server" /><br /><br />
<asp:Button ID="btnLogin" Text="Login" runat="server" OnClick="OnLogin" />
Video.aspx
<asp:Button ID="btnStart" Text="Start" runat="server" OnClick="OnStart" /><br /><br />
Start Time:<asp:Label ID="lblStartTime" Text="" runat="server" /><br />
End Time:<asp:Label ID="lblEndTime" Text="" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
Login.aspx.cs
protected void OnLogin(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT UserID FROM Users WHERE Username=@UserName AND Password=@Password", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UserName", txtUsername.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
string userid = Convert.ToString(cmd.ExecuteScalar());
con.Close();
if (!string.IsNullOrEmpty(userid))
{
Session["UserID"] = userid;
Response.Redirect("Video.aspx");
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Incorrect UserName or Password..!!')", true);
}
}
}
}
Video.aspx.cs
protected void OnStart(object sender, EventArgs e)
{
if (btnStart.Text == "Start")
{
DateTime startTime = DateTime.Now;
btnStart.Text = "Stop";
lblStartTime.Text = startTime.ToString("dd/MM/yyyy hh:mm:ss");
}
else if (btnStart.Text == "Stop")
{
DateTime stopTime = DateTime.Now;
btnStart.Text = "Start";
lblEndTime.Text = stopTime.ToString("dd/MM/yyyy hh:mm:ss");
string videoId = "";
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT v_id FROM Videos WHERE userid=@uId", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@uId", Session["UserID"].ToString());
con.Open();
videoId = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Times VALUES(@UID,@VId,@StartTime,@EndTime)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@UID", Session["UserID"].ToString());
cmd.Parameters.AddWithValue("@VId", videoId);
cmd.Parameters.AddWithValue("@StartTime", lblStartTime.Text);
cmd.Parameters.AddWithValue("@EndTime", lblEndTime.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Login.aspx.vb
Protected Sub OnLogin(ByVal sender As Object, ByVal e As EventArgs)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT UserID FROM Users WHERE Username=@UserName AND Password=@Password", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@UserName", txtUsername.Text.Trim())
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim())
con.Open()
Dim userid As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
If Not String.IsNullOrEmpty(userid) Then
Session("UserID") = userid
Response.Redirect("Video.aspx")
Else
ClientScript.RegisterClientScriptBlock(Me.GetType(), "", "alert('Incorrect UserName or Password..!!')", True)
End If
End Using
End Using
End Sub
Video.aspx.vb
Protected Sub OnStart(ByVal sender As Object, ByVal e As EventArgs)
If btnStart.Text = "Start" Then
Dim startTime As DateTime = DateTime.Now
btnStart.Text = "Stop"
lblStartTime.Text = startTime.ToString("dd/MM/yyyy hh:mm:ss")
ElseIf btnStart.Text = "Stop" Then
Dim stopTime As DateTime = DateTime.Now
btnStart.Text = "Start"
lblEndTime.Text = stopTime.ToString("dd/MM/yyyy hh:mm:ss")
Dim videoId As String = ""
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT v_id FROM Videos WHERE userid=@uId", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@uId", Session("UserID").ToString())
con.Open()
videoId = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Times VALUES(@UID,@VId,@StartTime,@EndTime)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@UID", Session("UserID").ToString())
cmd.Parameters.AddWithValue("@VId", videoId)
cmd.Parameters.AddWithValue("@StartTime", lblStartTime.Text)
cmd.Parameters.AddWithValue("@EndTime", lblEndTime.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
End Sub