我想在運行時綁定GridView
當DataSource
被選中,並且當用戶從DropDownList
選擇一個選項。但是選定的表或連接沒有正確完成。 請檢查下面的代碼,並給我適當的解決方案。網格視圖不顯示結果
public partial class index : System.Web.UI.Page
{
SqlConnection conn = new SqlConnection();
string option = "";
protected void Page_Load(object sender, EventArgs e)
{
option = selectProductdropdown.SelectedValue;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label2.Text = option;
if (option == "Books")
{
Label3.Text = option;
conn.ConnectionString = ConfigurationManager.ConnectionStrings["booksconnectionstring"].ConnectionString;
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT * from books", conn);
cmd.CommandType = CommandType.Text;
SqlDataAdapter reader = new SqlDataAdapter(cmd);
DataSet s = new DataSet();
reader.Fill(s);
GridView1.DataSource = s;
GridView1.DataBind();
conn.Close();
}
你可以在這裏把你的錯誤信息? – 2011-04-13 06:53:01