Thursday 29 March 2012

simple and easy form validation using HTML 5 attribute



<table>
<tr>
    <td class="style1">
      <div align="left">Title<sup>*</sup></div>
    </td>
    <td>
      <div align="left">
          <asp:TextBox ID="TextBox1" runat="server"  required="required" ></asp:TextBox>
   
        </div>
    </td>
  </tr>

 <tr>
    <td class="style1">
      <div align="left"> Price<sup>*</sup>   </div>
    </td>
    <td>
      <div align="left"> <asp:TextBox ID="TextBox2" runat="server"  required="required"              pattern="[0-9]+$"  title="only numeric constants"></asp:TextBox></div>
    </td>
  </tr>
<tr>
<button runat="server"/>
</tr>

---------------------------------------------------------------------------------
thats it..................


we need use required="required' attribte of html5
and if we need to validate for specific constraint use pattern attribute just like regular expression validator
just like above second condition

<table>

Tuesday 27 March 2012

List of all the states which may be usefull in register form to list out our states


Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh
Goa
Gujarat
Haryana
Himachal Pradesh
Jammu and Kashmir
Jharkhand
Karnataka
Kerala
Madhya pradesh
Maharashtra
Manipur
Megalaya
Mizoram
Nagaland
Orissa
Punjab
Rajashthan
Sikkim
Tamil Nadu
Tripura
Uttar Pradesh
Uttarakhand
West Bengal

Monday 26 March 2012

List of all the countries which may be usefull in register form to list out countries


Afghanistan
Akrotiri
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua and Barbuda
Argentina
Armenia
Aruba
Ashmore and Cartier Islands
Australia
Austria
Azerbaijan
Bahamas, The
Bahrain
Bangladesh
Barbados
Bassas da India
Belarus
Belgium
Belize
Benin
Bermuda
Bhutan
Bolivia
Bosnia and Herzegovina
Botswana
Bouvet Island
Brazil
British Indian Ocean Territory
British Virgin Islands
Brunei
Bulgaria
Burkina Faso
Burma
Burundi
Cambodia
Cameroon
Canada
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Clipperton Island
Cocos (Keeling) Islands
Colombia
Comoros
Congo, Democratic Republic of the
Congo, Republic of the
Cook Islands
Coral Sea Islands
Costa Rica
Cote d'Ivoire
Croatia
Cuba
Cyprus
Czech Republic
Denmark
Dhekelia
Djibouti
Dominica
Dominican Republic
Ecuador
Egypt
Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Europa Island
Falkland Islands
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern and Antarctic Lands
Gabon
Gambia, The
Gaza Strip
Georgia
Germany
Ghana
Gibraltar
Glorioso Islands
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Heard Island and McDonald Islands
Holy See (Vatican City)
Honduras
Hong Kong
Hungary
Iceland
India
Indonesia
Iran
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Jan Mayen
Japan
Jersey
Jordan
Juan de Nova Island
Kazakhstan
Kenya
Kiribati
Korea, North
Korea, South
Kuwait
Kyrgyzstan
Laos
Latvia
Lebanon
Lesotho
Liberia
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mayotte
Mexico
Micronesia, Federated States of
Moldova
Monaco
Mongolia
Montserrat
Morocco
Mozambique
Namibia
Nauru
Navassa Island
Nepal
Netherlands
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Northern Mariana Islands
Norway
Oman
Pakistan
Palau
Panama
Papua New Guinea
Paracel Islands
Paraguay
Peru
Philippines
Pitcairn Islands
Poland
Portugal
Puerto Rico
Qatar
Reunion
Romania
Russia
Rwanda
Saint Helena
Saint Kitts and Nevis
Saint Lucia
Saint Pierre and Miquelon
Saint Vincent and the Grenadines
Samoa
San Marino
Sao Tome and Principe
Saudi Arabia
Senegal
Serbia and Montenegro
Seychelles
Sierra Leone
Singapore
Slovakia
Slovenia
Solomon Islands
Somalia
South Africa
South Georgia and the South Sandwich Islands
Spain
Spratly Islands
Sri Lanka
Sudan
Suriname
Svalbard
Swaziland
Sweden
Switzerland
Syria
Taiwan
Tajikistan
Tanzania
Thailand
Timor-Leste
Togo
Tokelau
Tonga
Trinidad and Tobago
Tromelin Island
Tunisia
Turkey
Turkmenistan
Turks and Caicos Islands
Tuvalu
Uganda
Ukraine
United Arab Emirates
United Kingdom
United States
Uruguay
Uzbekistan
Vanuatu
Venezuela
Vietnam
Virgin Islands
Wake Island
Wallis and Futuna
West Bank
Western Sahara
Yemen
Zambia
Zimbabwe

Tuesday 13 March 2012

procedure to update the table values


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[updating]
(
@regid int,
@name varchar(50),
@email varchar(50),
@password varchar(50),
@phno varchar(50),
@addres varchar(50),
@institute varchar(50),
@course varchar(50),
@studying varchar(50)
)
as
update registration set
SE_name = @name,
SE_email = @email,
SE_password = @password,
SE_phno = @phno,
SE_address = @addres,
SE_institute = @institute,
SE_course = @course,
SE_studying = @studying
where
SE_regid = @regid

Friday 9 March 2012

How to store and retrieve images into databse in binary format.

In design page

<div id="new" align="left">
<p><label>Name:</label>
    <asp:Image ID="Image1" runat="server"  ImageAlign="Right" ImageUrl="~/images/commentbox.gif"/></p>
<div style=" padding-left:15px;">
    <asp:TextBox ID="TextBox1" runat="server" Width="191px"></asp:TextBox></div>
<p align="left"><label>E-mail</label></p>
<div align="left" style=" padding-left:15px;">
    <asp:TextBox ID="TextBox2" runat="server" Width="191px"></asp:TextBox></div>
   
<p align="left"><label>Upload Profile Picture</label></p>
<div align="left" style=" padding-left:15px;">
    <asp:FileUpload ID="FileUpload1" runat="server" /></div>  
  <div>
<label align="left">Comments/queries</label></div>
<div id="selector" align="left">

    <asp:TextBox ID="TextBox3" runat="server" TextMode="MultiLine" Height="80px"
        Width="585px"></asp:TextBox>
 
        </div>
             <div align="right" style="width: 71%">
    <asp:ImageButton ID="ImageButton1" runat="server" width="120px" Height="30"
        ImageUrl="~/images/comment.png" onclick="ImageButton1_Click"/>

       
       
        </div>
</div>
<br />
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label></div>
        ///// Inserting Into database
<div  id="selector2">
<br />
<asp:DataList ID="ddl1" runat="server" Width="620px"
        onitemcommand="ddl1_ItemCommand"
        onselectedindexchanged="ddl1_SelectedIndexChanged">
<ItemTemplate>
<div class="outr">
<table width="100%">
<tr><div style=" background-color:Maroon; padding-left:10px;"><asp:Label ID="name" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SE_name") %>' Font-Bold="true" ForeColor="White" Font-Size="Small" ></asp:Label></div></tr>
<tr>
<td valign="top" width="20%"><asp:Image ID="Image1" runat="server" 
         ImageUrl='<%# "GetImage.aspx?id=" + Eval("SE_commentid") %>' width="100px" Height="100px" align="left" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px"/> 
         //// Please observe here 'm using image handler
         </td>
<td valign="top" width="80%"><div><asp:TextBox ID="txt1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SE_comment")%>' CssClass="expanding" AutoPostBack="false" TextMode="MultiLine" Width="100%" BorderStyle="None" style="resize:none;"></asp:TextBox></div></td>

</tr>
<tr> </tr>
</table>
    <asp:Button ID="hiddenbutton" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SE_commentid") %>' visible="false" />
<%--<div><textarea runat="server" text='<%# DataBinder.Eval(Container.DataItem, "SE_comment")%>'></textarea></div>--%>
<div style="padding-left:10px;" align="right">
 <font style=" font-size:small; font-weight:bold;">Posted date:</font><asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SE_posteddate") %>' Font-Bold="true" ForeColor="DarkOliveGreen" Font-Size="Small" ></asp:Label><asp:Label
    ID="Label3" runat="server" ></asp:Label></div>
 
</div>
<%-- <hr  width="100%" color="#EEE6E6"/>--%>
<br />

</ItemTemplate>
</asp:DataList>
    <asp:Label ID="Label5" runat="server" Visible="false"></asp:Label>
</div>      
////// Retreiving from database




In code behind


 protected void Page_Load(object sender, EventArgs e)
    {
        string connstr = ConfigurationManager.ConnectionStrings["smartedu"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        try
        {
           // SqlCommand cmd = new SqlCommand("select top 10 * from Commentbox order by SE_commentid Desc", con);

           // cmd.Connection.Open();

           //ddl1.DataSource = cmd.ExecuteReader();

           //ddl1.DataBind();

   
            string query = "select top 10 * from Commentbox order by SE_commentid Desc";
            SqlDataAdapter da = new SqlDataAdapter(query, con);
            DataTable table = new DataTable();

            da.Fill(table);
            ddl1.DataSource = table;
            ddl1.DataBind();

            //cmd.Connection.Close();
            //cmd.Connection.Dispose();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
       
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string connstr = ConfigurationManager.ConnectionStrings["smartedu"].ConnectionString;
        SqlConnection conn = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "dbo.comment";
   
        cmd.Parameters.AddWithValue("@name",TextBox1.Text);
        cmd.Parameters.AddWithValue("@email", TextBox2.Text);
        cmd.Parameters.AddWithValue("@comment", TextBox3.Text);
   

        if (FileUpload1.HasFile)
        {
            //getting length of uploaded file
            int length = FileUpload1.PostedFile.ContentLength;
            //create a byte array to store the binary image data
            byte[] imgbyte = new byte[length];
            //store the currently selected file in memeory
            HttpPostedFile img = FileUpload1.PostedFile;
            //set the binary data
            img.InputStream.Read(imgbyte, 0, length);
           // string imagename = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            //use the web.config to store the connection string

            //string newimagename = TextBox1.Text + imagename;

            // Session["imagepreserve"] = imgbyte;
            //con.Open();
            //SqlCommand cmd1 = new SqlCommand("SELECT  max(SE_Regcollid) FROM institutereg", con);

            //int res = (int)cmd1.ExecuteScalar();
            //int newres = (res + 1);
            //string newid = TextBox1.Text + newres;
            //con.Close();


            //  con.Open();

           /// cmd.Parameters.AddWithValue("@imagename", newimagename);
           ///
            cmd.Parameters.AddWithValue("@image", imgbyte);

            //  con.Close();

        }
        else
        {


            string filePath = Server.MapPath("~/images/nocommentimage.png");
            Byte[] imgByte = System.IO.File.ReadAllBytes(filePath);

          //  string newimagename = TextBox1.Text + "imagenotfound";
            //con.Open();
            //SqlCommand cmd1 = new SqlCommand("SELECT  max(SE_Regcollid) FROM institutereg", con);

            //int res = (int)cmd1.ExecuteScalar();
            //int newres = (res + 1);
            //string newid = TextBox1.Text + newres;
            //con.Close();


            cmd.Parameters.AddWithValue("@image", imgByte);


        }


        cmd.Connection = conn;

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }

     
    }

    protected void ImageButton2_click(object sender, ImageClickEventArgs e)
    {
     
        string connstr = ConfigurationManager.ConnectionStrings["smartedu"].ConnectionString;
        SqlConnection con = new SqlConnection(connstr);
        string id = Request.QueryString["id"];

        SqlCommand cmd = new SqlCommand("SELECT SE_vote from Commentbox where SE_commentid = '" + id + " '", con);

       con.Open();

       Label5.Text = cmd.ExecuteScalar().ToString();

       con.Close();
       

     

    }

    protected void ddl1_ItemCommand(object source, DataListCommandEventArgs e)
    {

        if (e.CommandName == "view")
        {
            Button hb = (Button)(e.Item.FindControl("hiddenbutton"));
            Response.Redirect("comments.aspx?id=" + hb.Text);
        }

    }


In image handler

Just create the new page and save it as GetImage.aspx .




  protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];

        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["smartedu"].ConnectionString);
        string imageid = Request.QueryString["id"];

        con.Open();
        SqlCommand command = new SqlCommand("select SE_image from Commentbox where SE_commentid=" + imageid, con);
        SqlDataReader dr = command.ExecuteReader();
        dr.Read();
        Response.BinaryWrite((Byte[])dr[0]);
        con.Close();
        Response.End();
    }



thats it...................

Monday 5 March 2012

on mouseover changing the background color of the row in datalist


<style type="text/css">
   
    #divname {
background-color:white;
}
#divname:hover {
background-color:#dddddd;
}
</style>

--------------------------------------------------------------------------------------


<asp:DataList ID="DataList1" runat="server" Width="627px" >
    <ItemTemplate>
    <div id="divname">    /// use css style here
    <div id="div1" style="padding-left:5px;">
    <asp:Label ID="label1" runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "SE_collegename")%>' Font-Bold="true" ForeColor="#0099CC" Font-Size="Small" Font-Underline="true"></asp:Label>
    </div>
    <br />
    <div id="div2" style="padding-left:5px;">
    <asp:Label ID="label2" runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "SE_Loaction")%>' ></asp:Label>
    </div>
        <br />
   
    <div style="padding-left:5px;">
    <asp:Label ID="label3" runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "SE_category")%>' ></asp:Label>
   
    </div>
    <div id="div3" align="right" style="padding-right:5px;">
        <asp:LinkButton ID="LinkButton1" CommandName="view" runat="server">View more</asp:LinkButton>
    </div>
    </div>
    <hr  width="100%" color="#EEE6E6"/>
    <br />
    </ItemTemplate>
 
    </asp:DataList>




...................................................................

Thats it...............

Saturday 3 March 2012

stored procedure for searching records with if - else - condition


set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[search1]
(
@category varchar(max),
@state varchar(max),
@City varchar(max)
)as
if @category = 'Select Category' and @state = '' and @City = ''
Begin
Select * From Colleges;
END

else if @state = '' and @City = ''
Begin
Select * From Colleges where SE_category =@category
END

else if @category = 'Select Category' and @City = ''
Begin
Select * From Colleges where SE_Loaction =@state
END

else if @category = 'Select Category' and @state = ''
Begin
Select * From Colleges where SE_Loaction =@City
END

else if @category = 'Select Category'
Begin
Select * From Colleges where SE_Loaction = @state or SE_Loaction =@City
END

else if @state = ''
Begin
Select * From Colleges where SE_category =@category  and SE_Loaction =@City
END

else if @City = ''
Begin
Select * From Colleges where SE_category =@category  and SE_Loaction =@state
END

else
Begin
Select * From Colleges where SE_category =@category  and SE_Loaction =@state and  SE_Loaction =@City
END

Friday 2 March 2012

grouping of list values in dropdownlist

1 First we need to add other third party .dll file

Koutny.WebControls.DropDownGroupableList.Net2.dll


2 Then we need to include this in our project

we do this by adding this Koutny.WebControls.DropDownGroupableList.Net2.dll file in bin and adding inleft side toolbox

3 after this include this in web config file


<authentication mode="Windows"/>
<pages>
<controls>
<add tagPrefix="koutny" namespace="Koutny.Web.UI.WebControls" assembly="Koutny.WebControls.DropDownGroupableList.Net2"/>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/></controls>
<namespaces>
<add namespace="Koutny.Web.UI.WebControls"/>
</namespaces>
</pages>



4 after that use the control in design page like this


 <koutny:DropDownGroupableList ID="drop1" runat="server" CssClass="select" Width="150px" >
<koutny:OptionListItem Text="Select Category"/>
</koutny:DropDownGroupableList>

5 and in code behind


 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            drop1.Items.AddItem("Item1");
            drop1.Items.AddItem("Item2");
            drop1.Items.AddGroup("Group1", "G1");
            drop1.Items.AddItem("Item3", "Item3", "G1");
            drop1.Items.AddItem("Item4", "Item4", "G1");
            drop1.Items.AddItem("Item5");
        }
    }


6. to get the value of the selected item use like this

   protected void DisplaySelectedItem(object sender, EventArgs e)
{
OptionListItem selectedItem = drop1.SelectedItem;
if (selectedItem != null)
{
litSelectedItem.Text = selectedItem.Text;

OptionGroupItem selectedGroup = drop1.SelectedGroup;
if (selectedGroup != null) litSelectedGroup.Text = selectedGroup.Text;
else litSelectedGroup.Text = "none";
}
}


Thursday 1 March 2012

How to pass field value from one page to another page

Case: we need to send dropdownlist value which is in home page to search result page on button click


in button click include this
 
     Response.Redirect("Searchresults.aspx?DropDownList1=" + DropDownList1.SelectedItem.Text);

in serach result page access the value like this 

      String s = Request.QueryString["DropDownList1"];
       Label4.Text = s;


Thats it..........



DropDownList1 is the variable wch holds the value
DropDownList1.SelectedItem.Text is the property for the value.