SQLDataReader

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 94
Reputation: Tank50 is an unknown quantity at this point 
Solved Threads: 1
Tank50's Avatar
Tank50 Tank50 is offline Offline
Junior Poster in Training

SQLDataReader

 
0
  #1
Apr 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

Re: SQLDataReader

 
1
  #2
Apr 16th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: SQLDataReader

 
0
  #3
Apr 16th, 2009
Jerry is well answered.
Last edited by Ramy Mahrous; Apr 16th, 2009 at 11:48 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 2
Reputation: djloser is an unknown quantity at this point 
Solved Threads: 1
djloser djloser is offline Offline
Newbie Poster

Re: SQLDataReader

 
0
  #4
Apr 18th, 2009
+1
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC