I have a datatable which is having n no. of rows and each row has a unique field say ID.
Now I want to get a particular row for a specified ID.
eg. there are 5 rows in a datatable and the id's are
1,2,3,4 and 5 and if I want to select a row having ID = 4
What will be the code snippet for that?
Does anybody have any idea?
Thanks in advance,

Recommended Answers

All 16 Replies

You will need to modify your select statement. Are you using SQL table as your back end database?

i cant fully understand ur question as for as concerned i tried
select *from tablename where id='selectedid'

Actually what I m trying to do is I m firing a query in SQL Server 2005 to retrieve records for selected criteria.
Based on the selection the query result I m taking that in a datatable and I have a GridView where this list of results is displayed.
Now there is a hyperlink in the Grid to select the detailed information about a particular result.
eg. In the query result I get 5 records matching the selection criteria, which are stored in a datatable.
now these reults are displayed in the grid and when i click on the hyperlink of a particular row then the records of that row should get selected and the detailed info should get displayed in another page.
For this particular row selection I want a row from the datatable.

Hi Bhavna. Hw do u do?

Well, first of all I would like to repeat what you've asked so that it'll be, you know, better for me to give my view...

You are trying to select records of a particular field from a datatable displayed in a grid, through a hyperlink and also the datatable is previously populated using a select query from a SQL Server 2005 and the data are displayed in a grid,aren't you?


In this context, I would like to say that when you click the hyperlink a sub routine should be executed wherein you provide another select query which selects data from the datatable on the basis of the clicked field. Moreover, in the grid itself you can have the cell address. Why not you try using those cell addresses so that you can display records of tyhe particular field you wanted.

Hope my views help you.

hi sis,
i give the code here
for asp page

<asp:GridView DataKeyNames="nameid" OnRowCommand="list" ID="grid1" AutoGenerateColumns="false" runat="server" >
    <Columns>
    <asp:BoundField DataField="nameperson" HeaderText="firstname" />
    <asp:BoundField DataField="lastnameperson" HeaderText="lastname" />
   
    <asp:ButtonField ButtonType="Button" CommandName="list" HeaderText="list" />
    </Columns>
    </asp:GridView>
this is for asp.cs page
 protected void list(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "list")
        {
            int rowindex = Convert.ToInt32(e.CommandArgument);
            GridViewRow selectedrow = grid1.Rows[rowindex];
            int key = Convert.ToInt32(grid1.DataKeys[selectedrow.RowIndex].Value);

            SqlConnection con = new SqlConnection("Server=BDL-111\\BDLSQL2000;User id=sa;Password=Sqladmin;DataBase=Training;");
            con.Open();
            SqlCommand cmd = new SqlCommand("select *from dani where nameid=" + key + "", con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                first.Text = Convert.ToString(dr.GetString(1));
                last.Text = Convert.ToString(dr.GetString(2));
            }


        }

    }

i print the name in lables name as first and last
just copy and run it

Hi Bhavna. Hw do u do?

Well, first of all I would like to repeat what you've asked so that it'll be, you know, better for me to give my view...

You are trying to select records of a particular field from a datatable displayed in a grid, through a hyperlink and also the datatable is previously populated using a select query from a SQL Server 2005 and the data are displayed in a grid,aren't you?


In this context, I would like to say that when you click the hyperlink a sub routine should be executed wherein you provide another select query which selects data from the datatable on the basis of the clicked field. Moreover, in the grid itself you can have the cell address. Why not you try using those cell addresses so that you can display records of tyhe particular field you wanted.

Hope my views help you.

After that should I get all the details that I got from the query output datatable?
since I m showing only 4-5 fields from datatable in GridView and all the details say 50 fields are shown after the clicking of hyperlink i.e. in another page

I have a datatable which is having n no. of rows and each row has a unique field say ID.
Now I want to get a particular row for a specified ID.
eg. there are 5 rows in a datatable and the id's are
1,2,3,4 and 5 and if I want to select a row having ID = 4
What will be the code snippet for that?
Does anybody have any idea?
Thanks in advance,

hewy bhavna...
me too having same problem like yours...
now do u have the solution for ur problem??if yes then pl let me know..
thnks

Actually what I m trying to do is I m firing a query in SQL Server 2005 to retrieve records for selected criteria.
Based on the selection the query result I m taking that in a datatable and I have a GridView where this list of results is displayed.
Now there is a hyperlink in the Grid to select the detailed information about a particular result.
eg. In the query result I get 5 records matching the selection criteria, which are stored in a datatable.
now these reults are displayed in the grid and when i click on the hyperlink of a particular row then the records of that row should get selected and the detailed info should get displayed in another page.
For this particular row selection I want a row from the datatable.

hey bhavna...
me too having same problem like yours...
now do u have the solution for ur problem??if yes then pl let me know..
thnks

hey bhavna...
me too having same problem like yours...
now do u have the solution for ur problem??if yes then pl let me know..
thnks

Hi gunj,
Can u explain me the problem in bit detail?

Hi gunj,
Can u explain me the problem in bit detail?

hey bhavna...my problem is same like you.....
This is forum submitted by you....
Actually what I m trying to do is I m firing a query in SQL Server 2005 to retrieve records for selected criteria.
Based on the selection the query result I m taking that in a datatable and I have a GridView where this list of results is displayed.
Now there is a hyperlink in the Grid to select the detailed information about a particular result.
eg. In the query result I get 5 records matching the selection criteria, which are stored in a datatable.
now these reults are displayed in the grid and when i click on the hyperlink of a particular row then the records of that row should get selected and the detailed info should get displayed in another page.


me too tryng to do the same.....i ant to display all detailed information on the next page corresponding to particular row on clicking of an hyperlink that is attached in my grid.
thnksss..

hi bavana,
i think u r using template field for hyper link so u can do like this

<asp:Hyperlink id="something" text="more" navigateurl='<%# "~/moredetails.aspx?id="+Eval("uniquekey") %>' target="_bank"/>

use this hyper link in itemtemplate and unquekey is the id in db which u want pass as querystring to nxtpage...in next page u have uniqueid...so goto db for retriving ful details...hope it will be helpful...

hi bavana,
i think u r using template field for hyper link so u can do like this

<asp:Hyperlink id="something" text="more" navigateurl='<%# "~/moredetails.aspx?id="+Eval("uniquekey") %>' target="_bank"/>

use this hyper link in itemtemplate and unquekey is the id in db which u want pass as querystring to nxtpage...in next page u have uniqueid...so goto db for retriving ful details...hope it will be helpful...

but there is some problem in my sql database..i cant insert data in each field.so dere is not unique key.. now how can i retrive all info on newpage.aspx(based on the results coming on search.aspx page in the datagrid)


here is the code

<asp:datagrid id="DataGrid" runat="server" Width="83%" DataKeyField="ROW_ID" HeaderStyle-BackColor="#8cd1a2" BorderColor="White" HeaderStyle-BorderColor="black"
AutoGenerateColumns="False" AllowPaging="True" CellPadding="3" style="z-index: 100; left: 12px; position: absolute; top: 796px; overflow: auto;" Height="1px" OnSelectedIndexChanged="DataGrid_SelectedIndexChanged" AllowSorting="True" BackColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Overline="False" Font-Size="Medium" Font-Strikeout="False" Font-Underline="False" GridLines="None" HorizontalAlign="Left" PageSize="40" OnPageIndexChanged="DataGrid_PageIndexChanged" OnItemDataBound="DataGrid_ItemDataBound" >
<Columns>

<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" BorderColor="Black"></HeaderStyle>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" BorderColor="Black"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name")%>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="View Abstract ">
<ItemTemplate>
<asp:HyperLink ID="hyperlink1" Text='<%# Eval(" View Abstract") %>' NavigateUrl="~/NewAbs.aspx" runat="server" >
<%# DataBinder.Eval(Container.DataItem, " View Abstract")%>

</asp:HyperLink>

</ItemTemplate>
</asp:TemplateColumn>

but there is some problem in my sql database..i cant insert data in each field.so dere is not unique key.. now how can i retrive all info on newpage.aspx(based on the results coming on search.aspx page in the datagrid)


here is the code

<asp:datagrid id="DataGrid" runat="server" Width="83%" DataKeyField="ROW_ID" HeaderStyle-BackColor="#8cd1a2" BorderColor="White" HeaderStyle-BorderColor="black"
AutoGenerateColumns="False" AllowPaging="True" CellPadding="3" style="z-index: 100; left: 12px; position: absolute; top: 796px; overflow: auto;" Height="1px" OnSelectedIndexChanged="DataGrid_SelectedIndexChanged" AllowSorting="True" BackColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Overline="False" Font-Size="Medium" Font-Strikeout="False" Font-Underline="False" GridLines="None" HorizontalAlign="Left" PageSize="40" OnPageIndexChanged="DataGrid_PageIndexChanged" OnItemDataBound="DataGrid_ItemDataBound" >
<Columns>

<asp:TemplateColumn HeaderText="Name">
<HeaderStyle Font-Bold="True" HorizontalAlign="Left" BorderColor="Black"></HeaderStyle>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" BorderColor="Black"></ItemStyle>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Name")%>
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="View Abstract ">
<ItemTemplate>
<asp:HyperLink ID="hyperlink1" Text='<%# Eval(" View Abstract") %>' NavigateUrl="~/NewAbs.aspx" runat="server" >
<%# DataBinder.Eval(Container.DataItem, " View Abstract")%>

</asp:HyperLink>

</ItemTemplate>
</asp:TemplateColumn>

Hi gunj,
U have two ways to do, first what I thought earlier and the second which I implemented

1) First of all I need to know that on which criteria u r searching? Anyway if ur SP is querying with that and gives u the output as datatable, u can send this datatable in session to the next page and get the data from there to display the info after clicking the hyperlink. If u have any unique value in the gridview (in my case i had a pnumber field was unique) I looped out the records in the gridview and set hyperlink's navigate URL as the pnumber based upon which it fetches only the specified pnumber and displayed records accordingly.

2) Second which I implemented is same looped out the rows in grid, navigated the hyperlink to the pnumber and sent this pnumber to the next page as a querystring and based on that querystring I had a method in my application which fetches all records based on that particular pnumber. I got all records and displayed that .

hi dear.can u pl paste the code..it will help me...
actually my serach is based on the four field.--search by name
search by thesis
search by keywords
search by year...
according to these four different fields users can gt the results....
the results in the datagrid cOME OUT IN THIS WAY..

thesis number,title of thesis,name,year,adress,specialisation,hyperlink(for ABSTRACT)
i want now that this hyperlink which is in the last colummnn of the datagrid fetch the Abstarct from the table based on that row results and disply it on the next page..
im using sql queries for fetching the data from the table..

if you wants a specific row and you know its pk value you can use the Find method of rows
DataRow row = tableObj.Rows.Find(pkValue)
You can also use the Select method of table object which will return an array of rows
DataRow[] rows = tableObj.Select("","",rowStateEnum)

I completed and with the data rows that I want a DataTable.An index number to access a specific line or address. I have vb.net its easy, but I can not use C # to a number of.I want a specific line.I want to do something like = datarow Datatable.rows 4
Is the line number 4.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.