-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Vijayan
Message 1 in Discussion
Hi, I am facing the following problem in "ItemDataBound" function. Can anyone help
me in this regard ? I have pasted here both the pages ie. ASPX and CS files. Thanks
& Regards,
Vijayakumar
==========================================================================
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 82: if (e.Item.ItemType == ListItemType.EditItem)
Line 83: {
Line 84: DataRowView drv = (DataRowView) e.Item.DataItem;
Line 85: String oldCategory = drv["Category"].ToString();
Line 86: ListBox ddl = (ListBox) e.Item.FindControl("Category");
==========================================================================
*************************** DGrid_DropDown.Aspx *****************************
========================================================================== <%@ Page
language="c#" Codebehind="DGird_DropDown.Aspx.cs" AutoEventWireup="false"
Inherits="Payroll.NewFolder1.DGird_DropDown" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>DGird_DropDown</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="DGird_DropDown" method="post" runat="server">
<asp:DataGrid id="DG1" style="Z-INDEX: 101; LEFT: 74px; POSITION: absolute; TOP:
68px" runat="server" Width="489px" AutoGenerateColumns="False" OnEditCommand="onEdit"
OnUpdateCommand="onUpdate" OnCancelCommand="onCancel" OnItemDataBound="ItemDataBound">
<Columns>
<asp:BoundColumn DataField="EId" ReadOnly="True"
HeaderText="EId"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="First Name">
<ItemTemplate>
<asp:Label Text='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,"FirstName")) %>' Runat=server
ID="lblFirstName" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="txtFirstName" Text='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,"FirstName")) %>' Width="30" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:Label Text='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,"LastName")) %>' Runat=server
ID="lblLastName" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox Runat=server ID="txtLastName" Text='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem,"LastName")) %>' Width="30" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<HeaderTemplate>
<b>Category </b>
</HeaderTemplate>
<ItemTemplate>
<asp:Label Width="200" Text='<%# DataBinder.Eval(Container.DataItem,
"CategoryName") %>' runat="server" ID="lblCategory"/>
</ItemTemplate>
<EditItemTemplate>
<asp:listbox SelectionMode="Single" ID="Category" runat="server" Rows="1"
DataSource = "<%# GetCategoryTable() %>" DataTextField="CategoryName"
DataValueField="CatID" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" />
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML> ==========================================================================
*************************** DGrid_DropDown.Aspx.cs ***************************
========================================================================== using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration; namespace Payroll.NewFolder1
{
/// <summary>
/// Summary description for DGird_DropDown.
/// </summary>
public class DGird_DropDown : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblCategory;
protected System.Web.UI.WebControls.DataGrid DG1;
protected System.Web.UI.WebControls.Label Category;
private void Page_Load(object sender, System.EventArgs e)
{
if ( ! Page.IsPostBack)
{
BindGrid();
}
} #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 private void BindGrid()
{
SqlConnection con = new
SqlConnection(ConfigurationSettings.AppSettings["conString"]);
con.Open();
SqlCommand cmd = new SqlCommand("select a.EID,a.FirstName,a.LastName,b.CategoryName
from Employees a,Categories b where a.CategoryId = b.CatId",con);
SqlDataReader reader = cmd.ExecuteReader();
DG1.DataSource=reader;
DG1.DataBind();
con.Close();
} public SqlDataReader GetCategoryTable ()
{
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["conString"]);
String sqlConnection = "select * from Categories";
SqlCommand sqlCommand = new SqlCommand(sqlConnection, conn);
conn.Open();
SqlDataReader sqlDataReader;
sqlDataReader = sqlCommand.ExecuteReader();
return sqlDataReader;
sqlDataReader.Close();
conn.Close(); }
public void ItemDataBound(Object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.EditItem)
{
DataRowView drv = (DataRowView) e.Item.DataItem;
String oldCategory = drv["Category"].ToString();
ListBox ddl = (ListBox) e.Item.FindControl("Category");
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText(oldCategory));
}
}
public void onEdit(Object source, DataGridCommandEventArgs e)
{
DG1.EditItemIndex = e.Item.ItemIndex;
BindGrid();
}
public void onUpdate(Object source, DataGridCommandEventArgs e)
{
}
public void onCancel(Object source, DataGridCommandEventArgs e)
{
} }
}
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]