Change your query to load the record by ID, which should be unique, rather than loading by mobile, which is not:
const string strQuery = "select * from APPLICATION where ID = @ID";
...
cmd.Parameters.AddWithValue("@ID", DropDownList6.SelectedItem.Value);
Also:
catch (Exception ex)
{
throw ex;
}
Don't do that. You've just destroyed the stack trace of the exception, making it much harder to track down the line that caused it. If you must catch and re-throw an exception, use
throw;
instead of
throw ex;
, which will preserve the stack trace:
catch (Exception ex)
{
throw;
}
But in this case, since you're not doing anything with the exception, there's no need to catch it in the first place. Just remove the
catch
block.
And:
finally
{
con.Close();
con.Dispose();
}
Wrap the connection, and any other objects which implement
IDisposable
, in a
using (...) { ... }
block. That way, they will automatically be disposed when the block ends, and you won't need to worry about the
try..finally
block at all.
using (SqlConnection con = new SqlConnection("..."))
using (SqlCommand cmd = new SqlCommand(strQuery, con))
{
cmd.Parameters.AddWithValue("@ID", DropDownList6.SelectedItem.Value);
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
if (sdr.Read())
{
...
}
}
}
using statement - C# Reference | Microsoft Docs[
^]