Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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
Comments
Richard C Bishop 2-Apr-13 12:12pm    
Well, "asd" is not a command to retrieve data. Are you using something other than that?
YdoUaskbeta 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)
Richard C Bishop 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.
[no name] 2-Apr-13 12:50pm    
Wow what a familiar question. I answered this exact same question with this exact same code just yesterday.
YdoUaskbeta 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

C#
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.
 
Share this answer
 
Comments
[no name] 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900