这正是错误:无法启用约束。一行或多行包含违反非空,唯一或外键约束的值

时间:2017-01-29 20:33:56

标签: c# sql sql-server

这是程序运行时显示的错误。 。 。

无法启用约束。一行或多行包含违反非null,唯一或外键约束的值。

    private void searchClassSectionSchedule_Load(object sender, EventArgs e)
    {
        comboBox1.DataSource = CSData();
        comboBox1.DisplayMember = "ClassSection";
        comboBox1.ValueMember = "csec_id";
        comboBox1.SelectedIndex = -1;
    }
    DataTable dt = new DataTable();
    //Data of ClassSection is Taken in ComboBox2 from Table "Class_Section" with the help of Stored Procedure (CSEC_View_Data)
    private DataTable CSData()
    {
        string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand cmd = new SqlCommand("CSEC_View_Data", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                SqlDataReader r = cmd.ExecuteReader();
                dt.Load(r);
            }
        }
        return dt;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        button1.Enabled = true;
        while (dataGridView1.RowCount > 1)
        {
            dataGridView1.Rows.RemoveAt(0);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            dt = VeiwClassSectionTime();
            dataGridView1.DataSource = dt;
            button1.Enabled = false;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private DataTable VeiwClassSectionTime()
    {
        string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            using (SqlCommand cmd = new SqlCommand("CSEC_Time_Display", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("csec_id", comboBox1.SelectedValue);
                conn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                dt.Load(reader);
            }
        }
        return dt;
    }
}

此存储过程用于显示gridVeiw1中的数据,我认为它有错误,但它在哪里以及它将如何解决我不知道。 。

ALTER PROCEDURE [dbo].[CSEC_Time_Display]
(
    @csec_id NVARCHAR(50)
)       
AS 
BEGIN
    SELECT         
        ct.[ct_id]
       ,ct.[week_day]
       ,CONVERT(varchar(5), ct.[ct_start],108 ) AS 'ct_start'
       ,CONVERT(varchar(5), ct.[ct_end],108 ) AS 'ct_end'
       ,ct.[sub_code]
       ,ct.[t_id]
       FROM [Attendance].[dbo].[ClassTimmings] ct
       WHERE ct.[csec_id] = @csec_id
 END

这也是另一个存储过程: 这用于在ComboBox1中发送数据,并在项目正在运行时完美工作

ALTER PROCEDURE [dbo].[CSEC_View_Data]
AS 
BEGIN
     SELECT
         s.[csec_id]
        ,c.[c_program]+'-'+c.[c_semester]+'  '+s.[csec_section] AS 'ClassSection'
     FROM [Attendance].[dbo].[Class_Section] s
     INNER JOIN [dbo].[Class] c ON s.[c_id] = c.[c_id]
 END

1 个答案:

答案 0 :(得分:0)

我已将我的代码更改为以下内容并获得了完美的结果

private void searchClassSectionSchedule_Load(object sender, EventArgs e)
{
    comboBox1.DataSource = CSData();
    comboBox1.DisplayMember = "ClassSection";
    comboBox1.ValueMember = "csec_id";
    comboBox1.SelectedIndex = -1;
}
//Data of ClassSection is Taken in ComboBox2 from Table "Class_Section" with the help of Stored Procedure (CSEC_View_Data)
private DataTable CSData()
{
   DataTable dt = new DataTable();
    string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connString))
    {
        using (SqlCommand cmd = new SqlCommand("CSEC_View_Data", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            SqlDataReader r = cmd.ExecuteReader();
            dt.Load(r);
        }
    }
    return dt;
}

private void button2_Click(object sender, EventArgs e)
{
    button1.Enabled = true;
    while (dataGridView1.RowCount > 1)
    {
        dataGridView1.Rows.RemoveAt(0);
    }
}

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        dt = VeiwClassSectionTime();
        dataGridView1.DataSource = dt;
        button1.Enabled = false;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private DataTable VeiwClassSectionTime()
{
  DataTable dt = new DataTable();
    string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
    using (SqlConnection conn = new SqlConnection(connString))
    {
        using (SqlCommand cmd = new SqlCommand("CSEC_Time_Display", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("csec_id", comboBox1.SelectedValue);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            dt.Load(reader);
        }
    }
    return dt;
}

}