-->

Monday, May 26, 2014

How to change Gridview header's style like border style and color

Initial Steps for viewers
Step-1 : Open Visual Studio IDE
Step-2 : Add New Webform into your project
Step-3 : Open Design page for adding control onit.
Step-4 : You can change  Header's BorderStyle and BorderColor of it .



<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>

 <HeaderStyle BorderColor="Yellow" BorderStyle="Outset" />

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>


code output:



How to change back color of data list


How to change back color of data list



<!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></title>
    <style type="text/css">
        #form1
        {
            width: 162px;
            height: 384px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
<asp:DataList ID="DataList1" runat="server" DataKeyField="Sno" 
        DataSourceID="SqlDataSource1" BackColor="Red">
        <ItemTemplate>
            Sno:
            <asp:Label ID="SnoLabel" runat="server" Text='<%# Eval("Sno") %>' />
            <br />
            name:
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            <br />
            address:
            <asp:Label ID="addressLabel" runat="server" Text='<%# Eval("address") %>' />
            <br />
<br />
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [User] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [User] ([name], [address]) VALUES (@name, @address)" 
         SelectCommand="SELECT * FROM [User]" 
        UpdateCommand="UPDATE [User] SET [name] = @name, [address] = @address WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>



Friday, May 23, 2014

How to find minute difference between two times

If you want to get minute difference between two times then you should use TimeSpan Structure for that. First to create two DateTime object. Now, create difference between two dates using TimeSpan structure also get minutes using TotalMinutes property of TimeSpan structure.

Source Code

<form id="form1"
     runat="server">
    <asp:Button ID="Button1"
    runat="server"
    BackColor="#FF33CC"
    onclick="Button1_Click"
    Text="Click"
     Width="73px" />
    <div>  
    <asp:Label ID="Label1"
     runat="server"
     BackColor="Yellow"
     Text="Label"></asp:Label>  
    </div>
    </form>

Code Behind

protected void Button1_Click(object sender, EventArgs e)
    {
        {            
            DateTime now = DateTime.Now;
            Label1.Text = "now : " + now.ToString();
           DateTime dateAfter4Hours = now.AddHours(4);
            TimeSpan TS = dateAfter4Hours - now;            
            int minutes = (int)TS.TotalMinutes;
            Label1.Text += "<br ><br />after four hours: ";
            Label1.Text += dateAfter4Hours.ToString();
            Label1.Text += "<br ><br />This is difference between to datetime object in minutes  : ";
            Label1.Text += minutes;
        }

    }
Code Generate the following output
How to find minute difference between two times

How to get total days of month in asp.net c#

If you want to find total days in current month then you should use DayInMonth( ) method of DateTime structure. In this method you should pass the year and month, which you want to access total days of the month. Lets take an simple example to find total days of a month

Source Code

<form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1"
         runat="server"
         BackColor="#99CCFF"
         onclick="Button1_Click"
         Text="Click" />  
    </div>
       <asp:Label ID="Label1"
       runat="server"
       BackColor="#FFFF66"
       Text="Label"></asp:Label>
    </form>

Code Behind

protected void Button1_Click(object sender, EventArgs e)
        {          
            DateTime today = DateTime.Today;          
            int TotalOfDays = DateTime.DaysInMonth(today.Year, today.Month);
            Label1.Text = "today : " + today.ToLongDateString();
            Label1.Text += "<br /><br />Total days in month=";
            Label1.Text += TotalOfDays;

        }  
Code Generate the following output
How to get total days of month in asp.net c#

Thursday, May 22, 2014

How to make video and mp3 player in windows form c#

If you want to play audio file and video file in windows form c# project then you should go for windows media player control. This is unchecked component of toolbox bydefault. If you want to add media player into the list , check my previous article.
There are various step to design player in windows form, these are:
Step-1: Add menuStrip control on the form, add 'File' text in it as a root also design sub child of the root item like 'open' and 'add to the playlist'. Looking like

menuStrip control with items


Step-2 : Add Windows media player and ListBox on the form also set the properties of both controls
Window Media Player control --Name property = WindowsMediaPlayer1
ListBox Control -- Name property= listBox1

Step-3 : Double click on Open file menu item and handle raised event, also double click on Add to Playlist menu item.
Step-4 : Copy the code and use it
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace mediaplayer
{
    public partial class Form1 : Form
    {
        string[] path, filename;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog filedialog = new OpenFileDialog();
            if (filedialog .ShowDialog ()==DialogResult.OK)
            {
                WindowsMediaPlayer1.URL = filedialog.FileName;
                WindowsMediaPlayer1.Ctlcontrols.play();
                
            }
        }

        private void createPlaylistToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog filedialog = new OpenFileDialog();
            filedialog.Multiselect = true;
            if (filedialog .ShowDialog ()==DialogResult .OK)
            {
                filename = filedialog.SafeFileNames;
                path = filedialog.FileNames;
                for (int i = 0; i < filename.Length; i++)
                {
                    listBox1.Items.Add(filename[i]);
                    
                }
                
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            WindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
        }
    }
}
Code Generate the following output
How to make video and mp3 player in windows form c#

Tuesday, May 20, 2014

How to add Windows media player in toolBox windows form c#

Winforms toolbox does not contain windows media player bydefault. Suppose, you want to make video and mp3 player in c# then you should go for media player, which is not exist in toolbox. If you want to add it into the toolBox then follow these steps, which is given below.

If your toolbox does not contain media player

Step-1 : Right click on toolbox and select choose items..

choose items in vs toolbox
Step-2 : Now, appear a new window, select com component tab also check window media player in the list
select com component tab also check window media player in the list
Step-3 : Press 'ok button' and check window media player in the toolbox.
 window media player in the toolbox

Monday, May 19, 2014

Count number of visitor on website in asp.net c#

Introduction

If you want to count visitors, which is appear on your website. You can use Global.asax file for counting visitors. Google analytical tool is the best tool to count numbers of visitors, who is visit the site. I have simple demonstration of google analytical is:


  Now, create the simple code into c# for counting visitors.
Step-1 : Add Global.asax file into the project.
Step-2 : Add this code into  Application_Start block.

Application["visitcount"] = 0;

// In this code we create a application object, which hold 0. When application start by the server , This method is run once at a time.
Step-3 : Also add this code into Session_Start block.
 // Code that runs when a new session is started
        Application.Lock();
        Application["visitcount"] = (int)Application["visitcount"] + 1;
        Application.UnLock();
//  This method is raise on every post back event. The first line of code that is Application.Lock() define, only one client can access or modify the variable, which is stored in application object. The second line of code that is (Application["visitcount"] = (int)Application["visitcount"] + 1;) define, a application object incremented by one. In third line, locked application object will be unlocked.
Step-4 : Add new webform into the project, also take a label control on it.
Step-5 : On page_load event take the application object value onto the label text.

Complete Source code

<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>
    </form>
Code Behind
 protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Application["visitcount"].ToString();
    }
Global.asax file code
<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        Application["visitcount"] = 0;

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started
        Application.Lock();
        Application["visitcount"] = (int)Application["visitcount"] + 1;
        Application.UnLock();

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
       
</script>
Now, run the application. Code generate the following output
Count number of visitor on website in asp.net c#
© Copyright 2013 Computer Programming | All Right Reserved