473,322 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

problem in getting row index

Hi all,

I am displaying a gridview containing an UPDATE button. On clicking the button i need to redirect to another page. Here I am unable to find the row index of a clicked record button.
If i find it then i can get the details of that record and direct to another page.

I tried this but did'nt work...
Expand|Select|Wrap|Line Numbers
  1. if (e.CommandName.Equals("UPDATE"))
  2.         {
  3.             int x = GridView1.SelectedIndex;
  4.             string key;
  5.          // my primary key ( in column 2) to get that record
  6.             key = GridView1.Rows[x].Cells[2].Text;
  7.             string query;
  8.             query = "select * from tblStudentDetails1 where AdmissionNumber='" + key + "'";
  9.             SqlConnection objCon1 = new SqlConnection(con);
  10.             SqlDataAdapter objDa1 = new SqlDataAdapter(query, objCon1);
  11.             DataTable objDt1 = new DataTable();
  12.             objDa1.Fill(objDt1);
  13.             Session["students"] = objDt1;
  14.             Response.Redirect("admissionPage.aspx");
  15.         }
Apr 2 '09 #1
4 2364
At GridView1_RowCommand, the row number is e.ComandArgument;
Expand|Select|Wrap|Line Numbers
  1.         protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  2.         {
  3.             string strRow = e.CommandArgument.ToString();
  4.             switch (e.CommandName)
  5.             {
  6.                 case "New" :                
  7.                     break;
  8.                 case "Edit":
  9.                     break;
  10.                 case "Update":
  11. ..etc
  12.  
Apr 2 '09 #2
Hi boss i think you did not get what is my requirement..
For better understanding you can see the image..

In the grid if the user clicks the update button then i need to know the particular row number .
here the column index of "Admission number" is known if i know the row index then i will read the text in the grid cell like(104) and get the record from datatable and redirect to another page.
The major problem is getting only row index.

I will be greatful if any body helps me..

Expand|Select|Wrap|Line Numbers
  1. if (e.CommandName.Equals("UPDATE"))
  2. {
  3. int x=GridView1.SelectedRow.RowIndex;
  4. string key;
  5. // my primary key ( in column 2) to get that record
  6. key = GridView1.Rows[x].Cells[2].Text;
  7. string query;
  8. query = "select * from tblStudentDetails1 where AdmissionNumber='" + key + "'";
  9. SqlConnection objCon1 = new SqlConnection(con);
  10. SqlDataAdapter objDa1 = new SqlDataAdapter(query, objCon1);
  11. DataTable objDt1 = new DataTable();
  12. objDa1.Fill(objDt1);
  13. Session["students"] = objDt1;
  14. Response.Redirect("admissionPage.aspx");
  15. }
Attached Images
File Type: jpg grid.jpg (19.4 KB, 169 views)
Apr 4 '09 #3
When update, edit or delete is fired thru

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

the e.CommandArgument contains the row number. An example is here

If you set a breakpoint what do you see in e.CommandArgument? I assume your callback is that RowCommand one. If you are using
protected void GridView1.RowEditing then the row number is e.NewEditIndex
Apr 6 '09 #4
yeah it really solved my problem before your post.
Iused it like this....

Expand|Select|Wrap|Line Numbers
  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  2.     {
  3.         if (e.CommandName.Equals("UPDATE"))
  4.         {
  5.             int x = int.Parse(e.CommandArgument.ToString());
  6.             string key;
  7.             key = GridView1.Rows[x].Cells[2].Text;
  8.             string query;
  9.             query = "select * from tblStudentDetails1 where AdmissionNumber='" + key + "'";
  10.             SqlConnection objCon1 = new SqlConnection(con);
  11.             SqlDataAdapter objDa1 = new SqlDataAdapter(query, objCon1);
  12.             DataTable objDt1 = new DataTable();
  13.             objDa1.Fill(objDt1);
  14.             Session["students"] = objDt1;
  15.             Response.Redirect("admissionPage.aspx");
  16.         }
Many thanks for your post...
I got this idea after seeing your previous post.....
It made me easy..
Apr 6 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mr_burns | last post by:
Hi, i am getting the following errors when running my php script: Warning: Cannot send session cookie - headers already sent by (output started at ...
2
by: Dariusz | last post by:
I am trying out a fully CSS-P layout (first time with floats) and am having problems with getting certain DIV's from displaying properly - or rather - they are being completely ignored. Below is...
4
by: Vannela | last post by:
I want to open a new file and keep on appending the text depending on some condition. I have used this code but it is not appeneding properly It is giving some numbers in the output file...
7
by: Ankit Aneja | last post by:
I put the code for url rewrite in my Application_BeginRequest on global.ascx some .aspx pages are in root ,some in folder named admin and some in folder named user aspx pages which are in user...
0
by: Lisa Jones | last post by:
Hi Has anyone see this problem in ADO.Net when deleting a record (assuming that the table has identity increment Index) result in getting wrong index key if right after adding the record the table...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
1
by: KShapiro | last post by:
Hi All, I am trying to assist my wife, by making a simple menu system for the website. The web page is a .php4 file and does an include of the navigation file. Before it includes that file at...
5
by: lightgram | last post by:
Hi I have a problem, which after browsing through Google, seems to be fairly common. However having tried most suggestions I am still getting the problem. I have a menu bar across the top...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
10
by: biplab | last post by:
Sorry to bother again u people with the same code.....but another astonishing problem arising....please help me.... #include <stdio.h> #include <stdlib.h> #include <dos.h> #include <mem.h>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.