User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 329,727 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,540 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 179 | Replies: 6
Reply
Join Date: Sep 2006
Posts: 93
Reputation: bhavna_816 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhavna_816 bhavna_816 is offline Offline
Junior Poster in Training

How to get a datarow from a datable in C#?

  #1  
5 Days Ago
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,
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Bristol, UK
Posts: 919
Reputation: majestic0110 is on a distinguished road 
Rep Power: 2
Solved Threads: 30
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Posting Shark

Re: How to get a datarow from a datable in C#?

  #2  
5 Days Ago
You will need to modify your select statement. Are you using SQL table as your back end database?
If you have a quality, be proud of it and let it define you. Add it to the world!
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: vmadhu_ece is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vmadhu_ece vmadhu_ece is offline Offline
Newbie Poster

Re: How to get a datarow from a datable in C#?

  #3  
5 Days Ago
i cant fully understand ur question as for as concerned i tried
select *from tablename where id='selectedid'
Reply With Quote  
Join Date: Sep 2006
Posts: 93
Reputation: bhavna_816 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhavna_816 bhavna_816 is offline Offline
Junior Poster in Training

Re: How to get a datarow from a datable in C#?

  #4  
5 Days Ago
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.
Reply With Quote  
Join Date: May 2008
Posts: 17
Reputation: ardeezstyle is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
ardeezstyle ardeezstyle is offline Offline
Newbie Poster

Re: How to get a datarow from a datable in C#?

  #5  
5 Days Ago
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.
Reply With Quote  
Join Date: May 2008
Posts: 4
Reputation: vmadhu_ece is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
vmadhu_ece vmadhu_ece is offline Offline
Newbie Poster

Re: How to get a datarow from a datable in C#?

  #6  
4 Days Ago
hi sis,
i give the code here
for asp page
  1. <asp:GridView DataKeyNames="nameid" OnRowCommand="list" ID="grid1" AutoGenerateColumns="false" runat="server" >
  2. <Columns>
  3. <asp:BoundField DataField="nameperson" HeaderText="firstname" />
  4. <asp:BoundField DataField="lastnameperson" HeaderText="lastname" />
  5.  
  6. <asp:ButtonField ButtonType="Button" CommandName="list" HeaderText="list" />
  7. </Columns>
  8. </asp:GridView>
  9. this is for asp.cs page
  10. protected void list(object sender, GridViewCommandEventArgs e)
  11. {
  12. if (e.CommandName == "list")
  13. {
  14. int rowindex = Convert.ToInt32(e.CommandArgument);
  15. GridViewRow selectedrow = grid1.Rows[rowindex];
  16. int key = Convert.ToInt32(grid1.DataKeys[selectedrow.RowIndex].Value);
  17.  
  18. SqlConnection con = new SqlConnection("Server=BDL-111\\BDLSQL2000;User id=sa;Password=Sqladmin;DataBase=Training;");
  19. con.Open();
  20. SqlCommand cmd = new SqlCommand("select *from dani where nameid=" + key + "", con);
  21. SqlDataReader dr = cmd.ExecuteReader();
  22. while (dr.Read())
  23. {
  24. first.Text = Convert.ToString(dr.GetString(1));
  25. last.Text = Convert.ToString(dr.GetString(2));
  26. }
  27.  
  28.  
  29. }
  30.  
  31. }
i print the name in lables name as first and last
just copy and run it
Last edited by peter_budo : 1 Day Ago at 5:12 am. Reason: Keep It Organized - please use [code] tags
Reply With Quote  
Join Date: Sep 2006
Posts: 93
Reputation: bhavna_816 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
bhavna_816 bhavna_816 is offline Offline
Junior Poster in Training

Re: How to get a datarow from a datable in C#?

  #7  
4 Days Ago
Originally Posted by ardeezstyle View Post
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Marketplace (Sponsored Links)
Thread Tools Display Modes

Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 3:35 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC