-->

Monday, May 26, 2014

How to Declare Member Variables in Classes: JAVA

A class’s state is represented by its member variables. You declare a class’s member variables in the body of the class. Typically, programmer declare a class’s variables before you declare its methods, although this is not required.

classDeclaration {
        member variable declarations
        method declarations
}
To declare variables that are members of a class, the declarations must be within the class body, but not within the body of a method. Variables declared within the body of a method are local to that method i.e., available and accessible only inside the method.

Types


  • Class variable (static variable): A data member that is declared once for a class. All objects of the class type, share these data members, as there is single copy of them available in memory. The class variables are declared by adding keyword static in front of a variable declaration.
  • Instance variable: A data member that is created for every object of the class. For example, if there are 10 objects of a class type, there would be 10 copies of instance variables, one each for an object.
Consider the following code.


Public class sample {
        int anInt; // instance variable
        float aFloat; // instance variable
        static float anotherFloat; // class variable
};

Suppose there are five objects created for class type Sample. Then there would be five copies of variables anInt and aFloat but there would be one copy of variable anotherFloat which all five objects can share.

Notice that class variables are declared by adding a keyword static before the variable declaration. Keyword static in the variable declaration makes it class variable.

Method are similar: your classes can have instance methods and class methods. Instance methods operate on the current object’s instance variables but also have access to the class variables. Class methods (also called static methods), on the other hand, cannot access the instance variables declared within the class (unless they create a new object and access them through the object). Also, class methods can be invoked on the class, you don’t need an instance to call a class method.

A class method can be invoked by:

  • Just its name within its own class e.g., check( ).
  • Class-name.method-name outside its class e.g., Sample.check ( )

In above two lines, we assume that method check( ) is a static method in class Sample.

Implement Object-Oriented in JAVA

Fix A table control was not found in the template for 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" CellPadding="0" ExtractTemplateRows="True">
        <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>


solved code

<!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" CellPadding="0" ExtractTemplateRows="False">
        <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>

Solution of the problem

set ExtractTemplateRows="False" in data list, like
<asp:DataList ID="DataList1" runat="server" DataKeyField="Sno" 
        DataSourceID="SqlDataSource1" CellPadding="0" ExtractTemplateRows="False">


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#
© Copyright 2013 Computer Programming | All Right Reserved