Dennis VW
Dennis VW

Reputation: 3177

Using a dataset to fill listbox

In my Windows Forms Application I'm trying to fill a listbox with values from a dataset, but it just stays empty and doesn't give any exceptions.

This is my code

        private void FormTeams_load(object sender, EventArgs e)
    {

        try
        {

            DBTeams = new DBConnection();
            conString = Properties.Settings.Default.UserConnectionString;
            DBTeams.connection_string = conString;

            DBTeams.Sql = Properties.Settings.Default.SQL_Teams;

            ds = DBTeams.GetConnection;

            MaxRows = ds.Tables[0].Rows.Count;

            lsb_TeamsTeams.DataSource = ds;
            lsb_TeamsTeams.DisplayMember = "team_name";
            lsb_TeamsTeams.ValueMember = "team_ID";

        }
        catch (Exception err)
        {

            MessageBox.Show(err.Message);

        }

    }

Listbox name is "lsb_TeamsTeams", dataset name is "ds". The table contains 3 columns. I want column "team_ID" and "team_name" to be shown in this ListBox.

Okay, just to try out if my Query would display in my first Form, Form1, I tried the following. Here I'm trying to fill a second dataset with my second table in the database and then trying to pass the data to a ListBox.

        private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            secondForm = new FormTeams();

            DBPlayers = new DBConnection();
            conString = Properties.Settings.Default.UserConnectionString;
            DBPlayers.connection_string = conString;

            DBPlayers.Sql = Properties.Settings.Default.SQL_Players;
            ds = DBPlayers.GetConnection;

            DBPlayers.Sql = Properties.Settings.Default.SQL_Teams;
            ds2 = DBPlayer.GetConnection;

            lsb_Teams.DisplayMember = "team_name";
            lsb_Teams.ValueMember = "team_ID";
            lsb_Teams.DataSource = ds2;


            MaxRows = ds.Tables[0].Rows.Count;

            NavigateRecords();

            btn_Save.Enabled = false;
            btn_New.Enabled = true;
            btn_Cancel.Enabled = false;

        }
        catch (Exception err)
        {

            MessageBox.Show(err.Message);

        }
    }

This unfortunately throws this error: Invalid object name 'tbl_Teams'.

I don't quite understand why it would raise this error, I tried searching the problem on Google, but I could only find some comments about refreshing intellisense cache. I tried to find this in Visual Studio 2015 Enterprise but wasn't able to.

Here is the database connection methods I use.

        public System.Data.DataSet GetConnection
    {

        get { return MyDataSet(); }

    }

    private System.Data.DataSet MyDataSet()
    {

        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
        con.Open();
        da_0 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
        System.Data.DataSet dat_set = new System.Data.DataSet();
        da_0.Fill(dat_set, "Table_Data_1");

        con.Close();

        return dat_set;

    }

So while trying to access the database via SQL Server Management Studio, I found out the table I created in Visual Studio, was not present in the database, which explains why I got the error Invalid object name 'table' How can this be? I believe it has something to do with the fact I created the second table through Visual Studio. Somehow it did add it into the Visual Studio server explorer but not into the actual database. When using SQL management studio I was able to create the second table and then my query in C# would work.

But now the next problem has arrived with filling the ListBox.

I cannot display any data in my ListBox, but I can display data in a DataGridView, but I'd like to display it in a ListBox.

My code:

        private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            secondForm = new FormTeams();

            DBPlayers = new DBConnection();
            conString = Properties.Settings.Default.UserConnectionString;
            DBPlayers.connection_string = conString;

            DBPlayers.Sql = Properties.Settings.Default.SQL_Players;
            ds = DBPlayers.GetConnection;

            DBPlayers.Sql = Properties.Settings.Default.SQL_Teams;
            ds2 = DBPlayers.GetConnection;

            //The following lines of code diplay: " System.Data.DataViewManagerList Item TypeDescriptor " in the ListBox.
            lsb_Teams.DisplayMember = "team_name";
            lsb_Teams.ValueMember = "team_ID";
            lsb_Teams.DataSource = ds2.Tables[0];

            //This works, it displays the data from the table.
            dataGridView1.DataSource = ds2.Tables[0];


            MaxRows = ds.Tables[0].Rows.Count;

            NavigateRecords();

            btn_Save.Enabled = false;
            btn_New.Enabled = true;
            btn_Cancel.Enabled = false;

        }
        catch (Exception err)
        {

            MessageBox.Show(err.Message);

        }
    }

I have tried multiple positions of the .DataSource line, but that didn't help. I also tried to put the data from my other dataset ds.tables[0] into the ListBox and then it displays data from the DisplayMember column, but throws an error when starting the application cannot bind to the new value member. Parameter name: value.

Upvotes: 0

Views: 5209

Answers (2)

Abdellah OUMGHAR
Abdellah OUMGHAR

Reputation: 3745

You can use :

lsb_TeamsTeams.DataSource = ds.Tables[0];

and Check if ds.Tables[0] contains data.

you must also change second query DBPlayers.Sql to get data from tbl_Teams table :

DBPlayers.Sql = Properties.Settings.Default.SQL_Players;
ds2 = DBPlayer.GetConnection;

because, you use the same query of Players to fill seconde DataSet

Upvotes: 0

A Sal
A Sal

Reputation: 79

Check to see if your dataset's tables are populated (using code breaks). You need to use a DataAdapter to fill the dataset, and I don't think you are doing that. Also, please post your sql query if this doesn't help you.

Upvotes: 1

Related Questions