Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing a very basic epos type application where once the user adds a barcode and quantity into texbox and clicks a button, the datagrid shows the information added. At the moment the code works fine until I come to adding a new sale to datagrid. When I click the button, it over-rights the first recorded added to datagrid. How do I get all sales to show when I add them to datagrid?

Code so far:

C#
private void button1_Click(object sender, RoutedEventArgs e)
        {
            MySqlConnection conn = new MySqlConnection(connectionSQL);
            
           conn.Open();

            DataSet ds = new DataSet();

            MySqlDataAdapter da = new MySqlDataAdapter("Select barcode, name, price from Product WHERE barcode='" + textBox1.Text + "'", conn);

            MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);

            da.Fill(ds);
            ds.Tables[0].Columns.Add(new DataColumn("Quantity"));

            for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                ds.Tables[0].Rows[i]["Quantity"] = textBox2.Text;
            }

            this.dataGrid1.ItemsSource = ds.Tables[0].DefaultView;
            conn.Close();
Posted
Updated 9-Apr-13 23:01pm
v3
Comments
CHill60 9-Apr-13 9:26am    
Your data set only ever has one row in it and you're creating a new DataSet each time. Instead of creating a new dataset, add the new row to it
Member 9611735 9-Apr-13 9:30am    
I am unsure how to do that. Can you give me some pointers?
CHill60 9-Apr-13 10:14am    
Will get back to you ...
Member 9611735 9-Apr-13 10:32am    
Thank you!
Richard C Bishop 9-Apr-13 10:20am    
Another thing to take into consideration is the vulnerability of sql injection your code presents. Look into parameterized queries to thwart that danger.

1 solution

You have 2 ways to solve it

1. As state by Chill60, instead of creating a new dataset every time add a row in the dataset (dataset.Rows.Add())

2. In you code you are assigning the datasource for the grid, dont do that. Add rows to the grid and for each row, assign values to each column.

datagrid.Rows.Add()
 
Share this answer
 

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