|
Are you sure that error is on the reading line? It looks more like a connection problem.
Read will return 0 if the connection is closed normally. If it is closed unexpectedly you can get exceptions trying to call Read.
|
|
|
|
|
I have a small problem with retrieving a DataTable object from an asynchronous call.
My setup:
I have a Winfom app that has a reference to a library.
In the Library I have a class called Run which does:
public static DataTable GetAsyncDataTable(String Query, CommandType sqlCommandType)
{
Abstract.AbstractSqlCommand mySqlCommand = new Concrete.ConcreteSqlCommand();
mySqlCommand.GetAsyncDataTableBegin(Query, sqlCommandType);
queryDuration = mySqlCommand.queryDuration;
return mySqlCommand.MyTable;
}
In the GetAsyncDataTableBegin method the code is
public override IAsyncResult GetAsyncDataTableBegin(String Query, CommandType sqlCommandType)
{
return GetAsyncDataTableBegin(Query, null, sqlCommandType, null);
}
public override IAsyncResult GetAsyncDataTableBegin(String Query, List<SqlParameter> sqlParameters, CommandType sqlCommandType, String ConnectionString)
{
DateTime nu = DateTime.Now;
SqlCommand command = Get(Query, sqlParameters, sqlCommandType, ConnectionString);
return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection);
}
In the GetAsyncDataTableEnd Method the code is
public override void GetAsyncDataTableEnd(IAsyncResult myResult)
{
SqlCommand command = null;
SqlDataReader dr = null;
DateTime nu = DateTime.Now;
DataTable myTable = new DataTable();
try
{
command = (SqlCommand)myResult.AsyncState;
dr = command.EndExecuteReader(myResult);
myTable.Load(dr);
MyTable = myTable;
}
catch (Exception err)
{
String poef = err.ToString();
}
finally
{
if (null != dr)
dr.Close();
command.Connection.Close();
DateTime klaar = DateTime.Now;
queryDuration = (klaar - nu).Duration();
string Time = queryDuration.ToString();
}
}
And now my problem: When I call the Run.GetAsyncDataTable (first code block), my data is retrieved asynchronously in another thread but the lines in Run.GetAsyncDataTable keep on running so
queryDuration is 0:00:00.000 and mySqlCommand.MyTable = null;
I do not want to poll to wait while mySqlCommand.MyTable == null. How can I have the DataTable filled in the Run.GetAsyncDataTable method?
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Um ... do you understand what 'asynchronous' means? This is exactly what is supposed to happen; to retrieve the result from an asynchronous call you should provide a callback delegate, which is executed some time later, and in the meantime, your code continues to run. If you want the result to be available 'immediately' on the next line of code, why not use a synchronous query call?
|
|
|
|
|
Hey BobJanova,
I do not want to run an synchronous call to the database because I have sometimes a report running for over more than 1 hour.
Maybe my Run Class should be omitted and my form should use the Abstract/Concrete classes directly
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
If you have a report running for an hour you definitely don't want to be waiting for it to return! Yes, using the asynchronous database classes directly, and passing a callback to be called when the report is complete, might be a better way forward.
|
|
|
|
|
But in which class do I define the delegate?
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Wherever you're making the database calls (probably in the main form).
|
|
|
|
|
No in the Concrete classes
In Word you can only store 2 bytes. That is why I use Writer.
|
|
|
|
|
Hey Guys,
I am trying to develop a software which will keep track of time and date that someone worked for a week.
So obviously it should be in some sort of DataGrid format with option to select data and time.
I am curious to know whether there's any sample code on this that i can use.
If not, some help on how to use or sample code about DaatGrid and how that could be changed to have columns for data and time.
Thanks in advance.
|
|
|
|
|
You might find this[^] helpful.
|
|
|
|
|
That one is for a website.
I am thinking about something using C# Win Forms. A desktop application runnning
on Windows.
|
|
|
|
|
I think just about every beginning developer thinks about writing a time management application. There are plenty of examples because most of them think theirs is the best thing ever and want to share their exceptional work. There are also a plethora of examples of using DataGrid, you just have to do a simple search.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Mark Nischalke wrote: just about every beginning developer thinks about writing a time management
application
True. But since we're lousy at time management, we never complete one.
Will Rogers never met me.
|
|
|
|
|
How to convert with DateTime.ToString() method given date to yyyy\MM\dd format?
e.g. "2011\06\17"
I would not like to use String.Format(@"{0:D4}\{1:D2}\{2:D2}")
Чесноков
|
|
|
|
|
|
DateTime.ToString(@"yyyy\MM\dd");
|
|
|
|
|
Eraser_stp wrote: DateTime.ToString(@"yyyy\MM\dd");
This code will not work.
If you have this string date = DateTime.Now.ToString(@"yyyy\MM\dd"); again wrong format because you will get this result 2011M6d17
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.cacttus.com
|
|
|
|
|
My bad. Thanks for pointing and correct answer.
|
|
|
|
|
You can try this string dateformat = DateTime.Now.ToString(@"yyyy\\MM\\dd").Replace("\\",@"\");
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.cacttus.com
|
|
|
|
|
The .Replace is bothering me because "\\" is the same as @"\".
Maybe you meant @"\\" instead of "\\".
|
|
|
|
|
I released that isn't necessary to use Replace so just using @"yyyy\\MM\\dd" will give correct format 2011\06\17
Thanks for correcting me
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.cacttus.com
modified on Friday, June 17, 2011 6:07 AM
|
|
|
|
|
yes, thanks, that one works
Чесноков
|
|
|
|
|
No problem.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.cacttus.com
|
|
|
|
|
|
Thanks
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.cacttus.com
|
|
|
|