查询时提示ExecuteReader: CommandText 属性尚未初始化
public partial class _Default : System.Web.UI.Page
{
string sqlstr;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
sqlstr = "select * from bts ";
gridsql();
}
protected void Button1_Click(object sender, EventArgs e)
{
string txtbox = TextBox1.Text;
if (DropDownList1.SelectedValue == "-1 ")
{
if (!Page.IsPostBack)
sqlstr = "select * from bts where Pt_No= 'a ' ";
gridsql();
}
}
public void gridsql()
{
string connStr = ConfigurationManager.ConnectionStrings[ "BTS "].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
DataSet ds = new DataSet();
SqlCommand comm = new SqlCommand();
SqlDataAdapter sqlad = new SqlDataAdapter();
comm.Connection = conn;
comm.CommandText = sqlstr;
sqlad.SelectCommand = comm;
Response.Write(sqlad.SelectCommand.CommandText);
sqlad.Fill(ds, "bts ");
GridView1.DataSource = ds.Tables[ "bts "];
GridView1.DataBind();
}
------解决方案--------------------protected void Page_Load(object sender, EventArgs e)