CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2012
    Posts
    14

    [RESOLVED] Form1_Load problems

    hey, im having a slight problem with this, i know form1_load happens before the form is shown, so im trying to use that to my advanage, dunno if its the correct way to go about it but ill explain and hopefully get some tips.

    when my app is loaded, i want it to check to see if the db is online, if false it throws exception, if true, uses next bit of code.
    if true, i want it to see if the computer id is saved in the db, if true, goes to next check if exception, adds time stamp etc to db.
    only thing is,
    when the first exception is thrown and the db does not connect, the code keeps running instead of waiting, how do i do this so that if there is a connection error it stops the code from continuing.
    i have at the moment

    private void Form1_Load(object sender, EventArgs e)
    {
    //Database Connection on Application Run
    string connection = @"Data Source=blah blah blah";
    SqlConnection cn = new SqlConnection(connection);

    //following tests to make sure database connection is active if not shows
    //error on DatabaseError form
    try
    {
    cn.Open();
    }
    catch (Exception)
    {
    new DatabaseError().Show();

    }
    //check db if computer id is added
    //add if false and add start date and end date open form2
    //true, check if registered
    //if false check dates are in time if not close open form 3
    //if true check if activated by purchace, if true add times open form 4
    //if false pop up reminder to activate open form 5
    }

    thanks in advance

    Dean
    Last edited by VirUs1234; October 9th, 2012 at 08:54 AM.

  2. #2
    Join Date
    Sep 2012
    Posts
    14

    Re: Form1_Load problems

    changed it to this

    string connection = @"blah blah";
    SqlConnection cn = new SqlConnection(connection);
    cn.Open();
    //cn.Close();
    if (cn.State.Equals(ConnectionState.Open))
    {
    MessageBox.Show("if");
    }
    else
    {
    MessageBox.Show("else");
    }


    when i have the statement cn.Open(); it shows the if box, but if this is not true, it does nothing.
    when i have the statement cn.Close(); it shows the else box, but if this is not true, it does nothing.

    so how do i get it to throw the else statement from the cn.Open();

    Dean

  3. #3
    Join Date
    Sep 2012
    Posts
    14

    Re: Form1_Load problems

    sorted should really work harder before i post sorry for hassle

    SqlConnection cn = new SqlConnection(connection);
    try
    {
    cn.Open();
    if (cn.State.Equals(System.Data.ConnectionState.Open))
    {
    }

    }
    catch (Exception)
    {
    new DatabaseError().Show();
    }

    sorry again

    Dean

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Form1_Load problems

    Thanks for sharing your answer! Please mark your thread resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured