Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello, I'm trying to retreive values from a database using visual studio 2010/c# and access 2010, but the set doesn't seem to be filling, the messagebox testing it doesn't even show up. Here is the code I have.
            MessageBox.Show("Hello");
 
            string cmd = "asd";
            string myconstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Users\Jacob\Desktop\Database11.accdb";
            OleDbConnection con = new OleDbConnection(myconstring);
            OleDbCommand com = new OleDbCommand(cmd, con);
            OleDbDataAdapter adp = new OleDbDataAdapter(com);
                con.Open();                      
            DataSet set = new DataSet();
                adp.Fill(set);MessageBox.Show("Did it work?");
                set = new DataSet();
                MessageBox.Show(Convert.ToString(set.Tables[0].Rows[0][0]));
I don't know why the message box isn't showing up right after, and because neither of the test message boxes are loading, I can't tell for sure if the data was even retrieved. Could anyone help?
Posted 2-Apr-13 7:05am
Comments
richcb at 2-Apr-13 12:12pm
   
Well, "asd" is not a command to retrieve data. Are you using something other than that?
YdoUaskbeta at 2-Apr-13 12:32pm
   
Ah, Welp, I'm dumb. After I ran it with a Select * from table1 (the only table, and it has 2 values. I was able to get the second message box, but not the last one. (Table one has two fields, ID and Field1. ID is 1 or 2 and Field1 is Test and Test num respectively)
richcb at 2-Apr-13 12:34pm
   
Remove the "set = new DataSet();" before you show the last message. It is basically giving "set" a blank value when you do that.
ThePhantomUpvoter at 2-Apr-13 12:50pm
   
Wow what a familiar question. I answered this exact same question with this exact same code just yesterday.
YdoUaskbeta at 2-Apr-13 14:29pm
   
Thank you very much Pheonyx. And Phantom, sorry, I had to re-ask because next to edit it notified me that the question had been deleted.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

set = new DataSet();
                MessageBox.Show(Convert.ToString(set.Tables[0].Rows[0][0]));
That is because you are clearing the dataset by re-initialising it.
 
Why do you do this?!
 
Remove
set = new DataSet();
after you have done the "Did it work?" message box.
  Permalink  
Comments
ThePhantomUpvoter at 2-Apr-13 12:53pm
   
Offset the undeserved 1 vote. This is the correct answer. Also "asd" is not a select command. The messagebox is most likely not showing up due to an uncaught exception though.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 2 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100