faster or better approach of filling data to dropdownlist
-
Hi all, I am using asp.net2.0 , C#. I would like to know which is the best approach(faster ) to fill dopdownlist 1. making stored procedure and filling data to dropdownlist using datareader 2. passing query and filling data to dropdownlist using datareader As I am facing problem of slowness of filling data . if you find any better approach or faster approach please guide me. database is not much heavy Method I used to fill dropdownlist is
public void FillctrlList2(DropDownList ctrl, string procedurename,string fieldtext ,string fieldvalue , string conn, SqlCommand cmd, int i) { try { if (con == null) { this.openconnection(conn); this.con.Open(); // con.Open(); } if (con.State == ConnectionState.Closed) { this.openconnection(conn); con.Open(); } ctrl.Items.Clear(); ctrl.Items.Add(new ListItem("Select One", "0" )); cmd.Connection = con; cmd.CommandText = procedurename; cmd.CommandType = CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); ctrl.DataSource = rdr; ctrl.DataTextField = fieldtext; ctrl.DataValueField = fieldvalue; ctrl.DataBind(); rdr.Close(); } catch (Exception ex) { throw ex; } }
regards imran khan
-
Hi all, I am using asp.net2.0 , C#. I would like to know which is the best approach(faster ) to fill dopdownlist 1. making stored procedure and filling data to dropdownlist using datareader 2. passing query and filling data to dropdownlist using datareader As I am facing problem of slowness of filling data . if you find any better approach or faster approach please guide me. database is not much heavy Method I used to fill dropdownlist is
public void FillctrlList2(DropDownList ctrl, string procedurename,string fieldtext ,string fieldvalue , string conn, SqlCommand cmd, int i) { try { if (con == null) { this.openconnection(conn); this.con.Open(); // con.Open(); } if (con.State == ConnectionState.Closed) { this.openconnection(conn); con.Open(); } ctrl.Items.Clear(); ctrl.Items.Add(new ListItem("Select One", "0" )); cmd.Connection = con; cmd.CommandText = procedurename; cmd.CommandType = CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); ctrl.DataSource = rdr; ctrl.DataTextField = fieldtext; ctrl.DataValueField = fieldvalue; ctrl.DataBind(); rdr.Close(); } catch (Exception ex) { throw ex; } }
regards imran khan
There is nothing in that code that would make it slow in any way. You might make it slightly faster if you loop through the data reader youself instead of using data binding, but not enough to make it noticable unless you are putting thousands of items in the dropdown. If you really have that many items in a dropdown list, the problem is not the performance of the code, but that you have to rethink your user interface.
Despite everything, the person most likely to be fooling you next is yourself.
-
Hi all, I am using asp.net2.0 , C#. I would like to know which is the best approach(faster ) to fill dopdownlist 1. making stored procedure and filling data to dropdownlist using datareader 2. passing query and filling data to dropdownlist using datareader As I am facing problem of slowness of filling data . if you find any better approach or faster approach please guide me. database is not much heavy Method I used to fill dropdownlist is
public void FillctrlList2(DropDownList ctrl, string procedurename,string fieldtext ,string fieldvalue , string conn, SqlCommand cmd, int i) { try { if (con == null) { this.openconnection(conn); this.con.Open(); // con.Open(); } if (con.State == ConnectionState.Closed) { this.openconnection(conn); con.Open(); } ctrl.Items.Clear(); ctrl.Items.Add(new ListItem("Select One", "0" )); cmd.Connection = con; cmd.CommandText = procedurename; cmd.CommandType = CommandType.StoredProcedure; rdr = cmd.ExecuteReader(); ctrl.DataSource = rdr; ctrl.DataTextField = fieldtext; ctrl.DataValueField = fieldvalue; ctrl.DataBind(); rdr.Close(); } catch (Exception ex) { throw ex; } }
regards imran khan
catch (Exception ex)
{
throw ex;
}Please change that portion to:
catch (Exception ex)
{
throw;
}By doing it your way, you lose the stack trace and it can be harder to debug future problems. And maybe you actually want to do something in that catch other than rethrowing the error? If not, what's the point of catching it in the first place? I know this doesn't have anything to do with your problem as I don't see anything wrong with your databinding. So unless you're binding a large set of data like Guffa mentioned, this should work fine.
-
There is nothing in that code that would make it slow in any way. You might make it slightly faster if you loop through the data reader youself instead of using data binding, but not enough to make it noticable unless you are putting thousands of items in the dropdown. If you really have that many items in a dropdown list, the problem is not the performance of the code, but that you have to rethink your user interface.
Despite everything, the person most likely to be fooling you next is yourself.
Guffa wrote:
You might make it slightly faster if you loop through the data reader youself instead of using data binding,
Databinding uses the
IEnumerator
interface to iterate through each item. When we loop through theDataReader
, I guess the same thing is happening. So how this can boost the performance ? Please correct me.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
-
Guffa wrote:
You might make it slightly faster if you loop through the data reader youself instead of using data binding,
Databinding uses the
IEnumerator
interface to iterate through each item. When we loop through theDataReader
, I guess the same thing is happening. So how this can boost the performance ? Please correct me.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia My Website | Ask smart questions
The data binding adds a bit of overhead because it can use many different kinds of data sources. You can improve on this slightly as you can write code specificly for the data source and for the data that it contains.
Despite everything, the person most likely to be fooling you next is yourself.