Results 1 to 22 of 22

Thread: [RESOLVED] [REOPEN] Datareader to datatable

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Resolved [RESOLVED] [REOPEN] Datareader to datatable

    HI all
    I want to change the datareader to datatable.

    I want to make datatable to readonly format.
    Thanks
    Last edited by shakti5385; Oct 11th, 2007 at 12:19 AM.

  2. #2

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] Datareader to datatable

    C# Code:
    1. sqlDataReader = SqlCommand.ExecuteReader();
    2.                              DataTable dataTable;
    3.                 dataTable.Load(sqlDataReader);

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [RESOLVED] Datareader to datatable

    Moved from the CodeBank

  4. #4

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] Datareader to datatable

    Quote Originally Posted by Hack
    Moved from the CodeBank
    Thanks HacK for moving this thread.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Datareader to datatable

    Can I just point out that the code you posted will throw a NullReferenceException. You are calling the Load method of a DataTable you haven't yet created.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] Datareader to datatable

    Sir you are here to guide US,
    I am posting then whole function here please check it out!
    Thanks


    C# Code:
    1. public DataTable ReturnDataTableRead(string queryString)
    2.         {
    3.             SqlConnection sqlConnection = new SqlConnection();
    4.             sqlConnection = ReturnNewConnection();
    5.             SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection);
    6.             SqlDataReader sqlDataReader;
    7.             try
    8.             {
    9.                 sqlConnection.Open();
    10.                 sqlDataReader = SqlCommand.ExecuteReader();
    11.                 DataTable dataTable = new DataTable();
    12.                 dataTable.Load(sqlDataReader);
    13.                 return dataTable;
    14.             }
    15.             catch (Exception ex)
    16.             {
    17.                 throw (ex);
    18.             }
    19.             finally
    20.             {
    21.                
    22.                 sqlConnection.Close();
    23.             }
    24.         }

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [RESOLVED] Datareader to datatable

    Quote Originally Posted by shakti5385
    Sir you are here to guide US,
    I am posting then whole function here please check it out!
    Thanks


    C# Code:
    1. public DataTable ReturnDataTableRead(string queryString)
    2.         {
    3.             SqlConnection sqlConnection = new SqlConnection();
    4.             sqlConnection = ReturnNewConnection();
    5.             SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection);
    6.             SqlDataReader sqlDataReader;
    7.             try
    8.             {
    9.                 sqlConnection.Open();
    10.                 sqlDataReader = SqlCommand.ExecuteReader();
    11.                 DataTable dataTable = new DataTable();
    12.                 dataTable.Load(sqlDataReader);
    13.                 return dataTable;
    14.             }
    15.             catch (Exception ex)
    16.             {
    17.                 throw (ex);
    18.             }
    19.             finally
    20.             {
    21.                
    22.                 sqlConnection.Close();
    23.             }
    24.         }
    Since ReturnNewConnection() returns a SqlConnection object, there is no need to initialize a new SqlConnection object before the function call.

    'using' blocks drastically simplify your code and automatically handle object disposal.

    csharp Code:
    1. public DataTable ReturnDataTableRead(string queryString)
    2. {
    3.     DataTable dataTable = null;
    4.  
    5.     using (SqlConnection sqlConnection = ReturnNewConnection())
    6.     {
    7.         using (SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection))
    8.         {
    9.             using (SqlDataReader sqlDataReader = SqlCommand.ExecuteReader())
    10.             {
    11.                 dataTable = new DataTable();
    12.                 dataTable.Load(sqlDataReader);
    13.             }
    14.         }
    15.     }
    16.     return dataTable;
    17. }

  8. #8

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [RESOLVED] Datareader to datatable

    Thanks for the good advice , but in the case of the data reader what shoud I do?
    because a datareader required an open connection.
    just look the code of the data reader, here connection is not closed yet


    C# Code:
    1. public SqlDataReader ReturnDataReader(string queryString)
    2.         {
    3.             SqlConnection sqlConnection = new SqlConnection();
    4.             sqlConnection = ReturnNewConnection();
    5.             SqlCommand SqlCommand = new SqlCommand(queryString,sqlConnection);
    6.             SqlDataReader sqlDataReader;
    7.             try
    8.             {
    9.                 sqlConnection.Open();
    10.                 sqlDataReader = SqlCommand.ExecuteReader();
    11.                 return sqlDataReader;
    12.             }
    13.             catch (Exception ex)
    14.             {
    15.                 throw (ex);
    16.             }
    17.             finally
    18.             {
    19.                 //what to do , how use using Here
    20.             }
    21.         }

    EDIT
    One more thing in your code where are you opening the connection?

    Because using my fucntion I am returning a connection object but it is not opened


    C# Code:
    1. public SqlConnection ReturnNewConnection()
    2.         {
    3.             SqlConnection sqlConnection = new SqlConnection();
    4.             try
    5.             {
    6.                 sqlConnection.ConnectionString = _ConnectionString;
    7.                 return sqlConnection;
    8.             }
    9.             catch (Exception ex)
    10.             {
    11.                 throw (ex);
    12.             }
    13.         }
    Last edited by shakti5385; Oct 11th, 2007 at 12:32 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    csharp Code:
    1. try
    2. {
    3.     using (Thing myThing = new Thing())
    4.     {
    5.         // ...
    6.     }
    7. }
    8. catch
    9. {
    10. }
    or you can put the exception handler inside the using block. Which is better will depend on the situation, although in many cases it may not matter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    Ok thats fine

    But please look this code
    and tell me that where we are opening the connection?

    is the using block open the connection directly?


    C# Code:
    1. public DataTable ReturnDataTableRead(string queryString)
    2.         {
    3.             try
    4.             {
    5.                 DataTable dataTable = null;
    6.                 using (SqlConnection sqlConnection = ReturnNewConnection())
    7.                 {
    8.                     using (SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection))
    9.                     {
    10.                         using (SqlDataReader sqlDataReader = SqlCommand.ExecuteReader())
    11.                         {
    12.                             dataTable = new DataTable();
    13.                             dataTable.Load(sqlDataReader);
    14.                         }
    15.                     }
    16.                 }
    17.                 return dataTable;
    18.             }
    19.             catch
    20.             {
    21.                 throw;
    22.             }
    23.         }
    24.  
    25.  public SqlConnection ReturnNewConnection()
    26.         {
    27.             SqlConnection sqlConnection = new SqlConnection();
    28.             try
    29.             {
    30.                 sqlConnection.ConnectionString = _ConnectionString;
    31.                  return sqlConnection;
    32.             }
    33.             catch (Exception ex)
    34.             {
    35.                 throw (ex);
    36.             }
    37.         }

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    Well, it's not my code but presumably the ReturnNewConnection method opens the connection before returning it. The connection is disposed at the end of the 'using' block so it gets closed then.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    is that you want to say?
    C# Code:
    1. public SqlConnection ReturnNewConnection()
    2.         {
    3.             SqlConnection sqlConnection = new SqlConnection();
    4.             try
    5.             {
    6.                 sqlConnection.ConnectionString = _ConnectionString;
    7.                 sqlConnection.Open();
    8.                 return sqlConnection;
    9.             }
    10.             catch (Exception ex)
    11.             {
    12.                 throw (ex);
    13.             }
    14.             finally
    15.             {
    16.                 sqlConnection.Dispose();
    17.             }
    18.         }

    But above code will not work for the data reader because , a datareader required an open connection

    Please guide me sir

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    Who said to dispose the connection inside the ReturnNewConnection method? Of course you can't do that because the whole point of that method is to return a usable connection.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    So that is the code is it??

    C# Code:
    1. public SqlConnection ReturnNewConnection()
    2.         {
    3.             SqlConnection sqlConnection = new SqlConnection();
    4.             try
    5.             {
    6.                 sqlConnection.ConnectionString = _ConnectionString;
    7.                 sqlConnection.Open();
    8.                 return sqlConnection;
    9.             }
    10.             catch (Exception ex)
    11.             {
    12.                 throw (ex);
    13.             }
    14.         }

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    1. Why are you catching an exception when all you do is throw it again?
    2. If you want to rethrow a caught exception you just use the 'throw' key word on its own, without specifying the exception. Otherwise you lose the call stack information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    Ok sir check this is it correct?

    C# Code:
    1. private SqlConnection ReturnNewConnection()
    2.     {
    3.         SqlConnection sqlConnection = new SqlConnection();
    4.         try
    5.         {
    6.             sqlConnection.ConnectionString = _ConnectionString;
    7.             sqlConnection.Open();
    8.             return sqlConnection;
    9.         }
    10.         catch
    11.         {
    12.             throw ;
    13.         }
    14.     }

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    Again, why do you have any exception handling at all if you're not going to do anything about it? If you're simply going to rethrow the exception then what is the point of catching the exception at all? Unless you plan to add something meaningful to your 'catch' block you should get rid of the exception handling altogether.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    Sir I am not getting Please if possible redefine then code.

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    Am I speaking another language? Remove the exception handler, i.e. the try...catch block.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    Ok sir

    C# Code:
    1. private SqlConnection ReturnNewConnection()
    2.     {
    3.         SqlConnection sqlConnection = new SqlConnection();
    4.         sqlConnection.ConnectionString = _ConnectionString;
    5.         sqlConnection.Open();
    6.         return sqlConnection;
    7.     }

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [REOPEN] Datareader to datatable

    Perfect. Not that hard, was it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [REOPEN] Datareader to datatable

    Quote Originally Posted by jmcilhinney
    Perfect. Not that hard, was it?
    Yes not hard
    Try try until not get the success

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width