Conceptual doubts

[color=Green]Hi all,

First of all, thanks a lot for all your help....My special thanks goes out to "iwilld0it"...Thanks buddy.


I am putting up some of my doubts again, request for assistance....

Q.1. Whenever we create an SQLCommand object after creating SqlConnection object, do we need to write something like
SqlDataReader dr = cmd.ExecuteReader();

I mean, does'nt the query get executed just by creating a cmd(SqlCommand object)? Do we need to create a SqlDataReader object as well?


Q.2. I am trying to store the error information inside the database incase an error occurs. For this I am using Application_OnError event inside global.asax file.
I am using Session["err_info"] = Context.Error.ToString() method to store the error information in the err_info session variable. Then this variable is used to store the error info in the dbase inside the same event handler
inside SqlCommand object.
My problem is that the data never gets stored...!!!

Please correct me if I am wrong...

Regards,

Ruchika Chauhan
India[/color]

Comments

  • 1. You use cmd.ExecuteReader() if you are returning records from a query. You use cmd.ExecuteNonQuery() if the query returns no records like an insert, update, or delete. There is also cmd.ExecuteScalar() which returns the first column of the first row of data returned from a sql query ... this is perfect for those queries that return one-piece of data like ...

    [code]
    SELECT someField FROM sometable WHERE someID = 3
    [/code]

    To answer your question ... the sql query is not executed as soon as the command is created ... it is executed when you call one of the 3 functions mentioned.

    [hr]

    2. In the Application_Error you can use this code:

    [code]
    Dim ex As Exception = Server.GetLastError().InnerException

    LogError(ex)

    Server.ClearError()
    [/code]

    This assumes LogError is a function you have written that writes an exception to the database ...

    In the Application_Error event, the Server.GetLastError() always returns an HttpUnhandledException ... so you must check the InnerException property to get the actual exception ...

    Once you are done logging the exception ... you can:

    a. Let the error show on the page or ...
    b. Clear the error using Server.ClearError
    c. You could also redirect the user to a page w/ a friendly error message

    : [color=Green]Hi all,
    :
    : First of all, thanks a lot for all your help....My special thanks
    : goes out to "iwilld0it"...Thanks buddy.
    :
    :
    : I am putting up some of my doubts again, request for assistance....
    :
    : Q.1. Whenever we create an SQLCommand object after creating
    : SqlConnection object, do we need to write something like
    : SqlDataReader dr = cmd.ExecuteReader();
    :
    : I mean, does'nt the query get executed just by creating a
    : cmd(SqlCommand object)? Do we need to create a SqlDataReader object
    : as well?
    :
    :
    : Q.2. I am trying to store the error information inside the database
    : incase an error occurs. For this I am using Application_OnError
    : event inside global.asax file.
    : I am using Session["err_info"] = Context.Error.ToString() method to
    : store the error information in the err_info session variable. Then
    : this variable is used to store the error info in the dbase inside
    : the same event handler
    : inside SqlCommand object.
    : My problem is that the data never gets stored...!!!
    :
    : Please correct me if I am wrong...
    :
    : Regards,
    :
    : Ruchika Chauhan
    : India[/color]
    :
  • [color=Green]Thankyou so much for all your help...

    I'll really appreciate it if you could come online sometime and we could discuss this Application_Error event and lot many other doubts in detail.
    Trust me, I don't mean to offend you. I have recently completed a course in asp.net and so I am facing several doubts. This website is a good place to share code but again its very time consuming...by the time a reply to my doubt arrives, I am ready with 5 more doubts. So please if its Ok with you then I am leaving my ID here. My yahoo messengerID is ruchika_rc
    Will wait for your positive response...

    Anyhow these are my doubts to your reply...

    I was trying to make a connection inside Application_Error event, also I was using a session variable inside Application_Error event...

    Now my question is...Am I right in doing so?
    I mean you suggested a LogError() method but I used a different approach...So was I correct...?

    Please do correct me if I am wrong...Thanks again for the help.

    Regards,

    Ruchika
    India[/color][size=1][/size]



    : 1. You use cmd.ExecuteReader() if you are returning records from a
    : query. You use cmd.ExecuteNonQuery() if the query returns no records
    : like an insert, update, or delete. There is also cmd.ExecuteScalar()
    : which returns the first column of the first row of data returned
    : from a sql query ... this is perfect for those queries that return
    : one-piece of data like ...
    :
    : [code]:
    : SELECT someField FROM sometable WHERE someID = 3
    : [/code]:
    :
    : To answer your question ... the sql query is not executed as soon as
    : the command is created ... it is executed when you call one of the 3
    : functions mentioned.
    :
    : [hr]
    :
    : 2. In the Application_Error you can use this code:
    :
    : [code]:
    : Dim ex As Exception = Server.GetLastError().InnerException
    :
    : LogError(ex)
    :
    : Server.ClearError()
    : [/code]:
    :
    : This assumes LogError is a function you have written that writes an
    : exception to the database ...
    :
    : In the Application_Error event, the Server.GetLastError() always
    : returns an HttpUnhandledException ... so you must check the
    : InnerException property to get the actual exception ...
    :
    : Once you are done logging the exception ... you can:
    :
    : a. Let the error show on the page or ...
    : b. Clear the error using Server.ClearError
    : c. You could also redirect the user to a page w/ a friendly error
    : message
    :
    : : [color=Green]Hi all,
    : :
    : : First of all, thanks a lot for all your help....My special thanks
    : : goes out to "iwilld0it"...Thanks buddy.
    : :
    : :
    : : I am putting up some of my doubts again, request for assistance....
    : :
    : : Q.1. Whenever we create an SQLCommand object after creating
    : : SqlConnection object, do we need to write something like
    : : SqlDataReader dr = cmd.ExecuteReader();
    : :
    : : I mean, does'nt the query get executed just by creating a
    : : cmd(SqlCommand object)? Do we need to create a SqlDataReader object
    : : as well?
    : :
    : :
    : : Q.2. I am trying to store the error information inside the database
    : : incase an error occurs. For this I am using Application_OnError
    : : event inside global.asax file.
    : : I am using Session["err_info"] = Context.Error.ToString() method to
    : : store the error information in the err_info session variable. Then
    : : this variable is used to store the error info in the dbase inside
    : : the same event handler
    : : inside SqlCommand object.
    : : My problem is that the data never gets stored...!!!
    : :
    : : Please correct me if I am wrong...
    : :
    : : Regards,
    : :
    : : Ruchika Chauhan
    : : India[/color]
    : :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories