Click here to Skip to main content
15,112,252 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Hi
I am trying to add individual records from a sql server database into my wpf datagrid. The Problem i am having is that each time i want to add a new item to the datagrid, the datagrid deletes the previous item and only the new one is displayed.

This is the code for my button that adds the item to the datagrid:
C#
private void button1_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = new DataTable();
            dt = bl.CashSale(txtSKUCode.Text);
            dgvCashSale.ItemsSource = dt.DefaultView;

       
            
        }

This is my xaml code for the datagrid:
HTML
<<DataGrid AutoGenerateColumns="True" Height="129" HorizontalAlignment="Left" Margin="181,538,0,0" Name="dgvCashSale" VerticalAlignment="Top" Width="641" />>

This is my code for the CashSale Method in my buisness layer:
C#
public DataTable CashSale(string skuCode)
        {
            using (SqlConnection dbConn = new SqlConnection(sConnection))
            {
                SqlCommand dbCmd = dbConn.CreateCommand();
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.CommandText = "sp_GetStockItemDetails1";

                dbCmd.Parameters.Add(new SqlParameter("@skuCode", SqlDbType.NVarChar, 50));
                dbCmd.Parameters["@skuCode"].Value = skuCode;

                DataTable dt = new DataTable();

                try
                {
                    dbConn.Open();
                    SqlDataAdapter dbAdapter = new SqlDataAdapter(dbCmd);
                    dbAdapter.Fill(dt);
                }
                catch (SqlException)
                {
                    throw new ApplicationException("An error occured while connecting to database");
                }
                return dt;
            }
        }

And Finally this is my stored procedure to retreive the specific skuCode:
SQL
USE [RET-MAN]
GO
/****** Object:  StoredProcedure [dbo].[sp_GetStockItemDetails1]    Script Date: 08/02/2012 13:35:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[sp_GetStockItemDetails1]
@skuCode nvarchar(50)
AS
BEGIN

	SELECT skuCode,itemName,itemDesc,size,price FROM stock WHERE stock.skuCode=@skuCode

END

Faithfully
Ethan Adams
Programming Student
Posted
Updated 2-Aug-12 2:46am
v2
Comments
   
Can you stop your application and then run it again and now can you see that all the deleted records(means that you are not able to see at the time of inserting new item) are showing to your datagrid?.Please let me know.
   
Where is the code for inserting a record in DataGrid ?

1 solution

Take a good look at your button click handler code.

You're not adding an item. You're creating a new table, adding a single item to it and then binding the database to that table.

This DOES NOT ADD anything to the datagrid as the datagrid will only show you whatever it is you bind it to. It that's a table containing a single record, that's all you're doing to see.

What do you think the solution to this is going to be?? Instead of replacing the databatable, you have to add a new record to any EXISTING table.

You seriously need to pick up a beginners book on C# and work through it. From your code, it's obvious you're missing some basic knowledge and concepts of programming and C#.
   
v2
Comments
AdamsEthan 6-Aug-12 4:49am
   
Please pick up a English Grammar text book, because I can't understand a word that you are saying!!!
Dave Kreskowiak 6-Aug-12 7:56am
   
An English Grammer text book?? I've told you exactly where to look for the problem. You're on your own now, smart ass.

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