我正在从数据库中检索我的 toyName 并希望将其会话到下一页。我的会话没有任何问题,但我有这个错误,如下所示,我不确定我哪里出错了。我的 .aspx 页面中有 2 个链接按钮,这个有错误的链接按钮是我创建的第二个链接按钮。
错误信息:
Unable to cast object of type 'ASP.Catalogue_aspx' to type 'System.Web.UI.WebControls.LinkButton'.
源错误:
Line 13: protected void Page_Load(object sender, EventArgs e)
Line 14: {
Line 15: LinkButton LinkButton = (LinkButton)sender;
Line 16: int toyID = Convert.ToInt32(LinkButton.CommandName);
Line 17:
这是我的 page_load .cs 代码:
protected void Page_Load(object sender, EventArgs e)
{
LinkButton LinkButton = (LinkButton)sender;
int toyID = Convert.ToInt32(LinkButton.CommandName);
string strConnectionStr = ConfigurationManager.ConnectionStrings["ASPNETConnectionString"].ConnectionString;
string sql = "SELECT toyID, toyName FROM Toys WHERE toyID=@toyID";
SqlConnection myConnect = new SqlConnection(strConnectionStr);
myConnect.Open();
SqlCommand command = new SqlCommand(sql, myConnect);
command.Parameters.AddWithValue("@toyID", toyID);
SqlDataReader dr = command.ExecuteReader();
while (dr.Read())
{
Label1.Text = dr["toyName"].ToString();
}
dr.Close();
myConnect.Close();
}