Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i m new in this field. I have one problem.....
this is my coding.....
C#
con.Open();
 string str = "select name from tbl where city = 'xxx' ";
 cmd = new SqlCommand(str, con);
 dr = cmd.ExecuteReader();
 while (dr.Read())
 {
     Label lb = new Label();
     lb.Text = dr[0].ToString() + "<br>";
     form1.Controls.Add(lb);


     //-------- part 1 ------------------------------

     string val = "select * from othertbl where name= '" + dr[0].ToString(); +"'";
     cmd = new SqlCommand(val, con);
     dr = cmd.ExecuteReader();
     dr.Read();
     for(int a=0; a<10; a++)    //here 10 clm in database so i can take for loop
     {
     LinkButton lbn = new LinkButton();
     lbn.Text = dr[a].ToString() + "<br>";
     form1.Controls.Add(lbn);
     }
     //-----------------------------------------

 }
 con.Close();

when i run page without "part 1" whole portion... i get output like....
name1
name2
name3

but when i run this page with "part 1" that time i get output like.....
name1
lnk1
lnk2
lnk3

but i want to display like...
name1
lnk1
lnk2
lnk3

name2
lnk1
lnk2

name3
lnk1
lnk2
lnk3

here name and link is not fix which r dynamically changed according to data.
that type of coding possible for display data....? or any other idea like this...
plz help me... frnds

[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 3-Sep-12 8:32am
v2

You are making a terrible mistake using the same datareader.
by doing dr = cmd.executereader you are overwriting the one used in the loop.
You need two readers instances that operate independant from eachother.
 
Share this answer
 
Comments
[no name] 3-Sep-12 14:39pm    
+5 correct ans
member60 3-Sep-12 23:17pm    
my 5!
You have reused the dr variable, thus you loose the outer DataReader on the first run of the loop. Change tho this:

C#
con.Open();
string str = "select name from tbl where city = 'xxx' ";
cmd = new SqlCommand(str, con);
dr1 = cmd.ExecuteReader();
while (dr1.Read())
{
    Label lb = new Label();
    lb.Text = dr[0].ToString() + "<br>";
    form1.Controls.Add(lb);


    //-------- part 1 ------------------------------

    string val = "select * from othertbl where name= '" + dr[0].ToString(); +"'";
    cmd = new SqlCommand(val, con);
    var dr2 = cmd.ExecuteReader();
    dr2.Read();
    for(int a=0; a<10; a++)    //here 10 clm in database so i can take for loop
    {
    LinkButton lbn = new LinkButton();
    lbn.Text = dr2[a].ToString() + "<br>";
    form1.Controls.Add(lbn);
    }
    //-----------------------------------------

}
con.Close();
 
Share this answer
 
v2
Comments
jkpatelce 3-Sep-12 14:45pm    
thnxx....... bro
i got it... thnk u so much
Zoltán Zörgő 3-Sep-12 16:20pm    
Great! Than accept the solution, please.
member60 3-Sep-12 23:17pm    
my 5!
Hi,

There are couple of mistakes in your code,

1) Your select statement for executing query is inside While loop. it's too bad practice. Instead you can use Join to generate your data with single query.
2) You should use StoredProcedure instead of hardcode query.
3) Why you have define 10 count in For loop ? Instead you can get that information from the database(from row count).
4) Closing connection should be in finally block or Connection should be enclosed with using.

Best luck
Thanks
-Amit Gajjar
 
Share this answer
 
Comments
jkpatelce 3-Sep-12 23:00pm    
thnx.... amit
i am new in C#. so i have no full knlg about the all.
so now i can try to solve the problem according ur cmt.
if u have any best site or article for me... so give me a link.. plz
AmitGajjar 4-Sep-12 0:37am    
OfCourse for beginner this all things are very new. but you need to search in MSDN for all this stuff.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900