1

1) It loads extremely slow, takes over 20 seconds, but it does load the DataBase. Is that normal?

2) I put the hello and bye response to test if it connected well. It writes hello and bye numerous of times like hellohellohellohello...byebyebyebye... is this normal? I was thinking this has something to do with question 3.

3) When I comment out the catch(exception) I get an error saying InvalidOperationException "timeout expired." I think catch was good for catching those occasional errors, not the same error over and over. I think that means something is wrong?

    protected void Page_Load(object sender, EventArgs e)
    {

        SqlConnection Conn = new SqlConnection("Data Source=aserver;Initial Catalog=KennyCust;Persist Security Info=True;user id=sa;pwd=qwerty01");
        SqlDataReader rdr = null;
        string commandString = "SELECT * FROM MainDB";
        string commandString2 = "SELECT * From DetailDB";

        try
        {
            Conn.Open();
            SqlCommand Cmd = new SqlCommand(commandString, Conn);
            SqlDataAdapter sdp = new SqlDataAdapter(Cmd);
            DataSet ds = new DataSet();
            if (Conn != null)
            {
                Response.Write("Hello");
            }

            ds.Clear();
            sdp.Fill(ds);
            MasterCust.DataSource = ds.Tables[0];
            MasterCust.DataBind();
        }
        catch (Exception)
        {

        }
        finally
        {
            if (rdr != null)
            {
                rdr.Close();
            }
            if (Conn != null)
            {
                Conn.Close();
                if (Conn != null)
                {
                    Response.Write("Bye");
                }
            }
        }
4
  • 1
    Empty catches are considered bad... You obviously won't get any errors if you explicitly ignore them all. Commented Jan 4, 2012 at 22:44
  • how many rows/columns in MainDB? Commented Jan 4, 2012 at 22:47
  • To Austin. That is true, but I think the problem is that something is causing this to loop 20-30 times which is causing the error or lag most likely Commented Jan 5, 2012 at 0:28
  • I have no found the solution to this yet. I found something called tier 3 and that I should not mix data use in page_load as that can cause issues for some reason. I will post later if it works out:) Commented Jan 6, 2012 at 23:51

3 Answers 3

3

This is why it's typically not a good idea to just "swallow" exceptions. There's most likely a serious problem that's causing most of your database requests to time out, and rather than getting a useful error that you can use to figure out what the problem is, you just have users complaining that the system is taking forever to load a page.

Chances are the real problem lies in either your database table structure or your connection, or you simply have so much data that doing "SELECT * FROM ..." is killing your performance.

Regarding the multiple "hello" and "bye" messages, something is obviously causing Page_Load to get called multiple times. This can happen if this code is on a control and you're adding multiple instances of the control to your page. I've also seen it happen in other circumstances, but I can't remember exactly what.

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

2 Comments

I am glad you said that. I posted this somewhere else and they said it wasn't a good question...what are you asking=/
I have a very small DB. Few columns and like 3 rows. MasterCust is a GridView. I did comment out the mastercust codes and it ran without lag. Connected perfectly fine without saying hello 20 times.
0

It sounds like your query is timing out (default timeout is 30 seconds), but you are swallowing the exception with your empty catch block. Check the size of your dataset or do a smaller query (select top 100 * from maindb)

1 Comment

I have a very small DB. I tried the code anyways and same result
0

In the form load, how about you only do the SQL calls inside of:

if(!(Page.IsPostback))
{

  //Do SQL in here

}

So that the SQL doesn't run every time the page refreshes.

1 Comment

Hmm. Well, you could add a timeout arg to the sql connection string such that this("Data Source=aserver;Initial Catalog=KennyCust;Persist Security Info=True;user id=sa;pwd=qwerty01") becomes this ("Data Source=aserver;Initial Catalog=KennyCust;Persist Security Info=True;user id=sa;pwd=qwerty01"; timeout=1500) I think the default is 15 seconds so this is setting it to 1500 seconds. msdn.microsoft.com/en-us/library/…

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.