Click here to Skip to main content
15,888,315 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have created a ListBox that works as a controler/filter for my DataGridView. In the same form I also have a insert function to create a new company.

Now when a user creates a new company I want to update the listbox to contain the new company created.

C#
namespace MyNameSpace
{
    // Creating our Listbox, Datatable and DataGridView for later use
    public partial class CompanyForm : Form
    {
        private ListBox companyList = new ListBox();
        private DataTable dataEmployeesSource = new DataTable();
        private DataGridView dataEmployees = new DataGridView();

        //Connection String
        string cs = ConfigurationManager.ConnectionStrings["MyCS"].ConnectionString;

        // we initialize our methods but dont load them
        public CompanyForm()
        {
            InitializeComponent();

            initDataEmployees();
            initCompanyList();

            companyList.SelectedIndexChanged += listbox1_SelectedIndexChanged;
        }

        // The first DataEmployees method to fill our DataGridView is executed
        private void initDataEmployees()
        {
            const string sql = "Select fname, ename, c.companyName AS companyName FROM dbo.Users u inner join dbo.Company c on c.companyName = u.company;";
            dataEmployeesSource = selectIntoDataTable(sql);
            dataEmployees.DataSource = dataEmployeesSource;
        }

        // Method to fill our CompanyList which later works as a filter for our DataGridView
        private void initCompanyList()
        {
            const string sql = "Select companyName from dbo.Company";
            try
            {
                DataTable dt = selectIntoDataTable(sql);
                companyList.DataSource = dt;
                companyList.ValueMember = "companyName";
            }
            catch (Exception ex)
            {
                MessageBox.Show("There are no companies to display");
            }
        }
        // Filling our DataTable locally to enable fast filtering
        private DataTable selectIntoDataTable(string sql)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(cs))
            {
                try
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    using (SqlDataAdapter a = new SqlDataAdapter(cmd))
                    {
                        a.Fill(dt);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                finally
                {
                    con.Close();
                }
            }
                return dt;
            }
        // Here we show the changed value based on the selected index from our CompanyList
        private void listbox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRowView selectedRow = (DataRowView)companyList.SelectedItem;
            string selectedText = (string)selectedRow.Row["companyName"];
            DataView dv = dataEmployeesSource.DefaultView;
            string filter = string.Format("companyName = '{0}'", selectedText);
            dv.RowFilter = filter;
            dataEmployees.DataSource = dv;
        }
        // Create company, we create a company here and insert it into our dbo.Company table
        private void createCompany_Click_1(object sender, EventArgs e)
        {
            if (textBoxCompanyName.Text == "")
            {
                MessageBox.Show("Vänligen fyll i informationen");
                return;
            }
            using (SqlConnection con = new SqlConnection(cs))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into dbo.Company (companyName) values(@companyName)", con);
                cmd.Parameters.AddWithValue("@companyName", textBoxCompanyName.Text);
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                MessageBox.Show("Grattis! Du har skapat ett företag");

            }
        }
    }
}


What I have tried:

I have tried to add updates in the end of the createCompany_Click method
Posted
Updated 11-Jan-17 7:07am
Comments
[no name] 11-Jan-17 13:35pm    
Why are you creating a new dataset, filling it with your adapter and then not doing anything with it?

1 solution

You're not calling cmd.ExecuteNonQuery() in your createCompanyClick handler.
 
Share this answer
 
Comments
Member 12943813 11-Jan-17 14:30pm    
It doesn't actually do anything to add it. I need the listbox to be updated once I add a new company which happens in the createCompanyClick event
Maciej Los 12-Jan-17 2:46am    
ExceuteNonQuery is used to update database. If the database isn't going to update, then nothing will happen. Finally, you have to re-bind data on ListBox object.
Maciej Los 12-Jan-17 2:45am    
5ed!

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