Follow the steps.
Step 1. Create method VerifyInDB(string/int randomvariable) in you cs file as follows
public bool VerifyInDB(string/int RandomVariable)
{
bool b_RandomStatus=false;
sqlconnection con_SQLConnectionString=new sqlConnection(Constr);
SqlDataAdapter da_ForCheckforRandomInDB=new SqlDataAdapter("select * from DB..tbl_table where RandomColumnName="+ randomvariable, con_SQLConnectionString);
DataTable dt_ForCheckforRandomInDB=new DataTable()
da.fill(dt_ForCheckforRandomInDB)
if(dt_ForCheckforRandomInDB.Rows.Count()>0)
{
b_RandomStatus=true;
}
return b_RandomStatus;
}
Step 2. Create another method in the same *.cs file as follows
public string/int CreateRandomNumber()
{
string/int RandomVariable ;
if(VerifyInDB(RandomVariable))
{
CreateRandomNumber()
}
Return RandomVariable;
}
Step 3: Call
CreateRandomNumber()
any where in your code this will return random value which is not present in your DataBase.
Step 4:int/String RandumValue=CreateRandomNumber(); you will get the random value which is not repeated in DataBase.