Click here to Skip to main content
Sign Up to vote bad
good
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:32
Edited 21 Dec '12 - 20:12

Comments
Krunal Rohit - 22 Dec '12 - 0:35
You must alter your query link SELECT * FROM...
Mohamed Mitwalli - 22 Dec '12 - 2:21
Enter more record match with your where condition :D and start using parameter because of Sql Injection and using command .

1 solution

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 - 22 Dec '12 - 3:52
my +5!

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 206
1 Richard MacCutchan 145
2 Tadit Dash 140
3 Santhosh G_ 125
4 Volynsky Alex 105
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,957
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,155


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