0

我是 .NET 的初学者,并且在我在 page_load 上定义的单选按钮索引更改事件处理程序中使用 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 sequel 连接时,我得到一个小 r

4

1 回答 1

0

您的查询RadioButtonList1_SelectedIndexChanged似乎不正确。有一个and没有 a where

Select rptdesc,rptdesctext,categoryid from report_description
and categoryid = ...
^^^ should be WHERE
于 2012-06-05T16:43:49.510 回答