开发者

An unknown exception is coming before opening the form

An exception is coming in the following code. Can you please tell me whats the wrong in this code? It take time to open the form. An message box in catch block is show before open the form. My database is running but I don`t know why its not running smoothly? Please guide...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace IMS
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    // To set up a connection object
    System.Data.SqlClient.SqlConnection con;
    System.Data.OleDb.OleDbDataAdapter da;
    private void Form2_Load(object sender, EventArgs e)
    {
        con = new System.Data.SqlClient.SqlConnection(); 
        // TODO: This line of code loads data into the 'iMSDataSet1.Part_Group'
        //table. You can move, or remove it, as needed.
        this.part_GroupTableAdapter.Fill(this.iMSDataSet1.Part_Group);


        con.ConnectionString = "Data     Source=.\\DataDirectory\\IMS.sdf;Password=ims;Persist Security Info=True";
        //open up a connection to the database
        try
        {
            con.Open();
        }
        catch
        {
            MessageBox.Show("Database Exception");
        }
        /开发者_JAVA百科/Close up a connection to the database
        con.Close();

    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void AddPartGroup_Click(object sender, EventArgs e)
    {
        System.Data.OleDb.OleDbCommandBuilder cb;
        cb = new System.Data.OleDb.OleDbCommandBuilder(da);

        //DataRow dRow = 
    }
}
}


Well, you need to change how you handle exceptions. This:

catch
{
    MessageBox.Show("Database Exception");
}

is not terribly useful. Not only is it catching all exceptions (instead of just specific ones); it's ignoring the exception itself. At the very least, use something like:

catch(Exception e)
{
    MessageBox.Show("Database Exception: " + e.Message);
    // Now log e.ToString() somewhere as well
}

That will help you work out what's going wrong. As an aside, I would try not to do database operations in the UI thread, and you should usually only catch specific exceptions - but your most immediate change should be to stop ignoring the useful information in the exception which is being thrown.

Note that also, after you've caught the exception you're then continuing as if nothing's wrong - when in fact this sounds like it may well be a fatal error, and the rest of your application is going to fail pretty soon afterwards. You should probably force your application to close at this point (with an appropriate explanation), rather than trying to continue in a broken state. (Other options include retrying the failing operation etc - the important thing is not to just continue as if everything was okay.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜