I want to get the data from the access database to a text file in c#, i have written the following code but i am not getting any output -
private void button3_click(object sender, eventargs e) { try { connection.open(); oledbcommand command = new oledbcommand(); command.connection = connection; command.commandtext = "select action rvrait"; oledbdatareader reader = command.executereader(); richtextbox1.text = (reader["action"].tostring()); connection.close(); } catch(system.data.oledb.oledbexception) { }
your problem missing reader.read()
call. after creating oledbdatareader cannot start taking content of column in interested. reader not positioned on first record returned query (if any).
private void button3_click(object sender, eventargs e) { connection.open(); oledbcommand command = new oledbcommand(); command.connection = connection; command.commandtext = "select action rvrait"; oledbdatareader reader = command.executereader(); if(reader.read()) richtextbox1.text = (reader["action"].tostring()); connection.close(); }
in way move reader on first record , take content of column if there true returned call read().
pattern has been conceived facilitate reading more 1 record.
example...
list<string> listofactions = new list<string>(); while(reader.read()) listofactions.add(reader["action"].tostring());
note have removed empty try/catch. should avoided if don't plan exception because if leave in place hiding possible errors
Comments
Post a Comment