0

The following code produces an error. dbo.getit works fine when I call it directly on the server. The error occurs at cmd.ExecuteReader() . What am I doing wrong?

    string user;
    string pw;
    SqlDataReader dr = null;
    SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=myDB;    Integrated Security=True");

    user = Username.Text.Trim();
    pw = Password.Text.Trim();

    conn.Open();

   try {
        SqlCommand cmd = new SqlCommand("dbo.getit", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@param1", user);
        cmd.Parameters.AddWithValue("@param2", pw);

        dr = cmd.ExecuteReader();

        while ( dr.Read() )
        {
            Session["username"] = user;
            // Session["admin"] = 
            // Session["completed"] =
            Server.Transfer("all_is_well.aspx");

        }
        conn.Close();
        conn.Dispose();

    } catch (Exception ex) {
        if (dr != null)
        {
            dr.Close();
            conn.Close();
        }
        Server.Transfer("ERROR.aspx");
    }

SOLUTION: Replace the two corresponding lines above with these:

SqlCommand cmd = new SqlCommand("select * from dbo.getit(@param1, @param2);", conn);
cmd.CommandType = CommandType.text;
7
  • 3
    The error is due to a bug. Be more specific about the error, and we'll be in a better position to be more specific about the bug... aka, please read tinyurl.com/so-hints and edit your question. Commented Sep 29, 2011 at 16:59
  • usually the Exception ex has a Message and a StackTrace.. please put a breakpoint, read and post this informations to have the possibility to find the reason of the error. Commented Sep 29, 2011 at 17:01
  • I'm unable to use a break point in the environment I'm using. I'll try to print the error to the web page somehow. Commented Sep 29, 2011 at 17:06
  • before Server.Transfer save the exception in Session, then in Error.aspx read the session and propmt the error.. Commented Sep 29, 2011 at 17:12
  • 1
    ahhh, Emanuele, tx. seems obvious once you tell me. I see it can't find the stored procedure. That's odd, since I used the same sql in a VB version of this program I wrote a few weeks back. tx, i think I can figure it out from here. Commented Sep 29, 2011 at 17:24

3 Answers 3

2

This just seems questionable,

Session["username"] = user; 
Server.Transfer("all_is_well.aspx"); 

inside the while loop!

Can you at least finish iterating on the reader, using a temporary object to store the result of the query, and then initialize you session and do the Server.Transfer. .

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't Server.Transfer known to throw a ThreadAbortException
Agree inside the while loop, worse case OP could set a bool indicating userFound = true. Then after the reader finishes, if (userFound) do the server transfer, doing the server transfer inside a loop is pretty ponderous.
i don't think is this the problem: a reader is executed, but the code written there is not executed.. it was just useless sample code to test the program.
Avoid Server.Transfer because of the ThreadAbortException which can cause all kinds of unexpected behavior. Also here
1

Server.Transfer terminates execution of the current page and starts execution of a new page for the current request. Also, Transfer calls End, which throws a ThreadAbortException exception upon completion.

I think what you are trying to do (and I am answering based on what you are trying to do - not necessarily best pratice) is verify that the user is authorized/authenticated in some way based on a data store. You'd be better off not using ExecuteReader at all. Use ExecuteScalar. If the result of ExecuteScalar is not null, the user was found.

if (cmd.ExecuteScalar() != null)
{
   Server.Transfer("all_is_well.aspx");
}

else
{
   Server.Transfer("someErrorPage.aspx");
}

1 Comment

I think that's the right way to do this, given my starting point; however, that's not my error. For some reason it gives an error on the excecutescalar ... I'm experimenting with different sql right now. Part of the problem is I'm switching from using a dataadapter which worked perfectly (at least in vb) to using a datareader. sql = "dbo.getit"; sql = "dbo.getit(@param1, @param2);"; sql = "select * from dbo.getit(@param1, @param2);"; etc.
0

SOLUTION: Replace the two corresponding lines above with these:

SqlCommand cmd = new SqlCommand("select * from dbo.getit(@param1, @param2);", conn);
cmd.CommandType = CommandType.text;

That worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.