Share via


Need help with an SqlDataAdapter.fill(dt) error.

Question

Tuesday, October 13, 2009 12:46 AM

Previously i thought that the DataAdapter.Fill line was causing my form.load to complete when there was still a few lines beneath it that needed to be executed, but after asking about it, someone told me i wasnt opening the connection. I looked further into it and found that DataAdapter.Fill opened the connection if it wasnt already open, so that couldnt have been the problem... or could it?

i have played around with different ways to connect and i keep running into the same problem...
i am now trying to connect to an SQL database through this method: http://www.c-sharpcorner.com/forums/ShowMessages.aspx?ThreadID=65670 .

ive noticed (now that the con.Open() line is included) that my form.load is finishing at con.Open().

when i was experimenting with different ways to connect to the database, i used the con.open() line before, and the load continued until the DataAdapter.Fill line where it would still finish early.

im not sure why this is happening... has anyone had this problem before, can anyone help? my program hasnt progressed for days =(

heres my form.load..

private void FindKey_Load(object sender, EventArgs e)



        {







            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\KeyStore.mdf;Integrated Security=True;User Instance=True");







            //Open the Connection  







            con.Open();







            // Query The Database 







            SqlCommand command = new SqlCommand("SELECT * FROM Key", con);







            DataTable dt = new DataTable();







            SqlDataAdapter da = new SqlDataAdapter(command);







            // Fill the Result In DataTable 







            da.Fill(dt);







            //Show it in the list







            for (int i = 0; i < dt.Rows.Count; i++)



            {



                DataRow dRow = dt.Rows[i];



                FoundKey newkey = new FoundKey(dRow);



                lstMatchedKeys.Items.Add(newkey);



            }







                //Close the Connection







                con.Close();







        }

All replies (7)

Tuesday, October 13, 2009 1:30 PM âś…Answered

I'm not aware of any such bug in the current version of VS unless you're running under x64.  Under x64 the exception isn't raised but under x86 it should be.  Exceptions thrown inside either OnLoad or the Load event handler are caught and reported by the debugger correctly for me.

As for the actual exception go to FindKey.cs line 89.  There is an object there that is null.  You need to find out what that object is and fix it.  Unfortunately we can't help you with that because it is inside your code.

Michael Taylor - 10/13/09
http://p3net.mvps.org


Tuesday, October 13, 2009 12:49 AM | 1 vote

Try adding some exception handling around all this code.  Chances are one of the calls you're making is throwing an exception.  If an exception occurs then nothing below the exception point will be called.  The exception will also tell you what is going wrong so you can diagnose it.

Michael Taylor - 10/12/09
http://p3net.mvps.org


Tuesday, October 13, 2009 12:53 AM

I will do that now and get back here in a minute.

Shouldnt the compiler itself stop execution of the code completely if an exception is thrown though?

Rather than ignoring the exception and just finishing the method early?


Tuesday, October 13, 2009 1:04 AM | 1 vote

There's a flaw in VS2008 / NET 3.5, it swallows exceptions that are raised in the Load event if a debugger is attached.  Well, let's call it a bug, it is nasty.  Debug + Exceptions, check the Thrown checkbox on CLR exceptions.
Hans Passant.


Tuesday, October 13, 2009 1:07 AM

With the try/catch loops the code executed right through until the da.Fill(dt) line.

it then gave me the following error with the help of a messagebox.show():

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:line89

the rest of the code after this line did execute, but since the da.Fill(dt) line failed, no data was found.

so rather than stopping the form.load at the da.Fill(dt) line, it looks like its getting an error, but just not telling me about it (unless i force it with the try/catch's)


Tuesday, October 13, 2009 1:57 AM

Bump...

Thanks for your help so far guys!

Still havent been able to get past this problem though, and my project is still at a standstill =(

Any suggestions would be great!


Wednesday, October 14, 2009 1:35 PM

Snayler,

Just checked if you picked up the solution I had for my identical case.

You posted this already under a different thread. http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1350c0ac-7e83-41cc-8df8-11318e3c28ee

No need to open a connection for the adapter.fill method. It does it implicitly. However if you're running 64-bit you have to change the advanced compiler settings from anypc to x86. Then, at least in my case, the code ran properly.