ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.ASP.Net Error: The INSERT statement conflicted with the FOREIGN KEY constraint<p>Hey <a class="username" rel="akhter">akhter</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvSubDepartMent&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;SubDeptId&#34; HeaderText=&#34;SubDeptId&#34; /&gt; &lt;asp:BoundField DataField=&#34;SubDeptName&#34; HeaderText=&#34;SubDeptName&#34; /&gt; &lt;asp:BoundField DataField=&#34;DeptId&#34; HeaderText=&#34;DeptId&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; SubDepartName :&lt;br /&gt; &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtsubdept&#34; /&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlDepartMent&#34;&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Insert&#34; runat=&#34;server&#34; OnClick=&#34;Insert&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.BindGrid(); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT DeptId,DeptName FROM TestDepartment&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); ddlDepartMent.DataSource = dt; ddlDepartMent.DataTextField = &#34;DeptName&#34;; ddlDepartMent.DataValueField = &#34;DeptId&#34;; ddlDepartMent.DataBind(); ddlDepartMent.Items.Insert(0, new ListItem(&#34;Select Department&#34;, &#34;0&#34;)); } } } } } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM TestSubDepartMent&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); gvSubDepartMent.DataSource = dt; gvSubDepartMent.DataBind(); } } } } } protected void Insert(object sender, EventArgs e) { string DeptName = txtsubdept.Text; string DeptID = ddlDepartMent.SelectedValue; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;INSERT INTO TestSubDepartMent VALUES(@SubDeptName,@DeptId)&#34;, con)) { con.Open(); cmd.Parameters.AddWithValue(&#34;@SubDeptName&#34;, DeptName); cmd.Parameters.AddWithValue(&#34;@DeptId&#34;, DeptID); cmd.ExecuteNonQuery(); con.Close(); this.BindGrid(); } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Me.BindGrid() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT DeptId,DeptName FROM TestDepartment&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Using dt As DataTable = New DataTable() da.Fill(dt) ddlDepartMent.DataSource = dt ddlDepartMent.DataTextField = &#34;DeptName&#34; ddlDepartMent.DataValueField = &#34;DeptId&#34; ddlDepartMent.DataBind() ddlDepartMent.Items.Insert(0, New ListItem(&#34;Select Department&#34;, &#34;0&#34;)) End Using End Using End Using End Using End If End Sub Private Sub BindGrid() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT * FROM TestSubDepartMent&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Using dt As DataTable = New DataTable() da.Fill(dt) gvSubDepartMent.DataSource = dt gvSubDepartMent.DataBind() End Using End Using End Using End Using End Sub Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs) Dim DeptName As String = txtsubdept.Text Dim DeptID As String = ddlDepartMent.SelectedValue Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;INSERT INTO TestSubDepartMent VALUES(@SubDeptName,@DeptId)&#34;, con) con.Open() cmd.Parameters.AddWithValue(&#34;@SubDeptName&#34;, DeptName) cmd.Parameters.AddWithValue(&#34;@DeptId&#34;, DeptID) cmd.ExecuteNonQuery() con.Close() Me.BindGrid() End Using End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/Z5ByvZi.gif" alt="" width="334" height="235" /></p>https://www.aspforums.net:443/Threads/111351/ASPNet-Error-The-INSERT-statement-conflicted-with-the-FOREIGN-KEY-constraint/https://www.aspforums.net:443/Threads/111351/ASPNet-Error-The-INSERT-statement-conflicted-with-the-FOREIGN-KEY-constraint/Wed, 19 Dec 2018 23:35:39 GMT