Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Updated 21-Dec-12 20:12pm
v3
Comments
[no name] 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

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.
 
Share this answer
 
Comments
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)



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