Tuesday, 4 June 2013

How to generate Random password in asp.net using c#.

Introduction:-

In this article , I have explained how to generate random password in asp.net using c#. This is very simple to generate random password using Random function of c#. You are thinking what is need to generate random password . You have seen in many website when you register in website they have asked to enter your email_id  and then after they send user name and password in your email because it is tuff work to generate password manully.

Now open Visual studio -> open new website .

On design page put three labels.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Mixed Pass: <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><br />
        Lower Pass: <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label><br />
        Upper Pass: <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

Code behind page:-

protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = LowerOrUpperCasePassGen();
        Label2.Text = LowerCasePass();
        Label3.Text = UpperCasePass();
    }
    protected  Random ran = new Random();
    protected string[] strCharacters = { "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",
                                                           "1","2","3","4","5","6","7","8","9","0",
                                                           "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"};
    // mixed pass gen or combination with lower and uppercase
    public string LowerOrUpperCasePassGen()
    {
        int p = 0;
        string pass = string.Empty;
        Random ran = new Random();
        for (int x = 0; x < 8; x++)
        {
            p = ran.Next(0, 61);
            pass += strCharacters[p];
        }
        return pass;
    }
    //for only Upper case pass gen.
    public string UpperCasePass()
    {
        int p = 0;
        string pass = string.Empty;
        Random ran = new Random();
        for (int x = 0; x < 8; x++)
        {
            p = ran.Next(0, 35);
            pass += strCharacters[p];
        }
        return pass;
    }
    //for only lower case pass gen.
    public string LowerCasePass()
    {
        int p = 0;
        string pass = string.Empty;
        Random ran = new Random();
        for (int x = 0; x < 8; x++)
        {
            p = ran.Next(26, 61);
            pass += strCharacters[p];
        }
        return pass;
    }


In this code you can generate random password for 8 character or digit or mixed. If You want to generate random password greater than 8 then change the digit in for loop.

Friday, 31 May 2013

how to bind gridview using Jquery in asp.net .

Introduction:-

In this article , I have explained how to bind GridView using Jquery in asp.net. For this I have used jquery with ajax, because we cannot call method without using Ajax .

First Create a table:-

Create table login(userid varchar(50),password varchar(50), type varchar(50), status varchar(50));
Insert some dummy data in the table.

Drag and drop GridView Control on design page.

JQuery Code:-

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "GVBindUsingJQuery.aspx/BindGridView",
data: "{}",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#GridView1").append("<tr><td>" + data.d[i].userid + "</td><td>" + data.d[i].password + "</td><td>" + data.d[i].type + "</td><td>" + data.d[i].status + "</td></tr>");
}
},
error: function(result) {
alert("Error");
}
});
});
</script>
<style type="text/css">
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
</style>
Full Code:-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "GVBindUsingJQuery.aspx/BindGridView",
data: "{}",
dataType: "json",
success: function(data) {
for (var i = 0; i < data.d.length; i++) {
$("#GridView1").append("<tr><td>" + data.d[i].userid + "</td><td>" + data.d[i].password + "</td><td>" + data.d[i].type + "</td><td>" + data.d[i].status + "</td></tr>");
}
},
error: function(result) {
alert("Error");
}
});
});
</script>
<style type="text/css">
table,th,td
{
border:1px solid black;
border-collapse:collapse;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server">
        <HeaderStyle BackColor="#DC5807" Font-Bold="true" ForeColor="White" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Code behind Page:-

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using System.Text;
using System.Collections.Generic;
public partial class GVBindUsingJQuery : System.Web.UI.Page
{
    string constr = ConfigurationManager.ConnectionStrings["khamaconn"].ConnectionString;
    //MySqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("userid");
            dt.Columns.Add("password");
            dt.Columns.Add("type");
            dt.Columns.Add("status");
            dt.Rows.Add();
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.Rows[0].Visible = false;
        }
    }
    [WebMethod]
    public static LoginDetails[] BindGridView()
    {
        DataTable dt = new DataTable();
        List<LoginDetails> details = new List<LoginDetails>();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("Select * from login",con))
            {
                con.Open();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                foreach(DataRow dtrow in dt.Rows)
                {
                    LoginDetails login = new LoginDetails();
                    login.userid=dtrow["userid"].ToString();
                    login.password = dtrow["password"].ToString();
                    login.type = dtrow["type"].ToString();
                    login.status = dtrow["status"].ToString();
                    details.Add(login);
                }
            }
        }
        return details.ToArray();
    }
    public class LoginDetails
    {
        public string userid {get;set;}
        public string password { get; set; }
        public string type { get; set; }
        public string status { get; set; }
    }
}

Run the code and test.


Tuesday, 28 May 2013

How to send Bulk email in asp.net using c#.

Introduction:-

In the previous article , I have have explained how to send email in asp.net using c#. In this article I have explained how to send bulk email in asp.net using c#. Suppose that you have 50 email and your want to send them all at a time.

Let's create a table in which you have the following fields..

 id, name, emailid,country, state,city
 Insert the data in the table.

Now if you want to send the mail on button click event if you are using sql database. then read the table and use for loop for taking email id one by one.

for connection:-

string constr = ConfigurationManager.ConnectionStrings["sushilconn"].ConnectionString;

Code on button click event:-

protected void Button1_Click(object sender, EventArgs e)

    {
        con.Open();
        MySqlCommand cmd = new MySqlCommand("Select emailid from details", con);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        for (int i = 0;i< dt.Rows.Count;i++ )
        {
            string id=dt.Rows[i].ItemArray[0].ToString();
            MailMessage mail = new MailMessage();
            mail.To.Add(id);
            mail.From = new MailAddress("youremailid@gmail.com");
            mail.Subject = "Email using Gmail";

            string Body = "Hi, this mail is to test sending mail" +
                          "using Gmail in ASP.NET";
            mail.Body = Body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential
                 ("youremailid@gmail.com", "yourpassword");
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);

        }
}

The Above code send the mail one by one.

How to send email using gmail credential in asp.net using c#.

Introduction:-

In this article . I have explained how to send email using Gmail credential in asp.net. Using Gmail credential , you have required your gmailid and gmail password for sending a meil to someone. There are also antother way to send mail using SMTP server in asp.net , but using SMTP server only emailId required for sending email.
 
Design page like this:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        From :<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        To :
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        Subject :<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        Body :<asp:TextBox ID="TextBox4" runat="server" Height="39px"
            TextMode="MultiLine"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>

</html>

Don't forget to add following namespace in .cs page.

using System.Net.Mail;

Code Behind Page:-

protected void Button1_Click(object sender, EventArgs e)
    {
        
            MailMessage mail = new MailMessage();
            mail.To.Add(TextBox2.Text);
            mail.From = new MailAddress(TextBox1.Text);
            mail.Subject = TextBox3.Text;

            string Body = TextBox4.Text;
            mail.Body = Body;

            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
            smtp.Credentials = new System.Net.NetworkCredential
                 ("youremailid@gmail.com", "gmailpasword");
            //Or your Smtp Email ID and Password
            smtp.EnableSsl = true;
            smtp.Send(mail);
        }

    }

Run the code and test.
If any query regarding this article , feel free to ask.

Monday, 27 May 2013

How to convert GridView Column into HyperLink in asp.net.

Introduction:-

In this article , I have explained how to convert gridview column into hyperlink.

Design the page like this:-

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:HyperLinkField  runat="server" DataTextField="name" DataNavigateUrlFields="id" DataNavigateUrlFormatString="~/Details.aspx?id{0}" HeaderText="Name"/>
            <asp:BoundField DataField="Emailid" HeaderText="EmailId" />
        </Columns>
        </asp:GridView>

//Details.aspx page is another page on which you redirect to see details according to id.

Bind GridView:-

protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("id"), new DataColumn("name"), new DataColumn("Emailid"), new DataColumn("mobileno") });
            dt.Rows.Add(1,"Sushil Kumar","sushil@gmail.com","9874563258");
            dt.Rows.Add(2,"Muthu Kumar","muthu@gmail.com","5896574125");
            dt.Rows.Add(3,"Ashutosh Ojha","ashu@gmail.com","8745214789");
            dt.Rows.Add(4,"Sanjay wadhawa","sanjay@gmail.com","9852475635");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }


Wednesday, 22 May 2013

Trim() function in asp.net using c#.

Introduction:-


 In this article , I have explained Trim() function in c#. Trim() is used for removing leading and trailing white-space character. If the current string is empty or all the current instance is white-space then function return empty value.

Example:-

TextBox1.Text.Trim();

This will remove all leading and trailing white-space character from input string.

Monday, 20 May 2013

How to Disable resizable of multiline textbox in asp.net.

Introduction:-

In this article , I have explained how to disable resizable properties of textbox in asp.net.
Take a textbox on design page and set this property style like

style="resize:none"

Example:-

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" style="resize:none"></asp:TextBox>

You can also done by using CSS .

<style type="text/css">
textarea
{
     resize:none;
}

</style>