使用保存主键的表插入checklistbox值的问题 [英] Insert problem of checklistbox value with table that hold primary key

查看:61
本文介绍了使用保存主键的表插入checklistbox值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
{
    /// <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)
        {

        }
    }
}

























<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>

推荐答案

当您将某列作为主键时,列中的每个值都应该是唯一的。



如果输入一个已存在于表中的值,那么它将不允许您插入该值,因此给出了异常。
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.


这篇关于使用保存主键的表插入checklistbox值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆