Wednesday, 18 September 2013

How to change the image on mouseover And change the color of text

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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>

    <script type="text/javascript">
        cc = 0;
        function changeimage() {
            if (cc == 0) {
                cc = 1;
                document.getElementById('myimage').src = "Koala.jpg";
            }
            else {
                cc = 0;
                document.getElementById('myimage').src = "Tulips.jpg";
            }
        }

        function chaimage() {
            if (cc == 0) {
                cc = 1;
                document.getElementById('Img1').src = "Koala.jpg";
            }
            else {
                cc = 0;
                document.getElementById('Img1').src = "Tulips.jpg";
            }
        }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
     
 
<h1 onmouseover="style.color='red'"
onmouseout="style.color='black'">
When Mouse over this text color will be Red And when will be out Text color will be Black</h1><br />
Above text color will change on mouseover and mouseout<hr />
 <br />


 
 <img id="myimage"  onmouseout="changeimage()" onmouseover="changeimage()"
border="0" src="Tulips.jpg"
width="300" height="100" />
<p>Above Image will change when we mouseover on image and change the image when we mouse out  from image</p><hr />
 

 <br />

<img id="Img1"  onclick="chaimage()"
border="0" src="Tulips.jpg"
width="300" height="100" />
<p>Above Image will change when we click on image and change the image when we change on image</p>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

There is no need of to write the code in Default.aspx.cs page

The Output is 


Listview

--CategoriesMaster
CREATE TABLE [dbo].[CategoriesMaster](
        [CategoryID] [uniqueidentifier] NOT NULL,
        [CategoryName] [varchar](50) NOT NULL,
)

--CategoriesImageMaster
CREATE TABLE [dbo].[CategoriesImageMaster](
        [CategoryImageID] [uniqueidentifier] NOT NULL,
        [CategoryID] [uniqueidentifier] NOT NULL,
        [ImageName] [varchar](50) NOT NULL,
)






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView.aspx.cs" Inherits="TreeView" %>

<!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">
    <div>
    <asp:treeview ID="xtreeCat" runat="server" ImageSet="XPFileExplorer" NodeIndent="15"
           >
        <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
        <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
            HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
        <ParentNodeStyle Font-Bold="False" />
        <SelectedNodeStyle Font-Underline="False"
            HorizontalPadding="0px" VerticalPadding="0px" BackColor="#B5B5B5" />
        </asp:treeview>
    </div>
    </form>
</body>
</html>
                                                                                                                                        //Code behind                                                                                                                           using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class TreeView : System.Web.UI.Page
{
    // Calling the connection string from Web.Config

    string strconn = ConfigurationManager.ConnectionStrings["cnnLocal"].ToString();
    SqlConnection sqlconn;
    string errdesc = "0";
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // This function has the logic of Binding the Root Nodes and Child Nodes to the TREE
            BindTree();
        }
    }

    private void BindTree()
    {
        try
        {
            sqlconn = new SqlConnection(strconn);
            sqlconn.Open();

            // Assigning the RootNode Function to bind the roots
            DataTable dtaddingrootnodes = RootNodes();
            //Declaring the Head Node
            TreeNode headnode = new TreeNode();
            headnode.Text = "Head";
            // Adding the Head Node to the Tree
            xtreeCat.Nodes.Add(headnode);
           
            // Looping through the data table to bind the Actual Root Nodes
            for (int i = 0; i < dtaddingrootnodes.Rows.Count; i++)
            {
                TreeNode rootnode = new TreeNode();
                rootnode.Text = dtaddingrootnodes.Rows[i]["CategoryName"].ToString();
                rootnode.Value = dtaddingrootnodes.Rows[i]["CategoryID"].ToString();
                headnode.ChildNodes.Add(rootnode);

                // Creating a Datatable that will hold the data of actual Child Nodes on the base of the value of rootnode
                DataTable dtactualChildNodes = ChildNodes(Convert.ToString(rootnode.Value));

                //Running a For Loop through the datatable and binding the actual Child Nodes
                for (int j = 0; j < dtactualChildNodes.Rows.Count; j++)
                {
                    TreeNode childnode = new TreeNode();
                    childnode.Text = dtactualChildNodes.Rows[j]["ImageName"].ToString();
                    childnode.Value = dtactualChildNodes.Rows[j]["CategoryImageID"].ToString();
                    rootnode.ChildNodes.Add(childnode);
                }

            }
            sqlconn.Close();
        }
        catch (Exception ex)
        {
            errdesc = ex.Message;
        }
    }


    // Function to get the Data for Root Nodes
    private DataTable RootNodes()
    {
        DataTable dt = new DataTable();
        try
        {
            sqlconn = new SqlConnection(strconn);
            sqlconn.Open();
            SqlCommand cmd = new SqlCommand("select * from CategoriesMaster", sqlconn);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            da.Fill(dt);
            sqlconn.Close();
         
        }
        catch (Exception ex)
        {
            errdesc = ex.Message;
        }
        return dt;
    }


    // Function to bind Actual Child Nodes on the base of the Root Node value
    private DataTable ChildNodes(string RootNode)
    {
        DataTable dt = new DataTable();
        try
        {
            sqlconn = new SqlConnection(strconn);
            sqlconn.Open();
            SqlCommand cmd = new SqlCommand("select * from CategoriesImageMaster where CategoryID = '" + RootNode + "'", sqlconn);
            SqlDataAdapter da1 = new SqlDataAdapter();

            da1.SelectCommand = cmd;
            da1.Fill(dt);
         
        }
        catch (Exception ex)
        {
            errdesc = ex.Message;
        }
        return dt;
    }
}




Monday, 16 September 2013

All type of validation used in aspnet

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

<!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: 145px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                NAME</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="NAME" runat="server" 
                    ControlToValidate="TextBox1" ErrorMessage="*"></asp:RequiredFieldValidator>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                EMAIL ID 
            </td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                    ControlToValidate="TextBox2" ErrorMessage="ENTER YOUR ID"></asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
                    ControlToValidate="TextBox2" ErrorMessage="ID IN FORMATE" 
                    ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                PASSWORD</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                <asp:RangeValidator ID="RangeValidator1" runat="server" 
                    ControlToValidate="TextBox3" ErrorMessage="YOUR PASSWORD IS NOT STRONG" 
                    MaximumValue="9999" MinimumValue="10"></asp:RangeValidator>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                RE PASSWORD</td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                <asp:CompareValidator ID="CompareValidator1" runat="server" 
                    ControlToCompare="TextBox3" ControlToValidate="TextBox4" 
                    ErrorMessage="PASSWORD IS NOT EQUAL"></asp:CompareValidator>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                AGE</td>
            <td>
                <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
                <asp:RangeValidator ID="RangeValidator2" runat="server" 
                    ControlToValidate="TextBox5" ErrorMessage="AGE IS NOT IN RANGE" 
                    MaximumValue="30" MinimumValue="18"></asp:RangeValidator>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                MARKS</td>
            <td>
                <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style2">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Button" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    <div>
    
    </div>
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
        DisplayMode="List" ShowMessageBox="True" />
    </form>
</body>
</html>




To Increase Bill No. Automatically

To increase Billno. Automatically through database 
just make a function same as below and call it on Page load 
public void billno()
        {
            try
            {

                da = new SqlDataAdapter("select MAX(Id) from CustomerDetails", con);
                dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    string id = dt.Rows[0][0].ToString();
                    if (id == "")
                    {
                       TextBox1.Text = "1000";
                    }
                    else
                    {
                        int id1 = Convert.ToInt32(id.ToString());
                        id1 = id1 + 1;
                        TextBox1.Text = id1.ToString();
                    }
                }
                else
                {
                    TextBox1.Text = "1000";
                }
                //TextBox1.Text = sb + TextBox45.Text;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

Friday, 13 September 2013

Update With FindControl Method

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
             cmd = new SqlCommand("update EmployeeMaster set Emp_Name=@Emp_Name,Category=@Category,Department=@Department,Qualification=@Qualification,Specialization=@Specialization,Salary=@Salary,Gender=@Gender,D_O_B=@D_O_B,Address=@Address,Pincode=@Pincode,Mobile=@Mobile,Email=@Email,M_Status=@M_Status,F_M_Name=@F_M_Name,BloodGroup=@BloodGroup,Allergy=@Allergy where Emp_Id=@Emp_Id", con);

            cmd.Parameters.Add("@Emp_Id", SqlDbType.BigInt).Value =Convert.ToInt32( GridView1.DataKeys[e.RowIndex].Value);
            cmd.Parameters.Add("@Emp_Name", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox18"))).Text;
            cmd.Parameters.Add("@Category", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox19"))).Text;
            cmd.Parameters.Add("@Department", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox20"))).Text;
            cmd.Parameters.Add("@Qualification", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox21"))).Text;
            cmd.Parameters.Add("@Specialization", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox22"))).Text;
            cmd.Parameters.Add("@Salary", SqlDbType.BigInt).Value = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox23"))).Text);
            cmd.Parameters.Add("@Gender", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox24"))).Text;
            cmd.Parameters.Add("@D_O_B", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox25"))).Text;
            cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox26"))).Text;
            cmd.Parameters.Add("@Pincode", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox27"))).Text;
            cmd.Parameters.Add("@Mobile", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox28"))).Text;
            cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox29"))).Text;
            cmd.Parameters.Add("@M_Status", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox30"))).Text;
            cmd.Parameters.Add("@F_M_Name", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox31"))).Text;
            cmd.Parameters.Add("@BloodGroup", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox32"))).Text;
            cmd.Parameters.Add("@Allergy", SqlDbType.NVarChar).Value = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("TextBox33"))).Text;

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Write("<script language=javascript>alert('Record Updated successfully')</script>");
            GridView1.EditIndex = -1;
            showgrid();
        }

How to Use Multiview in asp.net

<%@ 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">
    <div>
       <%-- <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">

            <asp:View ID="View1" runat="server">
                <asp:Label ID="Label1" runat="server" BackColor="Gray" Font-Bold="true" Width="250px"  Text="View1"></asp:Label><br />
            </asp:View>
            <asp:View ID="View2" runat="server">
                <asp:Label ID="Label2" runat="server" BackColor="Gray" Font-Bold="true" Width="250px"  Text="View1"></asp:Label><br />
            </asp:View>
            <asp:View ID="View3" runat="server">
                <asp:Label ID="Label3" runat="server" BackColor="Gray" Font-Bold="true" Width="250px"  Text="View1"></asp:Label><br />
            </asp:View>

        </asp:MultiView>--%>

    <center>
    <br /><br /><br /><br /><br /><br />
<asp:MultiView ID="MultiView1" runat="server">
<table width="100%" cellpadding="2" cellspacing="5">
<tr>
<td>
<asp:View ID="View1" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" Width="150px" BackColor="Gray" OnClick="lnkTab1_Click">Tab1</asp:LinkButton><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"  >
        <Columns>
            <asp:TemplateField HeaderText="Sname">
               
                <ItemTemplate>
                     <%#Eval("Sname") %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:LinkButton ID="LinkButton2" runat="server" Width="150px" BackColor="Gray" OnClick="lnkTab2_Click">Tab2</asp:LinkButton><br />
<asp:LinkButton ID="LinkButton3" runat="server" Width="150px" BackColor="Gray" OnClick="lnkTab3_Click">Tab3</asp:LinkButton>
 </asp:View>
</td>
<td>
<asp:View ID="View2" runat="server">
<asp:LinkButton ID="LinkButton4" runat="server"  Width="150px" BackColor="Gray" OnClick="lnkTab1_Click">Tab1</asp:LinkButton><br />
<asp:LinkButton ID="LinkButton5" runat="server" Width="150px" BackColor="Gray"  OnClick="lnkTab2_Click">Tab2</asp:LinkButton><br />


     <asp:TextBox ID="TextBox2"
        runat="server"></asp:TextBox><br /><asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
    <asp:GridView ID="GridView2" AutoGenerateColumns="false" runat="server"  >
        <Columns>
            <asp:TemplateField HeaderText="Address">
               
                <ItemTemplate>
                     <%#Eval("Address")%>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
   <asp:LinkButton ID="LinkButton6" runat="server" Width="150px" BackColor="Gray" OnClick="lnkTab3_Click">Tab3</asp:LinkButton>
</asp:View>
</td>
<td>
<asp:View ID="View3" runat="server">

<asp:LinkButton ID="LinkButton7" runat="server" Width="150px" BackColor="Gray"   OnClick="lnkTab1_Click">Tab1</asp:LinkButton><br />
<asp:LinkButton ID="LinkButton8" runat="server" Width="150px" BackColor="Gray" OnClick="lnkTab2_Click">Tab2</asp:LinkButton><br />
<asp:LinkButton ID="LinkButton9" runat="server" Width="150px" BackColor="Gray" OnClick="lnkTab3_Click">Tab3</asp:LinkButton><br />
<br />
 <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <br /><asp:TextBox ID="TextBox5"
        runat="server"></asp:TextBox><br /><asp:TextBox ID="TextBox6" runat="server"></asp:TextBox><br />
       
    <asp:GridView ID="GridView3" AutoGenerateColumns="false"   runat="server"
         >
         <Columns>
            <asp:TemplateField HeaderText="Mobile
            ">
               
                <ItemTemplate>
                     <%#Eval("Mob") %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <br />
    <asp:LinkButton ID="LinkButton10" Width="150px" BackColor="Gray" ForeColor="Gray" runat="server">v</asp:LinkButton>
</asp:View>
</td>
</tr>
</table>
</asp:MultiView>

</center>  
</td>
</tr>
</table>
    </div>
    </form>
</body>
</html>

Coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=tarun;User ID=sa;Password=123");
    SqlDataAdapter da;
    DataTable dt;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetDefaultView();
        }

    }
    public void showgrid()
    {
        
    }


    private void SetDefaultView()
    {
        MultiView1.ActiveViewIndex = 0;
        da = new SqlDataAdapter("select Sname from tar", con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void lnkTab1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;

        da = new SqlDataAdapter("select Sname from tar", con);
        dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void lnkTab2_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;

        da = new SqlDataAdapter("select Address from tar", con);
        dt = new DataTable();
        da.Fill(dt);
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
    protected void lnkTab3_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 2;

        da = new SqlDataAdapter("select Mob from tar", con);
        dt = new DataTable();
        da.Fill(dt);
        GridView3.DataSource = dt;
        GridView3.DataBind();
    }
}




CODE OF AUTOCOMPLETE AJAX CONTROL IN ASP.NET

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
        
        public static string[] GetCompletionList(string prefixText, int count, string contextKey)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con1"].ToString());
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from Patient_master Where Patient_Code LIKE '" + prefixText + "%' order by Patient_Code", con);
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            List<string> li = new List<string>();
            while (dr.Read())
            {
                li.Add(dr["Patient_Code"].ToString());
            }

            return li.ToArray();
        }


************************
  <asp:TextBox ID="TextBox1" runat="server" ForeColor="Black" AutoPostBack="True"></asp:TextBox>
    <asp:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" 
        CompletionInterval="10" DelimiterCharacters="" Enabled="True" 
        MinimumPrefixLength="1" ServicePath="" TargetControlID="TextBox1" 
        UseContextKey="True" ServiceMethod="GetCompletionList">
    </asp:AutoCompleteExtender>
**************************