I have search facility that displays on a gridview control. If a user searches for the lastname with the same value it throws 2 similar value but with different firstname, middlename and so on. The only problem I have here is that it only showed the first
record but not the second one. This pertains to display on a label control. I used for loop on my gridview. Please refer this screenshot:
http://imageshack.us/photo/my-images/717/70840416.jpg/
I need help on how to display the second value on my label control.
Here's my snippet:
for (int i = 0; i < gvStudName.Rows.Count; i++)
{
Label lbidnumber = (Label)gvStudName.Rows[i].FindControl("lblidno");
objConn.Open();
string query2 = "Select * from StudentEnrolmentFile where IDNumber='" + lbidnumber.Text + "'";
SqlCommand objqry = new SqlCommand(query2, objConn);
SqlDataReader drQry = objqry.ExecuteReader();
while (drQry.Read())
{
lbidno.Text = drQry["IDNumber"].ToString();
lbprog.Text = drQry["ProgCode"].ToString();
}
objConn.Close();
}
the problem here is that it's not showing the other record right below the gridview from my orginal post there are 2 records however it's only showing one record with id no. 201200002 it must
also show 201200004
as well but it's not.
while (drQry.Read())
{
lbidno.Text = drQry["IDNumber"].ToString();
lbprog.Text = drQry["ProgCode"].ToString();
}
is always covering the old text with the currect value from datareader. That's why you always get the only one row data. If you want to show all results, you can use another GridView to list all.
sheen_buhay
Member
712 Points
540 Posts
Could not show 2nd record
Apr 11, 2012 02:59 AM|LINK
Hi,
I have search facility that displays on a gridview control. If a user searches for the lastname with the same value it throws 2 similar value but with different firstname, middlename and so on. The only problem I have here is that it only showed the first record but not the second one. This pertains to display on a label control. I used for loop on my gridview. Please refer this screenshot: http://imageshack.us/photo/my-images/717/70840416.jpg/
I need help on how to display the second value on my label control.
Here's my snippet:
for (int i = 0; i < gvStudName.Rows.Count; i++)
{
Label lbidnumber = (Label)gvStudName.Rows[i].FindControl("lblidno");
objConn.Open();
string query2 = "Select * from StudentEnrolmentFile where IDNumber='" + lbidnumber.Text + "'";
SqlCommand objqry = new SqlCommand(query2, objConn);
SqlDataReader drQry = objqry.ExecuteReader();
while (drQry.Read())
{
lbidno.Text = drQry["IDNumber"].ToString();
lbprog.Text = drQry["ProgCode"].ToString();
}
objConn.Close();
}
Thanks.
SheenBuhay
mm10
Contributor
6445 Points
1187 Posts
Re: Could not show 2nd record
Apr 11, 2012 09:58 AM|LINK
The following adds text to the labels for each row in the reader:
for (int i = 0; i < gvStudName.Rows.Count; i++)
{
Label lbidnumber = (Label)gvStudName.Rows[i].FindControl("lblidno");
objConn.Open();
string query2 = "Select * from StudentEnrolmentFile where IDNumber='" + lbidnumber.Text + "'";
SqlCommand objqry = new SqlCommand(query2, objConn);
SqlDataReader drQry = objqry.ExecuteReader();
int i = 0;
while (drQry.Read())
{
if(i == 0){
lbidno.Text = drQry["IDNumber"].ToString();
lbprog.Text = drQry["ProgCode"].ToString();
++i;
}
else
{
lbidno.Text += "<br/> " + drQry["IDNumber"].ToString();
lbprog.Text += "<br/> " + drQry["ProgCode"].ToString();
}
}
objConn.Close();
}
tdmca
Contributor
2396 Points
661 Posts
Re: Could not show 2nd record
Apr 11, 2012 10:33 AM|LINK
unable to understand your problem
after typing value in textbox you click search
not it fetch matching two records from database
now what do you do and what is problem
sheen_buhay
Member
712 Points
540 Posts
Re: Could not show 2nd record
Apr 13, 2012 02:15 AM|LINK
@mm10,
i've tried your logic however it's not working. kindly help. thanks.
SheenBuhay
sheen_buhay
Member
712 Points
540 Posts
Re: Could not show 2nd record
Apr 13, 2012 02:18 AM|LINK
@tdmca,
the problem here is that it's not showing the other record right below the gridview from my orginal post there are 2 records however it's only showing one record with id no. 201200002 it must also show 201200004 as well but it's not.
SheenBuhay
basheerkal
Star
10672 Points
2426 Posts
Re: Could not show 2nd record
Apr 13, 2012 06:16 AM|LINK
Just for a check try....
Response.Write(drQry["IDNumber"].ToString() + "<br>") ;
(Talk less..Work more)
sheen_buhay
Member
712 Points
540 Posts
Re: Could not show 2nd record
Apr 13, 2012 06:45 AM|LINK
@basheerkal,
it's still not working.
SheenBuhay
sajid007
Member
349 Points
111 Posts
Re: Could not show 2nd record
Apr 13, 2012 07:02 AM|LINK
hey sheen ,
i think you can change the select query by adding one more conditions so that it is easy for searching from DB.
hope it will helps you
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Could not show 2nd record
Apr 13, 2012 07:14 AM|LINK
Hi,
The code you use above:
while (drQry.Read())
{
lbidno.Text = drQry["IDNumber"].ToString();
lbprog.Text = drQry["ProgCode"].ToString();
}
is always covering the old text with the currect value from datareader. That's why you always get the only one row data. If you want to show all results, you can use another GridView to list all.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
mm10
Contributor
6445 Points
1187 Posts
Re: Could not show 2nd record
Apr 13, 2012 08:29 AM|LINK
Do you have several StudentEnrolmentFile with the same id? How many rows does your query return?