1

I tried to retrieve data from access database. I wrote the function inside of a class and i got this error. So please anyone know an answer for this question please let me know how can i fix it. the below function is wrote in a class and i need to know how to display data to listview control by using this function and how to call it form load event. i tried to fix this lots of time and i still didn't get a solution for this.

public List<String> displayRoom()
{
    List<String> rooms = new List<String>();

    String query = "select * from room";
    cmd = new OleDbCommand(query, connect);
    reader = cmd.ExecuteReader();
    while(reader.Read()){
        rooms.Add(reader["buyer_name"].ToString());
        rooms.Add(reader["room_type"].ToString());
        rooms.Add(reader["date_from"].ToString());
        rooms.Add(reader["date_to"].ToString());
    }
    return rooms;
}
6
  • What was the error you got? Give us some more information! Commented May 12, 2014 at 7:00
  • Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Windows.Forms.ListView" This is the error i'v got Commented May 12, 2014 at 7:02
  • Can you show us the statement where you assign the list-data to your listview? Commented May 12, 2014 at 7:05
  • @Matthijs did you mean form load event??? Commented May 12, 2014 at 7:08
  • You must have some sort assignmentstatement assigning the data you retrieved to the ListView; My best guess on what you have is this: ListView = displayRoom(); Commented May 12, 2014 at 7:11

2 Answers 2

0

If you can read data properly from database then just in the form load or constructor of the form class write following line:

//here dbClass is an instance of your database class
List<String> rooms = dbClass.displayRoom(); //though `get/load rooms()` would be more appropriate
foreach (string s in rooms)
  {
    ListViewItem lv=new ListViewItem(s);
    listView1.Items.Add(lv); //listView1 is the ListView instance in your form
  }

EDIT

If your Form has name Form, then either in Form1_Load(object sender, EventArgs e) or in public Form1() constructor (after InitializeComponent is called), you can put the above code.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain above code with my full code it's a function in database class may i insert all the data of the class file
I can use the code like above. but my function is placed on a class so i need to know how to call it into form load
If your class name which reads from database is DatabaseClass, in the form load method create an instance DatabaseClass dbClass=new DatabaseClass;, then use above code.
-2

The ExecuteReader requires an open connection:

connect.Open();

Insert just above

reader = cmd.ExecuteReader();

And close the connection when you are done

}
connect.Close();
return rooms;

See MSDN for more info...

3 Comments

If you are telling him about the connection, it should be closed too. Perhaps a Using statement?
But this doesn't answer his question; if you look at the error he provided in the comments. Also you can not open a command, you should open a connection, in this case "connect"
The error was not provided in the original post but added later on....

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.