Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Invalid Cast - where am i going wrong? 2

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hi all,
I have a .NET page w/DG control and I have two buttons - I want to fire a custom event for each....Only, I get an InvalidCastException when I try to check the commandType of the button...

ASPX ==>
Code:
<%@ Page language="c#" Codebehind="AddVenueContact.aspx.cs" AutoEventWireup="false" Inherits="vfcom.AddVenueContact" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<title>AddVenueContact</title>
		<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" Content="C#">
		<meta name="vs_defaultClientScript" content="JavaScript">
		<meta name="vs_targetSchema" content="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
	</HEAD>
	<body MS_POSITIONING="GridLayout">
		<form id="Form1" method="post" runat="server">
			<asp:DataGrid id="dgContacts" runat="server" AutoGenerateColumns="False" OnItemCommand="Item_Click">
				<Columns>
					<asp:BoundColumn HeaderText="Product ID" DataField="strName" />
					<asp:ButtonColumn ButtonType="PushButton" Text="Edit" CommandName="EditContacts" />
					
					<asp:BoundColumn HeaderText="Name" DataField="strPosition" />
					<asp:BoundColumn HeaderText="Description" DataField="strEmail" />
				</Columns>
			</asp:DataGrid>
			<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 320px" runat="server">Label</asp:Label>
		</form>
	</body>
</HTML>

CodeBehind ==>
Code:
using System;
using System.Collections;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.ApplicationBlocks.Data;

namespace vfcom
{
	/// <summary>
	/// Summary description for AddVenueContact.
	/// </summary>
	public class AddVenueContact : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.DataGrid dgContacts;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				DumpContacts();
			}
		}

		private void DumpContacts()
		{
			Session["ID"] = 1;
			if (Session["ID"]!= null)
			{
				string strVenueID = Session["ID"].ToString();
				//create the connection string and sql to be executed
				string strSql = "select strFirstName + ' ' + strLastName as strName, strPosition, strEmail FROM Venue_Contact WHERE VenueID = " + strVenueID ;
				string strConnTxt = ConfigurationSettings.AppSettings["connString"];
	
				dgContacts.DataSource = SqlHelper.ExecuteReader(strConnTxt, CommandType.Text, strSql);
				dgContacts.DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
		 
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		public void Item_Click(Object src, DataGridCommandEventArgs e )
		{
			 
			[red]if (((LinkButton)e.CommandSource).CommandName == "EditContacts") [/red]
			{
				Label1.Text="worked!";
			}
		}

 
 
	}
}

The line in Red is where I get the exception.

Here is the exception direct from the page + stack trace

Code:
Specified cast is not valid. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error: 


Line 72: 		{
Line 73: 			 
Line 74: 			if (((LinkButton)e.CommandSource).CommandName == "EditContacts") 
Line 75: 			{
Line 76: 				Label1.Text="worked!";
 

Source File: c:\inetpub\[URL unfurl="true"]wwwroot\vfcom\addvenuecontact.aspx.cs[/URL]    Line: 74 

Stack Trace: 


[InvalidCastException: Specified cast is not valid.]
   vfcom.AddVenueContact.Item_Click(Object src, DataGridCommandEventArgs e) in c:\inetpub\[URL unfurl="true"]wwwroot\vfcom\addvenuecontact.aspx.cs:74[/URL]
   System.Web.UI.WebControls.DataGrid.OnItemCommand(DataGridCommandEventArgs e)
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()


Thanks for the help, it is appreciated!

All hail the INTERWEB!
 
Well, since your ButtonColumn is set to use "PushButton", the CommandSource is not a LinkButton, which is what you are casting to.
Is that cast really needed? Does this work:
Code:
  if (e.CommandName == "EditContacts")




[pipe]
Share your knowledge! -
 
Will do - This is somethin I'll have to try tonight when I get home - thanks for your help - I'll post my results later on!

All hail the INTERWEB!
 
Dragon,
Your solution worked! Thank you! ChipH, I am now checking for Null values as well...I appreciate the help!

All hail the INTERWEB!
 
I will make that correction when I get home tonight - Thank you!

All hail the INTERWEB!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top