I have write simple form with new/edit/delete function. but I add new item and edit the existing item, that is no problems. when i delete item in the grid, the error message is appear on the screen. Anyone can suggest me this problems. I will appreciate it. Below is my code.
<Window x:Class="FBPOS.frmGroup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Group" Height="768" Width="1024">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="360"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<DataGrid x:Name="grdGroup" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1020" Height="350" CanUserAddRows="False"
AutoGenerateColumns="False" AlternatingRowBackground="Azure" SelectionMode="Single" SelectionChanged="grdGroup_SelectionChanged" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding GrpId}" Width="70" Header="ID"/>
<DataGridTextColumn Binding="{Binding GrpDescription}" Width="200" Header="Description"/>
<DataGridTextColumn Binding="{Binding GrpDescription2}" Width="200" Header="2nd Description"/>
<DataGridTextColumn Binding="{Binding Remarks}" Width="100" Header="Remarks"/>
<DataGridTextColumn Binding="{Binding KitchenPrinter1}" Width="100" Header="Kitchen Printer 1"/>
<DataGridTextColumn Binding="{Binding KitchenPrinter2}" Width="100" Header="Kitchen Printer 2"/>
<DataGridTextColumn Binding="{Binding KitchenPrinter3}" Width="100" Header="Kitchen Printer 3"/>
<DataGridTextColumn Binding="{Binding KitchenPrinter4}" Width="100" Header="Kitchen Printer 4"/>
<DataGridTextColumn Binding="{Binding LastModified}" Width="100" Header="Last Modified"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
<Grid Grid.Row="1">
<Grid Margin="30,20,20,20">
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Label Content="Group ID" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0"/>
<TextBox x:Name="txtGroupId" HorizontalAlignment="Left" Height="35" TextWrapping="Wrap" VerticalAlignment="Center"
Width="200" Grid.Column="1" Grid.Row="0" MaxLength="7"/>
<Label Content="Date" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2" Grid.Row="0"/>
<Label x:Name="lblCurrentDate" Content="CurrentDate" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="3"
Grid.Row="0" Width="200" Height="35"/>
<Label Content="Description" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="1"/>
<TextBox x:Name="txtDescription" HorizontalAlignment="Left" Height="35" TextWrapping="Wrap" VerticalAlignment="Center"
Width="200" Grid.Column="1" Grid.Row="1" MaxLength="30" KeyUp="txtDescription_KeyUp"/>
<Label Content="2nd Description" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2" Grid.Row="1"/>
<TextBox x:Name="txt2ndDescription" HorizontalAlignment="Left" Height="35" TextWrapping="Wrap" VerticalAlignment="Center"
Width="200" Grid.Column="3" Grid.Row="1" MaxLength="30"/>
<Label Content="Remarks" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="2"/>
<TextBox x:Name="txtRemarks" HorizontalAlignment="Left" Height="35" TextWrapping="Wrap" VerticalAlignment="Center"
Width="200" Grid.Column="1" Grid.Row="2" MaxLength="50"/>
<Label Content="Printer 1" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="3"/>
<ComboBox x:Name="cboPrinter1" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120"
Grid.Column="1" Grid.Row="3" MinWidth="200" MinHeight="35" ShouldPreserveUserEnteredPrefix="True" IsEditable="True" />
<Label Content="Printer 2" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2" Grid.Row="3" />
<ComboBox x:Name="cboPrinter2" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120"
Grid.Column="3" Grid.Row="3" MinWidth="200" MinHeight="35" IsEditable="True"/>
<Label Content="Printer 3" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="0" Grid.Row="4"/>
<ComboBox x:Name="cboPrinter3" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120"
Grid.Column="1" Grid.Row="4" MinWidth="200" Padding="0" Panel.ZIndex="-12" MinHeight="35" IsEditable="True" />
<Label Content="Printer 4" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="2" Grid.Row="5"/>
<ComboBox x:Name="cboPrinter4" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Center" Width="120"
Grid.Column="3" Grid.Row="4" MinWidth="200" MinHeight="35" Padding="0" IsEditable="True"/>
</Grid>
</Grid>
<Grid Grid.Row="3" Margin="30,0,0,0">
<StackPanel Orientation="Horizontal" Margin="5">
<Button x:Name="btnNew" Content="New" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="45"
Click="btnNew_Click"/>
<Button x:Name="btnSave" Content="Save" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="45"
Margin="10,0,0,0" Click="btnSave_Click"/>
<Button x:Name="btnDelete" Content="Delete" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Height="45"
Margin="10,0,0,0" Click="btnDelete_Click"/>
</StackPanel>
</Grid>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Printing;
using System.Data.SqlClient;
using System.Data;
namespace FBPOS
{
/// <summary>
/// Interaction logic for frmGroup.xaml
/// </summary>
public partial class frmGroup : Window
{
bool status,errmessage;
SQLConnectionString sqlconn;
public frmGroup()
{
InitializeComponent();
status = false;
sqlconn = new SQLConnectionString();
FillDataGrid();
PopulateInstalledPrintersCombo();
}
private void FillDataGrid()
{
try
{
SqlCommand sqlfilldata = new SqlCommand("SELECT [GrpId],[GrpDescription],[GrpDescription2],[Remarks],[LastModified],[KitchenPrinter1],[KitchenPrinter2],[KitchenPrinter3],[KitchenPrinter4]
FROM [dbo].[Groups] Order by GrpId", sqlconn.GetDatabaseConnection());
SqlDataAdapter sqldagroup = new SqlDataAdapter(sqlfilldata);
DataTable dtGroups = new DataTable("Groups");
sqldagroup.Fill(dtGroups);
//grdGroup.ItemsSource = null;
grdGroup.ItemsSource = dtGroups.DefaultView;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnNew_Click(object sender, RoutedEventArgs e)
{
ClearData();
lblCurrentDate.Content = DateTime.Now.ToShortDateString();
status = true;
txtGroupId.IsReadOnly = false;
txtGroupId.Focus();
}
private void ClearData()
{
txtGroupId.Text = string.Empty;
txtDescription.Text = string.Empty;
txt2ndDescription.Text = string.Empty;
txtRemarks.Text = string.Empty;
cboPrinter1.Text = string.Empty;
cboPrinter2.Text = string.Empty;
cboPrinter3.Text = string.Empty;
cboPrinter4.Text = string.Empty;
}
private void PopulateInstalledPrintersCombo()
{
// Add list of installed printers found to the combo box.
// The pkInstalledPrinters string will be used to provide the display string.
cboPrinter1.Items.Clear();
cboPrinter2.Items.Clear();
cboPrinter3.Items.Clear();
cboPrinter4.Items.Clear();
cboPrinter1.Items.Add(string.Empty);
cboPrinter2.Items.Add(string.Empty);
cboPrinter3.Items.Add(string.Empty);
cboPrinter4.Items.Add(string.Empty);
LocalPrintServer printServer = new LocalPrintServer();
PrintQueueCollection printqueueOnLocalServer = printServer.GetPrintQueues();
foreach (PrintQueue printer in printqueueOnLocalServer)
{
cboPrinter1.Items.Add(printer.Name);
cboPrinter2.Items.Add(printer.Name);
cboPrinter3.Items.Add(printer.Name);
cboPrinter4.Items.Add(printer.Name);
}
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
errmessage = false;
if (txtGroupId.Text== string.Empty)
{
Properties.Settings.Default.ErrorMessage = "Group Id field can't be empty.\n";
Properties.Settings.Default.Save();
errmessage = true;
}
if (txtDescription.Text == string.Empty)
{
Properties.Settings.Default.ErrorMessage = Properties.Settings.Default.ErrorMessage.ToString() + "Description field can't be empty.\n";
Properties.Settings.Default.Save();
errmessage = true;
}
if (errmessage == true)
{
MessageBox.Show(Properties.Settings.Default.ErrorMessage.ToString());
Properties.Settings.Default.ErrorMessage = string.Empty;
Properties.Settings.Default.Save();
}
else
{
try
{
if (status == true)
{
SqlCommand cmdcheck = new SqlCommand("sp_ValidateGroupId", sqlconn.GetDatabaseConnection());
cmdcheck.CommandType = CommandType.StoredProcedure;
SqlParameter myParm = cmdcheck.Parameters.Add("@GroupId", SqlDbType.NVarChar, 7);
myParm.Value = txtGroupId.Text;
SqlParameter myReturnParm = cmdcheck.Parameters.Add("@returnvalue",SqlDbType.Int);
myReturnParm.Direction = ParameterDirection.ReturnValue;
object chkexist;
chkexist = cmdcheck.ExecuteScalar();
if (Convert.ToBoolean(myReturnParm.Value) == true)
{
MessageBox.Show("Group Id * " +txtGroupId.Text + " * is already Exist.");
txtGroupId.SelectionStart = 0;
txtGroupId.SelectionLength = txtGroupId.Text.Length;
}
else
{
SqlCommand cmdnew = new SqlCommand("INSERT INTO [dbo].[Groups] ([GrpId],[GrpDescription],[GrpDescription2],[Remarks],[LastModified],[KitchenPrinter1],[KitchenPrinter2],[KitchenPrinter3],[KitchenPrinter4])
VALUES ('" + txtGroupId.Text + "','" + txtDescription.Text + "','" + txt2ndDescription.Text + "','" + txtRemarks.Text + "','" + Convert.ToDateTime(lblCurrentDate.Content.ToString())+"','" + cboPrinter1.Text + "','" + cboPrinter2.Text + "','" + cboPrinter3.Text + "','" + cboPrinter4.Text +"')",sqlconn.GetDatabaseConnection());
cmdnew.CommandTimeout = 15;
cmdnew.ExecuteNonQuery();
MessageBox.Show("Record is successfully saved.");
status = false;
txtGroupId.IsReadOnly = true;
FillDataGrid();
}
cmdcheck.Dispose();
}
else
{
SqlCommand cmdedit = new SqlCommand("UPDATE GROUPS Set GrpDescription='" + txtDescription.Text + "',
GrpDescription2 ='" + txt2ndDescription.Text + "', Remarks = '" + txtRemarks.Text + "', LastModified = '" + txtRemarks.Text + "', KitchenPrinter1 = '" + cboPrinter1.Text + "', KitchenPrinter2
= '" + cboPrinter2.Text + "', KitchenPrinter3 = '" + cboPrinter3.Text + "', KitchenPrinter4= '" + cboPrinter4.Text + "' where GrpId = '" + txtGroupId.Text +"';",sqlconn.GetDatabaseConnection());
cmdedit.ExecuteNonQuery();
cmdedit.Dispose();
FillDataGrid();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
}
}
}
private void txtDescription_KeyUp(object sender, KeyEventArgs e)
{
txt2ndDescription.Text = txtDescription.Text;
}
private void grdGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
txtGroupId.Text = (grdGroup.SelectedItem as DataRowView).Row["GrpId"].ToString();
lblCurrentDate.Content = (grdGroup.SelectedItem as DataRowView).Row["LastModified"].ToString();
txtDescription.Text = (grdGroup.SelectedItem as DataRowView).Row["GrpDescription"].ToString();
txt2ndDescription.Text = (grdGroup.SelectedItem as DataRowView).Row["GrpDescription2"].ToString();
txtRemarks.Text = (grdGroup.SelectedItem as DataRowView).Row["Remarks"].ToString();
cboPrinter1.Text = (grdGroup.SelectedItem as DataRowView).Row["KitchenPrinter1"].ToString();
cboPrinter2.Text = (grdGroup.SelectedItem as DataRowView).Row["KitchenPrinter2"].ToString();
cboPrinter3.Text = (grdGroup.SelectedItem as DataRowView).Row["KitchenPrinter3"].ToString();
cboPrinter4.Text = (grdGroup.SelectedItem as DataRowView).Row["KitchenPrinter4"].ToString();
status = false;
txtGroupId.IsReadOnly = true;
}
private void btnDelete_Click(object sender, RoutedEventArgs e)
{
if (grdGroup.HasItems == true && grdGroup.SelectedIndex != -1)
{
SqlCommand sqldetete;
string delstr = (grdGroup.SelectedItem as DataRowView).Row["GrpId"].ToString();
MessageBoxResult msresult = MessageBox.Show("Are you sure to delete '"+ delstr + "'", "Confirm", MessageBoxButton.YesNo);
if (msresult == MessageBoxResult.Yes)
{
try
{
sqldetete = new SqlCommand("Delete from Groups where GrpID = '" + delstr + "'", sqlconn.GetDatabaseConnection());
sqldetete.ExecuteNonQuery();
FillDataGrid();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
}
}