|
-
Oct 10th, 2007, 01:21 AM
#1
Thread Starter
Just Married
[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.
-
Oct 10th, 2007, 01:51 AM
#2
Thread Starter
Just Married
Re: [RESOLVED] Datareader to datatable
C# Code:
sqlDataReader = SqlCommand.ExecuteReader();
DataTable dataTable;
dataTable.Load(sqlDataReader);
-
Oct 10th, 2007, 06:41 AM
#3
Re: [RESOLVED] Datareader to datatable
-
Oct 10th, 2007, 07:51 AM
#4
Thread Starter
Just Married
Re: [RESOLVED] Datareader to datatable
 Originally Posted by Hack
Moved from the CodeBank
Thanks HacK for moving this thread.
-
Oct 10th, 2007, 07:58 AM
#5
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.
-
Oct 10th, 2007, 08:11 AM
#6
Thread Starter
Just Married
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:
public DataTable ReturnDataTableRead(string queryString)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = ReturnNewConnection();
SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection);
SqlDataReader sqlDataReader;
try
{
sqlConnection.Open();
sqlDataReader = SqlCommand.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(sqlDataReader);
return dataTable;
}
catch (Exception ex)
{
throw (ex);
}
finally
{
sqlConnection.Close();
}
}
-
Oct 10th, 2007, 02:50 PM
#7
Frenzied Member
Re: [RESOLVED] Datareader to datatable
 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:
public DataTable ReturnDataTableRead(string queryString)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = ReturnNewConnection();
SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection);
SqlDataReader sqlDataReader;
try
{
sqlConnection.Open();
sqlDataReader = SqlCommand.ExecuteReader();
DataTable dataTable = new DataTable();
dataTable.Load(sqlDataReader);
return dataTable;
}
catch (Exception ex)
{
throw (ex);
}
finally
{
sqlConnection.Close();
}
}
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:
public DataTable ReturnDataTableRead(string queryString)
{
DataTable dataTable = null;
using (SqlConnection sqlConnection = ReturnNewConnection())
{
using (SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection))
{
using (SqlDataReader sqlDataReader = SqlCommand.ExecuteReader())
{
dataTable = new DataTable();
dataTable.Load(sqlDataReader);
}
}
}
return dataTable;
}
-
Oct 11th, 2007, 12:17 AM
#8
Thread Starter
Just Married
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:
public SqlDataReader ReturnDataReader(string queryString)
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection = ReturnNewConnection();
SqlCommand SqlCommand = new SqlCommand(queryString,sqlConnection);
SqlDataReader sqlDataReader;
try
{
sqlConnection.Open();
sqlDataReader = SqlCommand.ExecuteReader();
return sqlDataReader;
}
catch (Exception ex)
{
throw (ex);
}
finally
{
//what to do , how use using Here
}
}
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:
public SqlConnection ReturnNewConnection()
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.ConnectionString = _ConnectionString;
return sqlConnection;
}
catch (Exception ex)
{
throw (ex);
}
}
Last edited by shakti5385; Oct 11th, 2007 at 12:32 AM.
-
Oct 11th, 2007, 01:04 AM
#9
Re: [REOPEN] Datareader to datatable
csharp Code:
try
{
using (Thing myThing = new Thing())
{
// ...
}
}
catch
{
}
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.
-
Oct 11th, 2007, 01:28 AM
#10
Thread Starter
Just Married
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:
public DataTable ReturnDataTableRead(string queryString)
{
try
{
DataTable dataTable = null;
using (SqlConnection sqlConnection = ReturnNewConnection())
{
using (SqlCommand SqlCommand = new SqlCommand(queryString, sqlConnection))
{
using (SqlDataReader sqlDataReader = SqlCommand.ExecuteReader())
{
dataTable = new DataTable();
dataTable.Load(sqlDataReader);
}
}
}
return dataTable;
}
catch
{
throw;
}
}
public SqlConnection ReturnNewConnection()
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.ConnectionString = _ConnectionString;
return sqlConnection;
}
catch (Exception ex)
{
throw (ex);
}
}
-
Oct 11th, 2007, 01:42 AM
#11
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.
-
Oct 11th, 2007, 01:49 AM
#12
Thread Starter
Just Married
Re: [REOPEN] Datareader to datatable
is that you want to say?
C# Code:
public SqlConnection ReturnNewConnection()
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.ConnectionString = _ConnectionString;
sqlConnection.Open();
return sqlConnection;
}
catch (Exception ex)
{
throw (ex);
}
finally
{
sqlConnection.Dispose();
}
}
But above code will not work for the data reader because , a datareader required an open connection
Please guide me sir
-
Oct 11th, 2007, 02:00 AM
#13
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.
-
Oct 11th, 2007, 02:04 AM
#14
Thread Starter
Just Married
Re: [REOPEN] Datareader to datatable
So that is the code is it??
C# Code:
public SqlConnection ReturnNewConnection()
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.ConnectionString = _ConnectionString;
sqlConnection.Open();
return sqlConnection;
}
catch (Exception ex)
{
throw (ex);
}
}
-
Oct 11th, 2007, 02:11 AM
#15
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.
-
Oct 11th, 2007, 02:23 AM
#16
Thread Starter
Just Married
Re: [REOPEN] Datareader to datatable
Ok sir check this is it correct?
C# Code:
private SqlConnection ReturnNewConnection()
{
SqlConnection sqlConnection = new SqlConnection();
try
{
sqlConnection.ConnectionString = _ConnectionString;
sqlConnection.Open();
return sqlConnection;
}
catch
{
throw ;
}
}
-
Oct 11th, 2007, 02:26 AM
#17
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.
-
Oct 11th, 2007, 02:36 AM
#18
Thread Starter
Just Married
Re: [REOPEN] Datareader to datatable
Sir I am not getting Please if possible redefine then code.
-
Oct 11th, 2007, 04:26 AM
#19
Re: [REOPEN] Datareader to datatable
Am I speaking another language? Remove the exception handler, i.e. the try...catch block.
-
Oct 11th, 2007, 05:02 AM
#20
Thread Starter
Just Married
Re: [REOPEN] Datareader to datatable
Ok sir
C# Code:
private SqlConnection ReturnNewConnection()
{
SqlConnection sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = _ConnectionString;
sqlConnection.Open();
return sqlConnection;
}
-
Oct 11th, 2007, 05:16 AM
#21
Re: [REOPEN] Datareader to datatable
Perfect. Not that hard, was it?
-
Oct 11th, 2007, 06:16 AM
#22
Thread Starter
Just Married
Re: [REOPEN] Datareader to datatable
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|