2014-12-15 1 views
0

내가 가지고있는 Topic 드롭 다운리스트를 채우고 캐시 테이블에 저장합니다 다음 코드를다른 선택에 따라 하나의 드롭 다운리스트를 필터링하는 방법

bookingData2 = new DataTable(); 
DataTable DTable_List = new DataTable(); 
string connString = @""; 
string query2 = @"Select * from [DB].dbo.[top]";// columng #1 = Specialty and column #2 = Topic 
using (SqlConnection conn = new SqlConnection(connString)) 
{ 
    try 
    { 
     SqlCommand cmd = new SqlCommand(query2, conn); 
     SqlDataAdapter da = new SqlDataAdapter(query2, conn); 
     da.Fill(bookingData2); 

     HttpContext.Current.Cache["cachedtable2"] = bookingData2; 

     bookingData2.DefaultView.Sort = "Topic ASC"; 

     Topic.DataSource = bookingData2.DefaultView.ToTable(true, "Topic"); // populate only with the Topic column 
     Topic.DataTextField = "Topic"; 
     Topic.DataValueField = "Topic"; 
     Topic.DataBind(); 
     Topic.Items.Insert(0, new ListItem("All Topics", "All Topics")); 

     da.Dispose(); 
    } 
    catch (Exception ex) 
    { 
     string error = ex.Message; 
    } 
} 

내가 가지고있는 Specialty 드롭 다운리스트를 채우고 저장합니다 다음 코드 또 다른 캐시 테이블 :

bookingData = new DataTable(); 
DataTable DTable_List = new DataTable(); 
string connString = @""; 
string query = @"select * from [DB].dbo.[SP]"; 

using (SqlConnection conn = new SqlConnection(connString)) 
{ 
    try 
    { 
     SqlCommand cmd = new SqlCommand(query, conn); 
     SqlDataAdapter da = new SqlDataAdapter(query, conn); 
     da.Fill(bookingData); 

     bookingData.DefaultView.Sort = "Specialty ASC"; 

     Specialty.DataSource = bookingData.DefaultView.ToTable(true, "Specialty"); 
     Specialty.DataTextField = "Specialty"; 
     Specialty.DataValueField = "Specialty"; 
     Specialty.DataBind(); 
     Specialty.Items.Remove("All Specialties"); 
     Specialty.Items.Insert(0, new ListItem("All Specialties", "All Specialties")); 

     da.Dispose(); 
    } 
    catch (Exception ex) 
    { 
     string error = ex.Message; 
    } 
} 

어떻게 다음을 수행하고 빠른 액세스를위한 캐시 테이블에 저장하는 Specialty 드롭 다운리스트의 인덱스 변화를 코딩 할 수 있습니다 :

protected void Specialty_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //re-populate the Topic dropdownlist to display all the topics based on the following criteria: 
     --> Where the Specialty column is either "All Specialties" OR "{specialty selected index value}" 
} 
+0

당신이 주제와 특수 DropDownLists를 채우는 데 사용하는 방법은 무엇입니까? Page_Load? 그리고 캐싱을 사용하지 않고 DataTable을 사용하고 있습니다. – Sam

+0

@Sam yea 처음 두 부분이 페이지로드시 실행됩니다. 전문 드롭 다운을 변경하면 두 가지 옵션을 토픽으로 업데이트하고 싶습니다. – SearchForKnowledge

답변

1

ViewState 또는 세션을 저장하십시오 (세션을 사용하지 않는 것이 좋습니다). 너무 무거울 경우. 그렇지 않으면 데이터베이스를 캐싱하거나 데이터베이스를 다시 쿼리하여 데이터를 다시 채울수록 좋습니다.

의 당신의 SelectedIndexChanged 경우에 그런

ViewState["bookingData2"] = bookingData2; // This should be before the following line 
Topic.DataSource = bookingData2.DefaultView.ToTable(true, "Topic"); 

Page_Load에서 다음과 같이의 ViewState에 bookingData2을 저장 가정 해 봅시다 할이

protected void Specialty_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //re-populate the Topic dropdownlist to display all the topics based on the following criteria: 
    // Where the Specialty column is either "All Specialties" OR "{specialty selected index value}" 
    DataTable bookingData2 = (DataTable)ViewState["bookingData2"]; 

    Topic.DataSource = bookingData2.Where(i => i.Specialty == "All Specialties" || i.Specialty == Specialty.SelectedValue).DefaultView.ToTable(true, "Topic"); // populate only with the Topic column 
    Topic.DataTextField = "Topic"; 
    Topic.DataValueField = "Topic"; 
    Topic.DataBind(); 
    Topic.Items.Insert(0, new ListItem("All Topics", "All Topics")); 

} 

업데이트 같은 - 캐시 된 객체와

마 우리가 사용했던 곳 대신에 Specialty_SelectedIndexChanged 이벤트에 이어 전에 ViewState.

if (HttpRuntime.Current.Cache["cachedtable2"] != null) 
{ 
    DataTable bookingData2 = HttpRuntime.Current.Cache["cachedtable2"] as DataTable; 
    // Rest of the code 
} 

이 코드를 시도하지 않았습니다. 문제가 있으면 알려주십시오.

+0

응답 해 주셔서 감사합니다! 나는 그것을 밖으로 테스트 할 것입니다 ... – SearchForKnowledge

+1

좋아, 내가 당신을 위해 일했는지 알려주십시오 – Sam

+0

그래서 코드의 첫 번째 부분에서 HTTPContext를 사용해야합니까? 오히려 ViewState를 사용합니까? 그것은 동시에 100 이상 ppl에 의해 액세스됩니다 응용 프로그램입니다 ... 그냥 속도에 대해 생각. – SearchForKnowledge

1

이 나를 위해 그것을 어떻게 해결 :

protected void Topic_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    try 
    { 
     if (Topic.SelectedIndex == 0) 
     { 
      string query = @"Specialty LIKE '%%'"; 

      DataTable cacheTable = HttpContext.Current.Cache["cachedtable"] as DataTable; 
      DataTable filteredData = cacheTable.Select(query).CopyToDataTable<DataRow>(); 

      filteredData.DefaultView.Sort = "Specialty ASC"; 

      Specialty.DataSource = filteredData.DefaultView.ToTable(true, "Specialty"); 
      Specialty.DataTextField = "Specialty"; 
      Specialty.DataValueField = "Specialty"; 
      Specialty.DataBind(); 
     } 
     else 
     { 
      string qpopulate = @"[Topic] = '" + Topic.SelectedItem.Value + "' or [Topic] = 'All Topics'"; //@"Select * from [DB].dbo.[table2] where [Specialty] = '" + Specialty.SelectedItem.Value + "' or [Specialty] = 'All Specialties'"; 
      DataTable cTable = HttpContext.Current.Cache["cachedtable2"] as DataTable; 
      DataTable fData = cTable.Select(qpopulate).CopyToDataTable<DataRow>(); 

      if (fData.Rows.Count > 0) 
      { 
       fData.DefaultView.Sort = "Specialty ASC"; 

       Specialty.DataSource = fData.DefaultView.ToTable(true, "Specialty"); 
       Specialty.DataTextField = "Specialty"; 
       Specialty.DataValueField = "Specialty"; 
       Specialty.DataBind(); 
      } 
      Specialty.Items.Insert(0, new ListItem("All Specialties", "All Specialties")); 
     } 
    } 
    catch (Exception ce) 
    { 
     string error = ce.Message; 
    } 
} 
+0

좋습니다. HttpContext.Current.Cache [ "cachedtable"]이 null인지 항상 확인하십시오. – Sam

관련 문제