protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ETHANConnectionString"].ConnectionString);
        string a1 = DropDownList1.SelectedItem.Value.ToString();        string sql = "select PowerID from DEPARTMENT where Department='"+a1+"'";
        DataTable dt = new DataTable();
        dt = conn.GetDataTable(sql);
        if (dt.Rows.Count!=0)
        {
            TextBox1.Text = dt.Rows[0][0].ToString();
        }
        else { Response.Write("<script>alert('插入失败!')</script>"); }    }错误 5 “System.Data.SqlClient.SqlConnection”不包含“GetDataTable”的定义,并且找不到可接受类型为“System.Data.SqlClient.SqlConnection”的第一个参数的扩展方法“GetDataTable”(是否缺少 using 指令或程序集引用?)

解决方案 »

  1.   

    conn.GetDataTable(sql);  SqlConnection没有你写的这个方法
      

  2.   

     protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        { 
            string a1 = DropDownList1.SelectedItem.Value.ToString();
            string sql = "select PowerID from DEPARTMENT where Department='" + a1 + "'";
            DataTable dt = new DataTable();
            dt = ReturnDataTable(sql);
            if (dt.Rows.Count != 0)
            {
                TextBox1.Text = dt.Rows[0][0].ToString();
            }
            else { Response.Write("<script>alert('插入失败!')</script>"); }
        }
        public static DataTable ReturnDataTable(string cmdtext)
        {
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["ETHANConnectionString"].ConnectionString;
            DataTable dt = new DataTable();
            SqlCommand cmd = new SqlCommand();
            cmd = new SqlCommand(cmdtext, cn);
            cmd.CommandType = CommandType.Text; ;
            SqlDataReader dr = null;
            using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(dr);
            }
            return dt;
        }
      

  3.   


    那我要是想达到
    DROPDOWNLIST  选择 DepartmentN 
       TEXTBOX  获取 PowerID 该如何是好?
      

  4.   

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ETHANConnectionString"].ConnectionString);
            string a1 = DropDownList1.SelectedItem.Value.ToString();        string sql = "select PowerID from DEPARTMENT where Department='"+a1+"'";
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
             sda.Fill(ds);
            if (ds.Tables[0].Rows.Count!=0)
            {
                TextBox1.Text =ds.Tables[0].Rows[0][0].ToString();
            }    }