943,608 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1504
  • C# RSS
Apr 16th, 2009
0

SQLDataReader

Expand Post »
Hi

I wrote below coding ,and last Message "Test" never appear in my project ,it means while loop never works.Plase tell me what is problem in my coding


 public partial class Form1 : Form
    {
        public static int Number_Records;
        public static DataSet ds;
        public static DataTable dt;
        public static int Next_Number;
        public Form1()
        {
            InitializeComponent();

    
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            SqlConnection con = new SqlConnection("Server=IT-Test;Database=NTY;user id=sa;password=12345");
            try
            {
                con.Open();
                string str = "select ExecutiveCode from dbo.RDExecutive order by ExecutiveCode";
                SqlCommand cmd = new SqlCommand(str, con);
                SqlDataReader CurrentOutLet1 = cmd.ExecuteReader();

                int i = 0;
                while (CurrentOutLet1.Read())
                {

                  Number_Records= i++;
                }
             
                for (int y1 = 0; y1 <  CurrentOutLet1.FieldCount; y1++)
                {
                  //  MessageBox.Show(CurrentOutLet1.FieldCount.ToString());
                    //MessageBox.Show(CurrentOutLet1.GetName(y1).ToString());
                   dt1.Columns.Add(CurrentOutLet1.GetName(y1),CurrentOutLet1.GetFieldType(y1));
                                   
                }

               
                while (CurrentOutLet1.Read())
                {
                    MessageBox.Show("Test");

              
                   }

No "Test" message.

Thanks
Tank50
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
Tank50 is offline Offline
124 posts
since Aug 2008
Apr 16th, 2009
1

Re: SQLDataReader

Simple, the first while loop already read through the entire data reader, so there is nothing left to read by the time you get to the last while loop.

Seems that you are going about it the hard way. Why not just use an SqlDataAdapter and fill the dtl table. That way, it automatically will get all of the columns, and the data in one method call.
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Apr 16th, 2009
0

Re: SQLDataReader

Jerry is well answered.
Last edited by Ramy Mahrous; Apr 16th, 2009 at 11:48 am.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Apr 18th, 2009
0

Re: SQLDataReader

+1
Reputation Points: 10
Solved Threads: 1
Newbie Poster
djloser is offline Offline
2 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Kuskal and Dijkstra
Next Thread in C# Forum Timeline: stringBuilder Remove problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC