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

Find Data Record

Status
Not open for further replies.

shiggyshag

Programmer
Dec 14, 2001
227
GB
HI

How do i find a specific row in my dataset/datatable and the bind that to my Datagrid?

I want to find the ContactID and bind to that?

Cheers
 
Hi

Had a look at that i tried

Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = dt.Select("ContactID = 3")

cContacts.DataSource = foundRows '
cContacts.DataBind()

But now says

DataBinder.Eval: 'System.Data.DataRow' does not contain a property with the name ContactID.

any idea?
 
What line produces the error?

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

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
This one

cContacts.DataSource = foundRows

I check the length of foundRows and it equals 1 so it has a value

Cheers
 
OK, post your DataGrid code from the HTML page as well

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

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
'Page.RegisterClientScriptBlock("FrameRefresh", "<script language='javascript'>window.top.frames[2].location.reload(true);</script>")
If Not Page.IsPostBack Then
doDataBind()
cPrevious.Enabled = False
cFirst.Enabled = False
End If
End Sub

Sub doDataBind()

Dim objConn As New SqlConnection
Dim strsql As String

objConn = New SqlConnection(strConn)

Dim myCommand As New SqlDataAdapter("spContacts", objConn)

myDataset = New DataSet
myCommand.Fill(myDataset, "tContacts")

Dim dt As DataTable = myDataset.Tables("tContacts")

If dt.Rows.Count > 0 Then
If Session("FindContactID") > "" Then
Stop
Dim foundRows() As DataRow
' Use the Select method to find all rows matching the filter.
foundRows = dt.Select("ContactID >3")

cContacts.DataSource = foundRows
cContacts.DataBind()
Else
cContacts.DataSource = myDataset.Tables("tContacts").DefaultView
cContacts.DataBind()
End If
End If

Session("ContactID") = cContacts.DataKeys.Item(0)
Session("ContactName") = "Batten"
cRecordCount.Text = cContacts.CurrentPageIndex.ToString + 1 & " of " & dt.Rows.Count
objConn.Close()

End Sub



<%@ Page Language="vb" AutoEventWireup="false" Codebehind="fContacts.aspx.vb" Inherits="RubiconArgo.fContacts"%>
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<%@ Register TagPrefix="ie" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>fContacts</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content=" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table style="Z-INDEX: 101; LEFT: 200px; WIDTH: 248px; POSITION: absolute; TOP: 16px; HEIGHT: 31px">
<TR>
<TD><asp:button id="cFirst" runat="server" Text="|<" Width="41px" ToolTip="First"></asp:button><asp:button id="cPrevious" runat="server" Text="<" Width="41px" ToolTip="Previous"></asp:button><asp:textbox id="cRecordCount" runat="server" Width="72px" BorderStyle="None" ReadOnly="True"
BorderWidth="0px"></asp:textbox><asp:button id="cNext" runat="server" Text=">" Width="41px" ToolTip="Next"></asp:button><asp:button id="cLast" runat="server" Text=">|" Width="41px" ToolTip="Next"></asp:button></TD>
</TR>
</table>
<asp:datagrid id="cContacts" style="Z-INDEX: 102; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"
Width="592px" BorderStyle="None" DataKeyField="ContactID" Height="376px" AllowPaging="True"
PageSize="1" AutoGenerateColumns="False" GridLines="None">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<TABLE border="0">
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Title</DIV>
</TD>
<TD width="25%">
<asp:dropdownlist id="cTitle" tabIndex="1" runat="server" Width="96px" Height="15px">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="Mr">Mr</asp:ListItem>
<asp:ListItem Value="Mrs">Mrs</asp:ListItem>
<asp:ListItem Value="Miss">Miss</asp:ListItem>
<asp:ListItem Value="Dr">Dr</asp:ListItem>
</asp:dropdownlist></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout"></DIV>
</TD>
<TD width="25%">
<asp:textbox id="ContactID" tabIndex=2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ContactID") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Forename</DIV>
</TD>
<TD width="25%">
<asp:textbox id=cForename tabIndex=2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Forename") %>' Width="176px">
</asp:textbox></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">Address
1</DIV>
</TD>
<TD width="25%">
<asp:textbox id=cAddress1 tabIndex=9 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address1") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Surname</DIV>
</TD>
<TD>
<asp:textbox id=cSurname tabIndex=3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Surname") %>' Width="176px">
</asp:textbox></TD>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">Address
2</DIV>
</TD>
<TD>
<asp:textbox id=cAddress2 tabIndex=10 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address2") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Tel:
Daytime</DIV>
</TD>
<TD>
<asp:textbox id=cTelDaytime tabIndex=4 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.DaytimeTel") %>' Width="176px">
</asp:textbox></TD>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">Address&nbsp;3</DIV>
</TD>
<TD>
<asp:textbox id=cAddress3 tabIndex=11 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Address3") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Tel:
Evening</DIV>
</TD>
<TD>
<asp:textbox id=cTelEvening tabIndex=5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.EveningTel") %>' Width="176px">
</asp:textbox></TD>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">City</DIV>
</TD>
<TD>
<asp:textbox id=cCity tabIndex=12 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.City") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Mobile</DIV>
</TD>
<TD>
<asp:textbox id=cMobile tabIndex=6 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.mobile") %>' Width="176px">
</asp:textbox></TD>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">County</DIV>
</TD>
<TD>
<asp:textbox id=cCounty tabIndex=13 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.County") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">Fax</DIV>
</TD>
<TD>
<asp:textbox id=cFax tabIndex=7 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Fax") %>' Width="176px">
</asp:textbox></TD>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">Postcode</DIV>
</TD>
<TD>
<asp:textbox id=cPostcode tabIndex=14 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Postcode") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 19px" align="right" ms_positioning="FlowLayout">E-mail</DIV>
</TD>
<TD>
<asp:textbox id=cEmail tabIndex=8 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Email") %>' Width="176px">
</asp:textbox></TD>
</TD>
<TD align="right">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout">Country</DIV>
</TD>
<TD>
<asp:textbox id=cCountry tabIndex=15 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Country") %>' Width="176px">
</asp:textbox></TD>
</TR>
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 112px; HEIGHT: 24px" align="right" ms_positioning="FlowLayout">Lead
Create Date</DIV>
</TD>
<TD width="25%">
<asp:textbox id=cLeadCreateDate tabIndex=2 runat="server" Text='<%# left(DataBinder.Eval(Container, "DataItem.LeadCreateDate"),10) %>' Width="176px">
</asp:textbox></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout"></DIV>
</TD>
<TD width="25%"></TD>
</TR>
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 24px" align="right" ms_positioning="FlowLayout">Lead
Source</DIV>
</TD>
<TD width="25%">
<asp:dropdownlist id="cLeadSource" tabIndex="1" runat="server" Width="176px" Height="15px"></asp:dropdownlist></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout"></DIV>
</TD>
<TD width="25%"></TD>
</TR>
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 24px" align="right" ms_positioning="FlowLayout">Site
Interest</DIV>
</TD>
<TD width="25%">
<asp:dropdownlist id="cSiteInterest" tabIndex="1" runat="server" Width="176px" Height="15px"></asp:dropdownlist></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout"></DIV>
</TD>
<TD width="25%"></TD>
</TR>
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 24px" align="right" ms_positioning="FlowLayout">Budget
</DIV>
</TD>
<TD width="25%">
<asp:dropdownlist id="cBudget" tabIndex="1" runat="server" Width="176px" Height="15px"></asp:dropdownlist></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout"></DIV>
</TD>
<TD width="25%"></TD>
</TR>
<TR>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 80px; HEIGHT: 24px" align="right" ms_positioning="FlowLayout">Contact&nbsp;Stage
</DIV>
</TD>
<TD width="25%">
<asp:dropdownlist id="cContactStage" tabIndex="1" runat="server" Width="176px" Height="15px"></asp:dropdownlist></TD>
<TD align="right" width="25%">
<DIV style="DISPLAY: inline; WIDTH: 72px; HEIGHT: 16px" align="right" ms_positioning="FlowLayout"></DIV>
</TD>
<TD width="25%">
<asp:Button id="Button1" runat="server" Text="Button" Width="80px" CommandName="Update"></asp:Button></TD>
</TR>
</TABLE>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle Visible="False"></PagerStyle>
</asp:datagrid><ie:tabstrip id="ts1" style="Z-INDEX: 103; LEFT: 32px; POSITION: absolute; TOP: 432px" runat="server"
AutoPostBack="True" SepDefaultStyle="background-color:#FFFFFF;border-color:#AAAAAA;border-width:1px;border-style:solid;border-top:none;border-left:none;border-right:none"
TabSelectedStyle="color:#000000;background-color:#FFFFFF;border-bottom:none" TabHoverStyle="color:blue"
TabDefaultStyle="color:#aaaaaa;background-color:#EEEEEE;border-color:#AAAAAA;border-width:1px;border-style:Solid;font-weight:bold;font-family:Verdana;font-size:11px;height:21;width:79;text-align:center;">
<ie:Tab Text="Hold"></ie:Tab>
<ie:Tab Text="Sales"></ie:Tab>
<ie:Tab Text="Blah"></ie:Tab>
<ie:Tab Text="Blah"></ie:Tab>
<ie:Tab Text="Blah"></ie:Tab>
</ie:tabstrip><INPUT style="Z-INDEX: 104; LEFT: 32px; WIDTH: 104px; POSITION: absolute; TOP: 16px; HEIGHT: 32px"
onclick="window.open('fFindContacts.aspx','main1')" type="button" value="Contact Search">
</form>
</body>
</HTML>
 
The reason it is erroring now is because you have the following (and also other references) in your DataGrid HTML:
Code:
Text='<%# DataBinder.Eval(Container, "DataItem.ContactID") %>'
As you have used the Select command of the DataTable to produce an array of DataRows, the array doesn't have a column named ContactID unlike the original DataTable which did. Therefore the error is saying that "'System.Data.DataRow' does not contain a property with the name ContactID.

Does that make sense to you?

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

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Hi

IT makes sense but why doesnt the datarow now have these colomn names?

Is there a way around this??

Cheers
 
A DataRow cannot contain column names because it doesn't have the ability to create columns, hence the name DataRow. Think of it like a row of Data in a database; you would have a table that contains columns and you would also have rows that belong to this table. It is exactly the same in this scenario with DataTables and DataRows.

If you wanted you could always add the DataRows to another DataTable by using DataTable1.Rows.Add

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

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top