Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
ADO.NET For discussion about ADO.NET.  Topics such as question regarding the System.Data namespace are appropriate.  Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 3rd, 2005, 07:30 AM
Authorized User
 
Join Date: Jun 2003
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to sandeep
Default DataGrid Problem

Hi!
    I am having a datagrid whose ItemTemplate looks like the following:

<asp:TemplateColumn HeaderText="Supervisior">

<ItemTemplate>
    <%# DataBinder.Eval(Container.DataItem,"SupName")%>
</ItemTemplate>

<EditItemTemplate>

<asp:DropDownList ID="drpReportsTo" DataTextField="Name" DataValueField="EmployeeID" DataSource = '<%# GetEmployees() %>'
SelectedIndex = '<%# GetSelEmployee((int)DataBinder.Eval(Container.Data Item,"ReportsTo")) %>' Runat = "server">
</asp:DropDownList>

</EditItemTemplate>

</asp:TemplateColumn>

When i click the edit button in the datagrid I am passing the value of ReportsTo to GetSelEmployee() function in code behind so that the dropdownlist in the EditItemTemplate selects the appropriate Item. My problem is that in the database the ReportsTo column can be null. If that happens the code : ((int)DataBinder.Eval(Container.DataItem,"ReportsT o")) produces an error as it is not able to convert null to int. Can anyone let me know how i can check if ReportsTo is not null and only then run

SelectedIndex = '<%# GetSelEmployee((int)DataBinder.Eval(Container.Data Item,"ReportsTo")) %>'

Thanks in advance!

Regards
Sandeep Saran
 
Old June 3rd, 2005, 09:18 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I'd recommend creating a helper function in the page. Have it check the value and return something appropriate.

protected int GetIndex(object value){
  if(value == DBNull){
    return -1;
  } else {
    return (int)value
  }
}

Call that from the data binding:

SelectedIndex = '<%# GetSelEmployee(GetIndex(DataBinder.Eval(Container. DataItem,"ReportsTo"))) %>'

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with datagrid Shishir ASP.NET 1.0 and 1.1 Professional 1 January 6th, 2007 09:01 AM
Datagrid Problem monika.vasvani ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 03:09 AM
DataGrid Problem anujrathi VB.NET 2002/2003 Basics 2 June 15th, 2006 02:18 AM
DataGrid Problem anujrathi ASP.NET 2.0 Basics 1 June 12th, 2006 03:52 PM
Datagrid problem dotnetprogrammer VS.NET 2002/2003 1 February 17th, 2005 05:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.