Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
List<string> list=new List<string>();// declered the list

// fetch data from the database...
 con.Open();
            SqlCommand cmd = new SqlCommand("select AC_NAME,AC_NO from PGM_MAST where AGENT_NO=" + txt_Agnt.Text + "", con);
            SqlDataReader dr; 
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                for ( j=0; j < dr.FieldCount; j++)
                {
                    list.Add(dr[j].ToString());
                    
                }
 
                txt_AC_Name.Text = list[0].ToString();
                txt_AC.Text= list[1].ToString();
                j++;
                 
               
            }
            dr.Close();
            con.Close();  
 
Using it shows only two records. But if I want to retrive more than two records from database then, what may I do? Give me a solution...
Posted 21-Dec-12 18:32pm
Edited 21-Dec-12 20:12pm
v3
Comments
Krunal Rohit at 22-Dec-12 0:35am
   
You must alter your query link SELECT * FROM...
Mohamed Mitwalli at 22-Dec-12 2:21am
   
Enter more record match with your where condition :D and start using parameter because of Sql Injection and using command .

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No, it is returning one row: the one that contains the AGENT_NO that matches the contents of the txt_Agnt text box. It adds two values to your table because you return two columns in your query: AC_NAME and AC_NO which are each added in your for loop.
 
If you want more records, then you need to broaden your search scope by altering your WHERE clause.
 
But don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
  Permalink  
Comments
AshishChaudha at 22-Dec-12 3:52am
   
my +5!

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

  Print Answers RSS
0 OriginalGriff 405
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 284
3 George Jonsson 265
4 King_Fisher 180
0 Sergey Alexandrovich Kryukov 10,899
1 Maciej Los 5,594
2 OriginalGriff 5,544
3 CPallini 3,819
4 BillWoodruff 3,579


Advertise | Privacy | Mobile
Web03 | 2.8.141030.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100