Well here I m going to explain how can we generate random DIGITS No. and also Random ALPHA NUMERIC DIGITS No. for password. Hope it will help you.
for random numbers click:- here
Create a method name Pin_password().
public string Pin_password(string rec)
{
string pin_pwd = string.Empty;
int j = 0; //for Upper Case because it will generate the Character same as like ch[i] doing.
Random rnd = new Random(); // random class for random generate the numbers using .Next()
for (int i = 0; i <= 2; i++) // For loop
{
j = j + 1; //increment the J variable for Random Characters.
// Create nums Array.
int[] nums = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// create an characters Array for it.
char[] ch = 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’ };
nums[i] = nums[rnd.Next(0, 9)];
ch[i] = ch[rnd.Next(0, 20)];
ch[j] = ch[rnd.Next(0, 25)];
pin_pwd += nums[i].ToString() + ch[i].ToString() + ch[j].ToString().ToUpper();
// just add/append the nums and ch in pin_pwd.
//pin_pwd will store the string of random generated password.
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// following will check the random generated no. is exists in database or not if YES then //generate new one for it.
adp = new SqlDataAdapter(“select LOWER(password) AS password from tbl_name where password=@password “, con);
adp.SelectCommand.Parameters.AddWithValue(“@password”, pin_pwd.ToLower());
dt = new DataTable();
adp.Fill(dt);
adp.Dispose();
if (dt.Rows.Count == 0)
{
return pin_pwd;
dt.Dispose();
// if not found then return it.
}
else
{
// if record exits then generate new one and return it.
for (int i = 0; i <= 2; i++)
{
j = j + 1;
int[] nums = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
char[] ch = 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’ };
nums[i] = nums[rnd.Next(0, 9)];
ch[i] = ch[rnd.Next(0, 20)];
ch[j] = ch[rnd.Next(0, 25)];
pin_pwd += nums[i].ToString() + ch[i].ToString() + ch[j].ToString().ToUpper();
}
return pin_pwd;
dt.Dispose();
}
con.Close();
}
with regards
vik
OUTPUT:-


Recent Comments