المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : SqlDataAdapter.Fill() error.



C# Programming
10-13-2009, 05:14 PM
gday all.

i am getting this error:

Error: System.NullReferenceException: Object reference not set to an instance of an object.
at Keystore.FindKey.FindKey_Load(Object sender, EventArgs e) in C:\Users\Shane\Documents\Visual Studio 2008\Projects\Keystore1\Keystore1\FindKey.cs:line 89

when running this code:

private void FindKey_Load(object sender, EventArgs e)
{
SqlCommand command;
DataTable dt;
try
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\KeyStore.mdf;Integrated Security=True;User Instance=True");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}

//Open the Connection

try
{
con.Open();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}

// Query The Database
try
{
command = new SqlCommand("SELECT * FROM Key", con);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
command = new SqlCommand("SELECT * FROM Key", con);
}

try
{
dt = new DataTable();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
dt = new DataTable();
}

try
{
SqlDataAdapter da = new SqlDataAdapter(command);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}

// Fill the Result In DataTable
try
{
da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}

//Show it in the list
try
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dRow = dt.Rows[i];
FoundKey newkey = new FoundKey(dRow);
lstMatchedKeys.Items.Add(newkey);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
//Close the Connection
try
{
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}

}

sorry for all the try/catch's. been using them to try and pinpoint the error and the error message.

The error is coming from the "da.Fill(dt);" line. The rest runs ok.

Can anyone help me overcome this problem? =)