Friday, 28 December 2012

How to go back to login page after session expires.

Wednesday, 26 December 2012

Write a program to ADD Two Matrix in C#

Yes here ,I am writing a program in C# to add two matrix.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class AddTwo_Matrix
    {
        static void Main(string[] args)
        {
            int[,] a = { { 1, 3, 4 }, { 2, 4, 7 }, { 3, 5, 6 } };
            int[,] b = { { 3, 5, 5 }, { 4, 5, 6 }, { 2, 7, 8 } };
            int[,] c = new int[3,3];
            int f = c.Length;
            int i, m = 0;
            int j = 0, n = 0;
            for (i = 0; i < 3; i++)
            {
                Console.WriteLine("");
                for (j = 0; j < 3; j++)
                {
                    Console.WriteLine(""+a[i,j]);
                }
            }
            Console.WriteLine("\n");
            for (m = 0; m < 3; m++)
            {
                Console.WriteLine("");
                for (n = 0; n < 3; n++)
                {
                    Console.WriteLine(""+b[m,n]);
                }
            }
            Console.WriteLine("\n");
            for (int k = 0; k < 3; k++)
            {
                Console.WriteLine("");
                for (int l = 0; l < 3; l++)
                {
                    Console.WriteLine(a[k,l]+b[k,l]+"\t");
                }
            }
            Console.WriteLine("yes it is Solved ,its very easy to add two matrix");
            Console.Read();

        }
    }
}

How to Find nth Salary and his id from a table in SQL server

I am writhing a query that will be helpful in finding nth salary and his id of that record.

so its very simple and very understandable.......

select ID ,salary from testing where salary in(select MIN(salary) from testing where
salary in (select top 4 salary from testing   order by salary desc))


Here, First we find the nth salary from the table in decreasing order and then we select min salary from that records means from it sub query.
and then can fetch id ,salary of the table from the sub query result as u seeing in query.
if you are unable to understand then please drop a comment at below..............
or mail me at Kushwaha.pankajkumar07@gmail.com

Tuesday, 25 December 2012

How To check Session authentication in asp.net

 I hope that it will be beneficial for everyone.....

protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtmajorhead.Text == "")
        {
            lbl_msg.Text = "User Name is required !";
        }
        else if (txtdescp.Text == "")
        {
            lbl_msg.Text = "Password is required !";
        }
        else
        {
            Parameter UserId = new Parameter(txtmajorhead.Text);
            Parameter Password = new Parameter(txtdescp.Text);
            SqlDataAdapter da = new SqlDataAdapter("select * from Login where UserID='" + UserId + "' and Password='" + Password + "'", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
           
            if (ds.Tables[0].Rows.Count > 0)
            {
                Session["UserID"] = ds.Tables[0].Rows[0]["UserID"].ToString();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if ((ds.Tables[0].Rows[i]["UserID"].ToString() == txtmajorhead.Text) && (ds.Tables[0].Rows[i]["Password"].ToString() == txtdescp.Text))
                    {
                        f = 1;

                        Response.Redirect("AddCategory.aspx");
                        break;
                    }
                }
            }
            if (f == 0)
            {

                lbl_msg.Text = "UserName And Password Are Incorrect";


            }
            txtmajorhead.Focus();

        }

Friday, 5 October 2012

How To Bind the Country, State and City in Asp.net

If you want to bind the Country and State and City in asp.net .....follow these Steps
1. Open Visual Studio 2010
2. New Project
3. And .aspx page Design will be like this or as per as your requirement

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="statebinding.aspx.cs" Inherits="WebApplication1.statebinding" %>

<!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">
        .style1
        {
            width: 100%;
            height: 62px;
        }
        .style2
        {
            width: 202px;
        }
        .style3
        {
            width: 202px;
            height: 34px;
        }
        .style4
        {
            height: 34px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <br />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
       
        </ContentTemplate>
        </asp:UpdatePanel>
   
    </div>
    <table class="style1">
        <tr>
            <td>
                <asp:Label ID="lblCountry" runat="server" Text="Country"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" Height="23px" Width="153px"
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged1">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblState" runat="server" Text="State"></asp:Label>
            </td>
            <td>
                <asp:DropDownList ID="DropDownList2" runat="server" Height="22px" Width="152px"
                    onselectedindexchanged="DropDownList2_SelectedIndexChanged">
                </asp:DropDownList>
            </td>
        </tr>
    </table>
    <table class="style1">
        <tr>
            <td class="style3">
                <asp:Label ID="lblCity" runat="server" Text="City"></asp:Label>
            </td>
            <td class="style4">
                <asp:DropDownList ID="DropDownList3" runat="server" Height="28px"
                    style="margin-left: 3px" Width="151px">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </form>
</body>
</html>
4.And Now Do the Code for aspx.cs File...............

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class statebinding : System.Web.UI.Page
    {
      // public string str="Data Source=PANKAJ-PC\\SQLEXPRESS2008;Initial Catalog=Data;User ID=sa;Password=sa2008";
       private SqlConnection con = new SqlConnection("Data Source=PANKAJ-PC\\SQLEXPRESS2008;Initial Catalog=Data;User ID=sa;Password=sa2008");
       public void BindCountry()
       {
           con.Open();
           SqlCommand com = new SqlCommand("Select County,CountryId  from Country",con);
           SqlDataReader ds = com.ExecuteReader();
           DropDownList1.DataSource = ds;
           DropDownList1.Items.Clear();
           DropDownList1.Items.Add("--Please Select Country--");
           DropDownList1.DataTextField = "County";
           DropDownList1.DataValueField = "CountryId";
           DropDownList1.DataBind();
           con.Close();
       }
       public void BindState()
       {
           con.Open();
           SqlCommand com1 = new SqlCommand("select State,StateId from countryState where CountryId='" + DropDownList1.SelectedValue +"'", con);
           SqlDataReader rd = com1.ExecuteReader();
           DropDownList2.DataSource = rd;
           DropDownList2.Items.Clear();
           DropDownList2.Items.Add("--Please select State--");
           DropDownList2.DataTextField = "State";
           DropDownList2.DataValueField = "StateId";
           DropDownList2.DataBind();
           con.Close();

       }
       public void citybind()
       {
           con.Open();
           SqlCommand com2 = new SqlCommand("Select * from stateCity where StateId='"+DropDownList2.SelectedValue+"'",con);
           SqlDataReader dr = com2.ExecuteReader();
           DropDownList3.DataSource = dr;
           DropDownList3.Items.Clear();
           DropDownList3.Items.Add("--Please select City--");
           DropDownList3.DataTextField = "City";
           DropDownList3.DataValueField = "CityId";
           DropDownList3.DataBind();
           con.Close();
       }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCountry();
            }

        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            citybind();
        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            BindState();
        }

    }
}

If you face any problem then Send feedback to me ............Thanks

Thursday, 4 October 2012

How To Bind Dropdownlist from Database in asp.net

Please follow these steps to bind the dropdown list in asp.net
1. Open Visual Studio 2010
2.New Project
3.Asp.net Web Application

Design the Default page like this:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 89px;
        }
        .style3
        {
            width: 89px;
            height: 41px;
        }
        .style4
        {
            height: 41px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    <asp:Label ID="Label1" runat="server" Text="Please Select"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" Height="22px" Width="152px">
                    </asp:DropDownList>
                &nbsp;<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                        Text="Button" />
                </td>
              
            </tr>
            <tr>
                <td class="style3">
                    </td>
                <td class="style4">
                    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
                </td>
                <td class="style4">
                    </td>
                          </tr>
        </table>
   
    </div>
    </form>
</body>
</html>








After that Code for the Default.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string connectionstring = "Data Source=PANKAJ-PC\\SQLEXPRESS2008;Initial Catalog=Data;User ID=sa;Password=sa2008";
            SqlConnection con = new SqlConnection(connectionstring);
            string com1="select * from pankaj";
            SqlDataAdapter ad = new SqlDataAdapter(com1,con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            DropDownList1.DataSource = dt;
            DropDownList1.DataBind();
            GridView1.DataSource = dt;
            GridView1.DataBind();
            DropDownList1.DataTextField = "Name";
            DropDownList1.DataValueField = "Pid";
            DropDownList1.DataBind();
            Label2.Visible = false;
          


        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string connectionstring = "Data Source=PANKAJ-PC\\SQLEXPRESS2008;Initial Catalog=Data;User ID=sa;Password=sa2008";
            SqlConnection conn = new SqlConnection(connectionstring);
            SqlCommand com = new SqlCommand("select * from pankaj where pid='"+DropDownList1.SelectedValue+"'",conn);
            SqlDataAdapter dt1 = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            dt1.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            Label2.Text = "Record Found According to Selection";
            Label2.Visible = true;
        }

      
    }
}
 

Output will look like this..............................................



Thursday, 27 September 2012

How to get text box value by using java Script



Add A text box to the Page and then

 <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="display()"/>

--JavaScript  Code--

  <script language="javascript" type="text/javascript">
        function display() {
            var text = document.getElementById('<%= TextBox1.ClientID %>').value;
            if (text==="") {
                alert("Please enter Something very carefully");
            }
            else {
                alert("Hi its good");
            }
        }
    </script>

Tuesday, 18 September 2012

How to check a number is Prime or no ?

By this Code You can check a number is Prime or not....and can you check static array values also..


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class primenumber
    {
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine("Please enter the nu,mber which u want to test");
            int number = Convert.ToInt32(Console.ReadLine());
            for ( i = 2; i < number; i++)
            {
                if (number % i == 0)
                {
                    Console.WriteLine("Number is not prime {0}", number);
                    break;
                }
            }
            if (number == i)
            {
                Console.WriteLine("Number is Prime {0}", number);
            }
            primenumber ob = new primenumber();
            ob.checkedi();
            Console.ReadKey();
        }
        public void checkedi()
        {
            int[] arr = {12,23,34,456,56,1243,123};
            for (int j = 0; j < arr.Length; j++)
            {
                for(int k=2;k<arr.Length; k++)
                {
                    if (arr[j] % k == 0)
                    {
                        Console.WriteLine("array number {0} is not prime", arr[j]);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Array number is {0} prime",arr[j]);
                        break;
                    }
                }
            }
        }
    }
}

Fibonacci Series code in c#




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Fibonacciseries
    {
        int prev = 0;
        int next = 1;
        public void fibonacci(int n)
        {
            for (int i = 0; i < n; i++)
            {
                int sum = prev + next;
                prev = next;
                next = sum;
                Console.WriteLine(sum);
               
            }
        }
       
        static void Main(string[] args)
        {
            Fibonacciseries ob = new Fibonacciseries();
            Console.WriteLine("Please enter the length of the series");
            ob.fibonacci(Convert.ToInt32(Console.ReadLine()));
            Console.ReadKey();
        }
     
    }
}

Thursday, 6 September 2012

How to add Twitter Follow Button to Asp.net Application using c#

If you want to add twitter follow button to your application in Asp.net
Then just do it.............

Add the given code in content place holder section where you want to add twitter button.


 <a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="true">Follow @twitterapi</a>
      <script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = "//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs); } } (document, "script", "twitter-wjs");</script> 


this code taken from twitter developer page. if you want to extra help then go to twitter developer page.

Inheritance using C#

Inheritance is mechanism by which a class can inherit the property of the Base class. A derived class can access all Non-private member of the base class.
Classes can inherit from another classThis is accomplished by putting a colon after the class name when declaring the class

public class A
{
    public A() { }
}

public class B : A
{
    public B() { }
}


Then gains all the non-private data and behavior of the base class in addition to any other data or behaviors it defines for itself.

Wednesday, 5 September 2012

Static Class in C#


A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
A Static class have these properties......
1.    Static class can’t be instantiated.
2.    Static class can contain only static members. It does not have any non static members
3.    Static class can’t contain Instance Constructor, because if any static constructor contain instance constructor then it will callable when any instance of the class is created.
For example:

public ClassName(int x, int y)
{
    this.x = x;
    this.y = y;
}

And this constructor will callable when it will be call as like this
This allows CoOrd objects to be created with default or specific initial values, like this:

ClassNamep2 = new ClassName(5, 3); // Static class does not allow to make an instance of the class. That’s why static class does not have any instance constructor.

4.    Static class does not inherited by any other derived class. Because they are implicitly sealed.

Tuesday, 4 September 2012

Add Share Button to Asp.net application


Drag and Drop a label inside of content place holder.
This label can drop any where in the page its totally depend on your choice.after that rename it as lblShare  and add the code at the page load event as given below.........
and Run your application ..........

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Label ID="lblShare" runat="server" Text=""></asp:Label>
</asp:Content>>

Code Behind.


using System.Web.UI.HtmlControls;
protected void Page_Load(object sender, EventArgs e)
{      
    lblShare.Text = "<a name=\"fb_share\" type=\"button\"></a>" +
                    "<script " +
                    "src=\"http://static.ak.fbcdn.net/connect.php/js/FB.Share\" " +
                    "type=\"text/javascript\"></script>";
    HtmlMeta tag = new HtmlMeta();
    tag.Name = "title";
    tag.Content = "This is the page title";
    Page.Header.Controls.Add(tag);
    HtmlMeta tag1 = new HtmlMeta();
    tag.Name = "description";
    tag.Content = "This is a page description.";
    Page.Header.Controls.Add(tag1);           
}


using this code you can share your page at Facebook. 

Code For Triangle Shape in C#

If you want to make a triangle shape using C# then just do it...........

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class testing
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = i; j < 10; j++)
                {
                    Console.Write(" ");
                }
                for (int k =i+1; k> 0; k--)
                {
                    Console.Write("*");
                }
                Console.WriteLine("");
       
            }
            Console.ReadKey();
        }
    }
}

Output of the Program will be like this.....


Wednesday, 29 August 2012

Code For Pyramid Shape in c#


Program to display a shape in form of Pyramid.............

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class pyramidagain
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)
            {

                for (int k = i; k < 10; k++)
                {
                    Console.Write(" ");
                }

                for (int j = 2 * i + 1; j > 0; j--)
                {
                    Console.Write("*");
                 
                }
                Console.WriteLine("");
             
            }
            Console.ReadKey();
        }
    }
}

Result of the Program will be as given below.....

Tuesday, 28 August 2012

To compile and run from a command prompt



With The Help of these steps you can run your program through command prompt.
  1. Paste the code from the preceding procedure into any text editor, and then save the file as a text file. Name the file Hello.cs. C# source code files use the extension.cs.
  2. On the Start menu, expand Visual Studio Tools, and then choose the shortcut to open a Visual Studio Command Prompt window.
    As an alternative, you can follow the instructions in How to: Set Environment Variables to enable command-line builds from a standard Command Prompt window.
  3. In the Visual Studio Command Prompt window, navigate to the folder that contains your Hello.cs file.
  4. Enter the following command to compile Hello.cs.
    csc Hello.cs
    If your program has no compilation errors, an executable file that is named Hello.exe is created.
  5. In the Visual Studio Command Prompt window, enter the following command to run the program:
    Hello


    Note: This information is taken from Microsoft site so if you want to know more then follow the link which is given below...





Friday, 24 August 2012

Main method can be Private?

Yes, Main method of the program can be private.it does not effect on output of the program.
i have checked It practically using C# on the visual Studio 2010.


You can see it in Example..............


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Factorial
    {
        public int fact(int n)
{
    if (n == 0) return 1;
    else return n * fact(n - 1);
}
       private static void Main(string[] args)
        {
            Factorial ob = new Factorial();
            Console.WriteLine("Type a number up to 20:");
            int n = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(ob.fact(n));
            Console.Read();

        }
    }
    }

And Second thing if any program contain more than one Main method then what will happen?
No Problem, if  first main method declared as private and Second is Public then first Main method will compile and run but it can not be entry point of your Application.

For Example......


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class MAinmethod
    {
==>        private static void Main(string[] args)
        {
            Console.WriteLine("Its awsome,, i am private and runnig here");
        }
        class testing
        {

==>          public static void Main(string[] args)
            {
                Console.WriteLine("i am public and running here");
                MAinmethod.Main(args);
                Console.Read();

            }
        }
    }
}

And Next question arise that if given example both are private then what will happen?


So it will not effect on the program output or in compilation and run. and take last Main method as the entry point of the Program...

for example............


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class MAinmethod
    {
-->        private static void Main(string[] args)
        {
            Console.WriteLine("Its awsome,, i am private and runnig here");
        }
        class testing
        {

-->          private static void Main(string[] args)
            {
                Console.WriteLine("i am public and running here");
                MAinmethod.Main(args);
                Console.Read();

            }
        }
    }
}

out put of this Program will be............