2012-06-05 69 views
0

我是.NEt的初學者,並且難以在page_load上定義的單選按鈕索引更改的事件處理程序中使用sql連接。SQL連接變量不在當前上下文中

下面是我的代碼

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace Controls 
{ 
    public partial class Report_Selection : System.Web.UI.Page 
    { 


     protected void Page_Load(object sender, EventArgs e) 
     { 

      GridView1.HeaderStyle.Font.Bold = true; 
      RadioButtonList1.SelectedIndexChanged += new EventHandler(RadioButtonList1_SelectedIndexChanged); 
using (SqlConnection cnn = new SqlConnection("Data Source=DBSW9079;Initial Catalog=Underwriting;Integrated Security=SSPI;")) 

      { 
       SqlCommand cmd; 
       SqlDataReader sdr; 

       if (!IsPostBack) 
       { 
        cmd = new SqlCommand("select Categoryid,CategoryTitle from Report_Category", cnn); 
        cnn.Open(); 
        sdr = cmd.ExecuteReader(); 
        SelectCategorydlist1.DataSource = sdr; 
        SelectCategorydlist1.DataTextField = "CategoryTitle"; 
        SelectCategorydlist1.DataValueField = "categoryid"; 
        SelectCategorydlist1.DataBind(); 
        cnn.Close(); 

       } 
       else 
       { 
        //It's a Post back 
        //make the grid visible and fill it 

        GridView1.Visible = true; 
        RadioButtonList1.SelectedValue = "1"; 
        cmd = new SqlCommand("Select rptdesc,rptdesctext,categoryid from report_description " + "where categoryid != 99999" 
         + "and categoryid = " + Convert.ToInt32(SelectCategorydlist1.SelectedValue).ToString(), cnn); 
        cnn.Open(); 
        sdr = cmd.ExecuteReader(); 
        GridView1.DataSource = sdr; 
        GridView1.DataBind(); 

        sdr.Close(); 





        { 


        } 

       } 






       } 

      } 

     void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e) 
     { 



      SqlCommand cmd1; 
      SqlDataReader sdr1; 
      if (RadioButtonList1.SelectedIndex.Equals(1)) 
      { 
       RadioButtonList1.ClearSelection(); 
       cmd1 = new SqlCommand("Select rptdesc,rptdesctext,categoryid from report_description " 
         + "and categoryid = " + Convert.ToInt32(SelectCategorydlist1.SelectedValue).ToString(), cnn); 
       cnn.Open(); 
       sdr1= cmd1.ExecuteReader(); 
       GridView1.DataSource = sdr1; 
       GridView1.DataBind(); 

       sdr1.Close(); 
      } 

     } 







     } 
    } 

在上面的代碼時,我使用在事件處理程序的CNN續集連接我得到一個小[R

+0

順便說一句,爲什麼你有page_load功能?你應該只從page_load控制數據綁定'if(!IsPostback)'。其餘的應該在適當的事件處理程序中完成。那麼爲什麼你要讓網格可見並且在回發時將其綁定呢?什麼導致了回傳?應該處理此事件以調用數據綁定網格並切換可見性的方法。 –

+0

所以當頁面被加載時,下拉列表會被填充,當下拉值發生變化時,頁面會回傳並將數據檢索到網格控件中。 Iam是一個新手,在這個階段嘗試一些事情來了解它是如何工作的。 –

+0

然後處理DropDownList的[SelectedIndexChanged](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.selectedindexchanged.aspx)事件並在那裏填充網格。 –

回答

0

你在RadioButtonList1_SelectedIndexChanged查詢似乎是不正確的。有一個and沒有where

Select rptdesc,rptdesctext,categoryid from report_description 
and categoryid = ... 
^^^ should be WHERE 
相關問題