Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
This is my xaml code:

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="464" Width="383" Loaded="Window_Loaded"
>

<Label Content="Medicine Group" Height="31" HorizontalAlignment="Left" Margin="12,79,0,0" Name="label1" VerticalAlignment="Top" Width="122" />
<Label Content="Medicine Name" Height="35" HorizontalAlignment="Left" Margin="12,137,0,0" Name="label2" VerticalAlignment="Top" Width="122" />
<Label Content="Unit Price" Height="34" HorizontalAlignment="Left" Margin="12,198,0,0" Name="label3" VerticalAlignment="Top" Width="122" />

IsSynchronizedWithCurrentItem="{x:Null}"
SelectionChanged="medicinegroup_SelectionChanged" />
 

Foreground="Gray" Text="... Select Group ...">

 


SelectionChanged="medicinename_SelectionChanged"
IsSynchronizedWithCurrentItem="{x:Null}"
/>

Foreground="Gray" Text="... Select Name ..." HorizontalAlignment="Right" Width="101">



Text="{Binding ElementName=medicinename, Path=SelectedItem.UnitPrice}" />
<Label Content="Quantity" Height="34" HorizontalAlignment="Left" Margin="12,262,0,0" Name="label4" VerticalAlignment="Top" Width="122" />
<Label Content="Total Price" Height="28" HorizontalAlignment="Left" Margin="12,320,0,0" Name="label5" VerticalAlignment="Top" Width="122" />

TextChanged="quantity_TextChanged" VerticalAlignment="Top" Width="148" />

<Button Content="Save" Height="32" HorizontalAlignment="Left" Margin="130,381,0,0" Name="save"
VerticalAlignment="Top" Width="110"
Click="save_Click" BorderBrush="#FFE5D9D9" ForceCursor="True" ClickMode="Release" />
<Label Content="Transaction ID" Height="33" HorizontalAlignment="Left" Margin="12,29,0,0" Name="label6" VerticalAlignment="Top" Width="122" />



 
And this is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.ComponentModel;
using System.Collections;
using System.Data;
using System.Data.Objects;
using System.Data.SqlClient;
 
namespace Medicine
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{

public MainWindow()
{
InitializeComponent();

}
 
private void Window_Loaded(object sender, RoutedEventArgs e)
{
FillMedicineGroup();
transidgeneration();
}
 

private void FillMedicineGroup()
{
SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = "Integrated Security=true;Initial Catalog= Medicenes;Data Source=.\\SQLExpress";
SqlCommand cmd = new SqlCommand();
cmd.Connection = dataConnection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select MedicineGroupID,MedicineGroupName from MedicineGroups";
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
dataConnection.Open();
dAdapter.Fill(objDs);
dataConnection.Close();
medicinegroup.SelectedValuePath = "MedicineGroupID";
medicinegroup.DisplayMemberPath = "MedicineGroupName";
medicinegroup.ItemsSource = objDs.Tables[0].DefaultView;


}
 
private void medicinegroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

if (medicinegroup.SelectedValue.ToString() != "")
{
int MedicineGroupID = Convert.ToInt32(medicinegroup.SelectedValue.ToString());
FillMedicineName(MedicineGroupID);
txtSelectgroup.Visibility = medicinegroup.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;
}

}
 
private void FillMedicineName(int medicinegroupID)
{
SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = "Integrated Security=true;Initial Catalog= Medicenes;Data Source=.\\SQLExpress";
SqlCommand cmd = new SqlCommand();
cmd.Connection = dataConnection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select MedicineGroupID,MedicineName,UnitPrice FROM MedicineNames WHERE MedicineGroupID =@MedicineGroupID";
cmd.Parameters.AddWithValue("@MedicineGroupID", medicinegroupID);
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
dataConnection.Open();
dAdapter.Fill(objDs);
dataConnection.Close();

if (objDs.Tables[0].Rows.Count > 0)
{

medicinename.SelectedValuePath = "MedicineGroupID";
medicinename.DisplayMemberPath = "MedicineName";
medicinename.ItemsSource = objDs.Tables[0].DefaultView;
}

}
 
private void medicinename_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
txtSelectname.Visibility = medicinename.SelectedItem == null ? Visibility.Visible : Visibility.Hidden;
}
 
private void transidgeneration()
{
SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = "Integrated Security=true;Initial Catalog=Medicenes;Data Source=.\\SQLExpress";
dataConnection.Open();
 
SqlCommand cmd = new SqlCommand("SELECT TransactionID FROM MedicineSales ", dataConnection);
SqlDataReader reader = cmd.ExecuteReader();
var b = 0;
 
while (reader.Read())
{
b = Convert.ToInt32(reader["TransactionID"].ToString());
b = b + 1;
transactionid.Text = Convert.ToString(b);
}
 
dataConnection.Close();
}
 
private void save_Click(object sender, RoutedEventArgs e)
{

 
}
 
private void quantity_TextChanged(object sender, TextChangedEventArgs e)
{
var copy = Convert.ToInt32(quantity.Text);
var price = Convert.ToDouble(unitprice.Text);
var a = copy * price;
totalprice.Text = a.ToString();
 
}
}
}
 
And this my SQLSERVER 2008 query;
USE [Medicenes]
GO
/****** Object: Table [dbo].[MedicineGroups] Script Date: 01/16/2013 20:26:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MedicineGroups](
[MedicineGroupID] [bigint] NOT NULL,
[MedicineGroupName] [varchar](50) NOT NULL,
CONSTRAINT [PK_MedicineGroups] PRIMARY KEY CLUSTERED
(
[MedicineGroupID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[MedicineNames] Script Date: 01/16/2013 20:26:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MedicineNames](
[MedicineGroupID] [bigint] NOT NULL,
[MedicineID] [bigint] NOT NULL,
[MedicineName] [varchar](50) NOT NULL,
[UnitPrice] [float] NOT NULL,
CONSTRAINT [PK_MedicineNames] PRIMARY KEY CLUSTERED
(
[MedicineID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[MedicineSales] Script Date: 01/16/2013 20:26:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[MedicineSales](
[TransactionID] [bigint] NOT NULL,
[MedicineID] [bigint] NOT NULL,
[UnitPrice] [float] NOT NULL,
[Quantity] [int] NOT NULL,
[TotalPrice] [float] NOT NULL,
CONSTRAINT [PK_MedicineSales] PRIMARY KEY CLUSTERED
(
[TransactionID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: ForeignKey [FK_MedicineNames_MedicineGroups] Script Date: 01/16/2013 20:26:21 ******/
ALTER TABLE [dbo].[MedicineNames] WITH CHECK ADD CONSTRAINT [FK_MedicineNames_MedicineGroups] FOREIGN KEY([MedicineGroupID])
REFERENCES [dbo].[MedicineGroups] ([MedicineGroupID])
GO
ALTER TABLE [dbo].[MedicineNames] CHECK CONSTRAINT [FK_MedicineNames_MedicineGroups]
GO
/****** Object: ForeignKey [FK_MedicineSales_MedicineNames] Script Date: 01/16/2013 20:26:21 ******/
ALTER TABLE [dbo].[MedicineSales] WITH CHECK ADD CONSTRAINT [FK_MedicineSales_MedicineNames] FOREIGN KEY([MedicineID])
REFERENCES [dbo].[MedicineNames] ([MedicineID])
GO
ALTER TABLE [dbo].[MedicineSales] CHECK CONSTRAINT [FK_MedicineSales_MedicineNames]
GO
 
How can i get the medicine id from medicinename table and insert it into medicinesales when clicking save?
Posted 17-Jan-13 22:08pm
priom22580

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100