Click here to Skip to main content
16,013,440 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 




SQL
create table Customer 
(
cust_id numeric(10) null,
cust_name nvarchar(max)  null,
cust_machine2 nvarchar(max) null,
);


C#
<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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        string res;
        //get checked value from CheckBox which is located in Stack Panel…………….
        
        
        
 
        
        
        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)
        {

        }
    }
}








XML
<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>
Posted
Updated 10-Jul-14 7:57am
v3
Comments
Member 10925486 10-Jul-14 12:57pm    
can any buddy tell me how this code work perfectly with primary key id and save checklist box item with sequence ?
right now its shows error at "int rows = cmd.ExecuteNonQuery();" when i am trying to make cust_id as primary key and save only on checklistbox item with error into customer column
Member 10925486 10-Jul-14 12:58pm    
plz kindly fix it or give me some sample code
CHill60 10-Jul-14 13:30pm    
Because you have put your question into the title of the post we can't work out what your problem is. Use the Improve question link to put the question into the body of the post and choose a short, relevant title
Member 10925486 10-Jul-14 13:59pm    
i update my question ........ can you plz now kindly tell me whats wrong in my code ..........!
ZurdoDev 10-Jul-14 15:38pm    
What is the problem?

1 solution

When you make some column as Primary Key, then each value in the column should be unique.

If you are entering one value that is already existing in Table, then it will not allow you to insert that, hence giving exception.
 
Share this answer
 
Comments
Member 10925486 10-Jul-14 21:05pm    
Tadit you is right its actually the problem of primary key :)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900