简体   繁体   中英

Exactly this is the error: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints

This is the Error being Displayed when the Program is being Run . . .

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

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

This Stored Procedure is used for Displaying data in gridVeiw1 and i think it has Error but where it is and how it will be Resolved i dont know . . .

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

Also This is another Stored Procedure : This is used for sending data in ComboBox1 and is Perfectly Working when Project is being Run

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

I have Changed my Code to the following and Got the Perfect Result

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM