Click here to Skip to main content
12,953,865 members (54,026 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
This is my xaml code:
<window x:class="Medicine.MainWindow" xmlns:x="#unknown">
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"
>
<grid>
<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" />
<combobox height="31" horizontalalignment="Left" margin="176,79,0,0" name="medicinegroup" verticalalignment="Top" width="148">
IsSynchronizedWithCurrentItem="{x:Null}"
SelectionChanged="medicinegroup_SelectionChanged" />

<textblock ishittestvisible="False" margin="200,84,60,315" name="txtSelectgroup">
Foreground="Gray" Text="... Select Group ...">


<combobox height="31" horizontalalignment="Left" margin="176,137,0,0" x:name="medicinename" verticalalignment="Top" width="148">
SelectionChanged="medicinename_SelectionChanged"
IsSynchronizedWithCurrentItem="{x:Null}"
/>
<textblock ishittestvisible="False" margin="0,142,60,253" name="txtSelectname">
Foreground="Gray" Text="... Select Name ..." HorizontalAlignment="Right" Width="101">

<textbox height="34" horizontalalignment="Left" margin="176,198,0,0" name="unitprice" verticalalignment="Top" width="148">
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" />
<textbox height="34" horizontalalignment="Left" margin="176,262,0,0" name="quantity">
TextChanged="quantity_TextChanged" VerticalAlignment="Top" Width="148" />
<textbox height="37" horizontalalignment="Left" margin="176,320,0,0" name="totalprice" 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" />
<textbox height="33" margin="176,29,37,0" name="transactionid" verticalalignment="Top" />



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
{
/// <summary>
/// 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 21:08pm
priom22638

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,429
CHill60 3,490
Maciej Los 3,103
ppolymorphe 2,020
Jochen Arndt 1,975


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2017
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