Here is how it should be written.
int text;
if(int.TryParse(this.Txtusers.Text, out text)
{
using(var con = new SqlConnection(connectionString)
{
using(var cmd = new SqlCommand("select TOP (@top) * from Avaya_Id where LOB = @LOB and Status = 'Unassigned'", con))
{
cmd.Parameters.Add("@top", SqlDbType.Int).Value = text;
cmd.Parameters.Add("@LOB", SqlDbType.Int).Value = DDLOB.SelectedItem.Value;
con.Open();
using(var rdr = cmd.ExecuteReader())
{
GridView1.DataSource = rdr;
GridView1.DataBind();
}
}
}
}
Points of interest:
- Using parameters to avoid the risk of Sql Injection.
- Changed
Convert.ToInt32
to int.TryParse
. Never trust user input.
- Use the
using
statement for every instance that implements the IDisposable
interface.
- Please note that using
top x
without an order by
clause means you get x arbitrary records from the database - since database tables are unordered by nature and the only way to ensure the order of the rows returned from a select
statement is to use the order by
clause.
Please note I've guessed that the second parameter is an int, if it's not, change the data type.