Tuesday 27 December 2011

How to get current URL / Request.Url in Javascript?

Definition: The href property returns the entire URL of the current page.

Code:
      function GetURL()
      {
        var Url = document.location.href;
       alert(Url);
      }

Wednesday 21 December 2011

How to clear TextBox value using javascript?

Definition: Clear TextBox value.
To clear TextBox value we will use onclick() event of TextBox. onclick() we will call user defined javascript function.

Code:
1. First put TextBox control on page.

<asp:TextBox ID="TextBox1" runat="server" Text="Your Text Here" onclick="javascript:Clear(this);"></asp:TextBox>

2. Now write onclick() event as shown above.

3. Now in Head tag we will write javascript function.

<head runat="server">
    <title>Form validation</title>
    <script language="javascript" type="text/javascript">
                  function Clear(txt)
                {
                         txt.value="";
                 }
  </script>
</head>

4. When user click inside the TextBox its value will be clear.





Tuesday 20 December 2011

How to generate random password in Asp.Net / C#.Net?

Definition: Generate Random Password.

Code:
private string GeneratePassword(byte Length)
    {
        char[] Chars = new char[] {
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
        };
        string String = string.Empty;
        Random Random = new Random();

        for (byte a = 0; a < Length; a++)
        {
            String += Chars[Random.Next(0, 61)];
        };

        return (String);
    }

How to call GeneratePassword method?

string pwd = string.Empty;
//generate random password
pwd = GeneratePwd(8);  //we have to pass length of password. you can pass any length


Friday 2 December 2011

How to prepare masala bhindi?

Ingredients for stuffed bhindi:





500 gm Okra (Bhindi), washed and dried
3 tbsp - oil
1 tsp cumin seeds (jeera)
1 medium sized onion, chopped
2 green chilies, seeded and chopped
3/4-inch ginger, finely chopped
A pinch of Asafetida powder
1 tomato chopped

For Stuffing :
3 tsp coriander powder
2 tsp turmeric powder
2 tsp ground fennel (saunf)
2 tsp dried mango powder (amchur) / or you can use 1tsp lemon juice
1/2tsp chili powder or to taste
Salt To Taste 
For Garnishing:
Coriander leaves
Preparation of stuffed okra:
  1. Cut the stalk of each okra and make lengthwise slit.
  2. Combine stuffing ingredients and mix well. Stuff each okra with the mixture.
  3. Sauté cumin with the little oil until it starts to crackle.
  4. Add onions, green chilies and ginger. Sauté till onion turns transparent, then put in asafetida and cook for a few seconds.
  5. Add tomato and cook until it turns pulpy.
  6. Add the okra and cook for 5 minutes until tender and well coated with the sauce (masala). 
  7. Now put off the flame and garnish with coriander leaves. 
  8. Serve stuffed bhindi hot with chapati,rice or paratha.

How to Create File in C#.net?

File.Create Method (String)
Creates or overwrites a file in the specified path.

Code:
Add Namespace:  System.IO

using System.IO;

protected void btnCreate_Click(object sender, EventArgs e)
    {
        string ls_FilePath = "G:\\";
        string ls_FileName = txtFilename.Text + ".txt";

        try
        {
            File.Create(ls_FilePath + "\\" + ls_FileName);
            lblmsg.Text = "File Created";
        }
        catch(Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
       
    }

How to show image in GridView in Asp.net?

Show image in Gridview control.

Code:

1. First Create Table with following structure:

CREATE TABLE [Image1]
(
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [ImageName] [varchar](50) ,
    [Description] [varchar](50)
)

2. Now insert GridView control on your ASPX page and set the following properties as show below.

        <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False"
            CellPadding="4" ForeColor="#333333" GridLines="None">
              <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
              <RowStyle BackColor="#EFF3FB" />
              <Columns>
                <asp:TemplateField HeaderText="id">
                    <ItemTemplate>
                    <asp:Label ID="lblEmpCode" runat="server" Text='<%#    DataBinder.Eval(Container.DataItem, "ID")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="name">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ImageName")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="image">
                    <ItemTemplate>
                        <asp:Image ID="myImage" runat="server" Height="25" ImageUrl='<%# DataBinder.Eval ( Container, "DataItem.ID", "~/Images/{0}.jpg" ) %>' />
                    </ItemTemplate>
                </asp:TemplateField>
              </Columns>
             
              
              <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
              <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
              <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
              <EditRowStyle BackColor="#2461BF" />
              <AlternatingRowStyle BackColor="White" />
        </asp:GridView>

3. Fill grid. You can use following code or your own code to fill grid with database table.
    Please set "WebConnectionString" connection string according to your SQL Server.
    Add Namespace: using System.Data;

 private void FillGrid()
    {
        string ls_SQL = string.Empty;
        string ls_Constr = string.Empty;
        SqlConnection cn;
        SqlDataAdapter adp;
        DataTable dtImage = new DataTable();

        try
        {
            ls_SQL = "SELECT  id,imagename,description FROM Image";
            ls_Constr = ConfigurationManager.ConnectionStrings["WebConnectionString"].ConnectionString;
            cn = new SqlConnection(ls_Constr);
            cn.Open();
            adp = new SqlDataAdapter(ls_SQL, cn);
            adp.Fill(dtImage );
            if (dtImage .Rows.Count > 0)
            {
                GridView3.DataSource = dtImage ;
                GridView3.DataBind();
            }
            cn.Close();
        }
        catch (Exception ex)
        {
        }
    }

4. Output:


5. Note: 
I have save my images in Images folder in Application Path

Here you can see 1.jpg,2.jpg,3.jpg and 4.jpg . I have save them with same ID as in the table.