Click here to Skip to main content
13,451,090 members (55,668 online)
Rate this:
Please Sign up or sign in to vote.
See more:
List<string> list=new List<string>();// declered the list

// fetch data from the database...
            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++)

                txt_AC_Name.Text = list[0].ToString();
                txt_AC.Text= list[1].ToString();

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
Updated 21-Dec-12 20:12pm
Krunal Rohit 22-Dec-12 0:35am
You must alter your query link SELECT * FROM...
Mohamed Mitwalli 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
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.
AshishChaudha 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180318.3 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2018
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