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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrid datakeys error 2

Status
Not open for further replies.

compu66

Programmer
Dec 19, 2007
71
US
hey

Dim dataindex as interger=(CInt(datagrid.DataKeys(CInt(e.Item.ItemIndex))))

I am getting Index out of range exception for this.

Thanks for any help in advance.

 
Protected Sub dglogAbsences_ItemCommand(ByVal source As System.Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dglogAbsences.ItemCommand
If (e.CommandName = "Delete") Then

Dim strsql As String

dim dataindex as interger=(CInt(dglogAbsences.DataKeys(CInt(e.Item.ItemIndex))))
strsql = "Delete from PersonnelAbsences where personnelid=" &dataindex


Dim dh As DataHandler

dglogAbsences.DataSource = dh.ExecuteDML(strsql)
dglogAbsences.DataBind()
end sub


here I have to delte a row in datagrid. I am in my way to get the datakey of that row.
 
That is the codebehind.. Mark stated to post the source (HTML) of your page.
 
Also, should we assume this:
Code:
Dim dataindex as inte[red]r[/red]ger=(CInt(datagrid.DataKeys(CInt(e.Item.ItemIndex))))

is an accidental typo?
 
we are using framework 2.0

<asp:datagrid id="dglogAbsences" runat="server" gridlines="Vertical" cellpadding="0"
cssclass="insideContentWidth" visible="False"
allowpaging="True" autogeneratecolumns="False" AllowSorting="True" OnSortCommand="SortGrid"
AlternatingItemStyle-VerticalAlign="Middle" ItemStyle-VerticalAlign="Middle"
PageSize="25">
<AlternatingItemStyle CssClass="altRowStyle" VerticalAlign="Middle"></AlternatingItemStyle>
<ItemStyle CssClass="rowStyle" VerticalAlign="Middle"></ItemStyle>
<HeaderStyle CssClass="DatagridHeader"></HeaderStyle>
<FooterStyle CssClass="DatagridFooter"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="AbsenceID" Visible="False" HeaderText="ID" />
<asp:BoundColumn DataField="AbsenceStartDateDisp" SortExpression="AbsenceStartDate" HeaderText="Absence Start" />
<asp:BoundColumn DataField="AbsenceEndDateDisp" SortExpression="AbsenceEndDate" HeaderText="Absence End" />
<asp:BoundColumn DataField="Reason" SortExpression="Reason" HeaderText="Reason"></asp:BoundColumn>
<asp:BoundColumn DataField="Comments" HeaderText="Comments"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Edit">
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<!-- See codebehind file for column content. -->
<asp:label id="lblEdit" runat="server"></asp:label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="&lt;img src='../images/Cancel.gif' border='0'&gt;">
</asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
 
You first problem is that you haven't specified the DataKeyField property for the DataGrid.

The second problem is if you are using version 2.0 of the framework, you shouldn't be using a DataGrid control anyway. This has been superceded by the improved GridView control.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
hey all
If I assign datakeyfield as AbsenceID its giving this error

ERROR:A field or property with the name 'AbsenceID ' was not found on the selected data source.

But the datasource dgresult at 1 has the column AbsenceID and the method which it is calling i.e GetLogAbsences
has this query
Query
SELECT AbsenceID, Reason, Comments, convert("varchar",AbsenceStartDate,101)
as AbsenceStartDateDisp, convert("varchar",AbsenceEndDate,101) as AbsenceEndDateDisp
FROM PersonnelAbsences WHERE personnelID= 66 ORDER BY AbsenceStartDate


Datagrid :
Private Sub FilllogAbsencesGrid()
Dim dh As New DataHandlerAdmin
Dim dsResults As DataSet

dglogAbsences.Visible = True
'--- Get sorted equipment list data & display results in data grid.
dsResults = dh.GetLogAbsences(selStaff.SelectedValue, Session("sortColumn"), Session("sortOrder"))------------------------1

If dsResults.Tables(0).Rows.Count > 0 Then
dglogAbsences.Visible = True
lblAdd.Text = "<a href=""#"" onClick=""window.open('absenceAdd.aspx?act=1&personnelID=" & selStaff.SelectedValue & "&siteCode=" & selSite.SelectedValue & "', 'addAbsence', 'width=500,height=440,scrollbars=yes,menubar=no,toolbar=no')"">" & _
"+ Add Absence</a>"
lblMessage.Text = "Employee Absences"

dglogAbsences.DataSource = dsResults
dglogAbsences.DataBind()
Else
dglogAbsences.Visible = False
lblAdd.Text = "<a href=""#"" onClick=""window.open('absenceAdd.aspx?act=1&personnelID=" & selStaff.SelectedValue & "&siteCode=" & selSite.SelectedValue & "', 'addAbsence', 'width=500,height=440,scrollbars=yes,menubar=no,toolbar=no')"">" & _
"+ Add Absence</a>"
lblMessage.Text = "This Employee has no Absences."
End If



The column which iam assigning as datakeyfield is there in the datasource I dont understand why its giving error.


Thanks
 
Hi,

Well the "column which iam assigning as datakeyfield is there in the datasource" is there only for records which ask

--FROM PersonnelAbsences WHERE personnelID= 66-- ??

For any other personnelID it won't be there..

Is this correct?

Cheers, j

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top