public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
DataSet dsPlant = new DataSet();
SqlDataAdapter sdap = new SqlDataAdapter();
SqlCommand sc = new SqlCommand(); protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int i = 0;
if (con.State == ConnectionState.Closed)
con.Open();
try
{ sc.CommandText = "select plname,OrderNo from GS_pl_Plant order by plorderby,orderno";
sc.Connection = con;
sdap.SelectCommand = sc;
sdap.Fill(dsPlant,"plant");
}
finally
{
con.Close();
}
DropDownList1.Items.Clear();
for (i = 0; i <= dsPlant.Tables[0].Rows.Count - 1; i++)
{
DropDownList1.Items.Add(dsPlant.Tables[0].Rows[i][0].ToString());
}
}
}
protected void Button2_Click(object sender, EventArgs e)
{
//int si = DropDownList1.SelectedIndex;
Label3.Text = dsPlant.Tables[0].Rows[3][0].ToString(); //此处发生错误
}
}(本人刚从DELPHI转C#,刚开始写程序就遇到了问题,求解决)
上面程序中是做一个查询,然后添加到DropDownList,由于DropDownList显示字段并不是键字段,于是我想根据DropDownList1.SelectedIndex去访问DATASET中的对应行。
访问时发生了一个错误,“无法找到表 0”,我知道是表填充后被提前释放了,请教要怎样我在Button2的时候才能引用,也就是该DataTable在该页面的全局有效,不要在设计期添加DATASET的情况下。先谢过了!
不知道能不能给我推荐本好书?