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:
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:
<<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:
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:
USE [RET-MAN]
GO
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