Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi guys,

I have asp.net website with C#, sql server 2008.

In one of sql table, the table contains 1000's of records.

I have to fetch a record based on its RecordID.

so, i have stored table in session while loading the page.

using C# code, i'm selecting the record.
DataTable dt = new DataTable();
dt = (DataTable)Session["dt"];
string ddl_val = ddl.selectedValue.toString();

foreach (DataRow dr in dt.Rows)
                {
                    if (dr["RecID"].ToString() == ddl_val)
                    {
                        txt.Text = dr["OldRef"].ToString();
                        txtd.Text = dr["Description"].ToString();
                        txtu.Text = dr["UnitMeasurement"].ToString();
                        txtup.Text = dr["UnitPrice"].ToString();
                    }
                }


its a dropdown list from where i'm getting value to fetch a record.
n
its in a repeater.

so, its taking some where around 1min to fill all the textboxes, on every selection.

Can anyone please, help me, how can i sort out this.

Please,


Thanks
Posted
Updated 15-Oct-14 22:19pm
Comments
Mehdi Gholam 16-Oct-14 3:51am    
Obviously you need to rethink how you are doing things.
george4986 16-Oct-14 4:11am    
pass the ddl_val to backend and fetch only matched record instead of loading all records in table

Don't retrieve all records: use an SQL WHERE clause to restrict the return to just the values you are interested in:
SQL
SELECT OldRef, Description, UnitMeasurement, UnitPrice FROM MyTable WHERE RexId=@ddl_val
And provide the @ddl_val parameter via a parameterised query.
 
Share this answer
 
Comments
Mehdi Gholam 16-Oct-14 4:15am    
I believe the problem is not the query but how he is displaying the data (in text boxes in a repeater)
abdul subhan mohammed 16-Oct-14 4:16am    
Dude, its a dropdownlist n its located in a repeater, so, if i call it from d server, then on each n every dropdownlist selection it will hit the server again n again, bcoz of this i have stored this table in the session n taking from session.
Secondly, this contains 1000's of record on which its taking time to fetch the record.
Is there any other sol'n. Please.
I think you can do this by fetching the specific record from the database as you have it with you from the session. Check the below code,
C#
var recordData = dt.Select("RecID = " + ddl_val);

if (recordData.Count() > 0)
{
     txt.Text = recordData[0]["OldRef"].ToString();
     txtd.Text = recordData[0]["Description"].ToString();
     txtu.Text = recordData[0]["UnitMeasurement"].ToString();
     txtup.Text = recordData[0]["UnitPrice"].ToString();
}
 
Share this answer
 
In this situation I'd try to filter the data as close to the source as possible. SQL is optimised so that it's more performant returning queries than manipulating large amounts of data in memory using C#.

If this isn't possible and manipulating this in the code-behind is a must, wouldn't LINQ be more appropriate rather then iterating through the whole collection?

Thanks
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900