データベースからtoyNameを取得していて、次のページにセッションしたいと思っています。私のセッションには何も問題はありませんが、以下のようなエラーが発生し、どこで問題が発生したのかわかりません。.aspxページに2つのリンクボタンがあり、エラーが発生しているこのリンクボタンは、作成した2番目のlinkButtonです。
エラーメッセージ:
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();
}