My Code Is Not Working If I Assigned Primary Key At Cust_Id With Not Null Values ......If I Run This Code Without Primary Key Its Work And Save All Checklistbox Item In Customer Column But its insert values without sequence
create table Customer
(
cust_id numeric(10) null,
cust_name nvarchar(max) null,
cust_machine2 nvarchar(max) null,
);
<pre>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.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace WpfApplication2
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string res;
private void btngetvalue_Click(object sender, RoutedEventArgs e)
{
res = "";
foreach (UIElement ul in spforcheckbox.Children)
{
if (ul is CheckBox)
{
CheckBox chk = (CheckBox)ul;
if (chk.IsChecked == true)
{
string dbconnection = (@"Data Source=EMIII-PC; Initial Catalog=gymdatabase;Integrated Security=True");
SqlConnection conn = new SqlConnection(dbconnection);
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("INSERT INTO Customer(cust_id,cust_name,cust_machine2) VALUES ( @custid,@custname,@custmach2 )", conn))
{
cmd.Parameters.AddWithValue("@custid", txtcid.Text);
cmd.Parameters.AddWithValue("@custname", txtcname.Text);
cmd.Parameters.AddWithValue("@custmach2", chk.Content);
int rows = cmd.ExecuteNonQuery();
}
}
}
}
}
}
private void txtcid_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void txtcname_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="35"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="CheckBox in W P F" TextAlignment="Center" FontSize="20" Foreground="Red" Padding="3,0,25,35"></TextBlock>
<Label x:Name="textcid" Grid.Column="0" Content="Cust Id" Padding="30,10,10,3" Margin="0,30,0,5" Grid.RowSpan="2"/>
<Label x:Name="textcname" Grid.Row="2" Grid.Column="0" Content="Cust Name" Padding="30,10,10,3"></Label>
<TextBox x:Name="txtcid" Grid.Row="1" Grid.Column="1" Margin="136,0,131,6" Width="150" TextChanged="txtcid_TextChanged"></TextBox>
<StackPanel Grid.Row="2" x:Name="spforcheckbox" Grid.Column="1" Width="150" Height="125" removed="Azure" Margin="134,56,133,34" >
<CheckBox Margin="4" Content="Chest Press" IsChecked="True" />
<CheckBox Margin="4" Content="Leg Press" />
<CheckBox Margin="4" Content="Butterfly" />
<CheckBox Margin="4" Content="Sitted ROwing" />
</StackPanel>
<Button Grid.Row="3" Grid.Column="0" Content="Submit" x:Name="btngetvalue" Margin="5,5,5,5" Click="btngetvalue_Click" ></Button>
<Label Grid.Row="3" Grid.Column="1" x:Name="lblmessage" Margin="5,5,5,5" ></Label>
<TextBox x:Name="txtcname" Grid.Row="2" Grid.Column="1" Margin="134,7,133,177" Width="150" TextChanged="txtcname_TextChanged"/>
</Grid>
</Window>