SQL连接变量不在当前上下文中

时间:2012-06-05 16:41:35

标签: asp.net

我是初学者。在使用我在page_load上定义的单选按钮索引更改的eventhandler时使用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

1 个答案:

答案 0 :(得分:0)

RadioButtonList1_SelectedIndexChanged中的查询似乎不正确。有and没有where

Select rptdesc,rptdesctext,categoryid from report_description
and categoryid = ...
^^^ should be WHERE