i have created two database tables.
1.town(town id as pk,town name)
2. phase(phase id,town id, phase name. phase id and town id are composite keys)
then i created a web form consists o textbox named as phase id, dropdown name as townid that takes town id from table 1, and textbox named as phase name. textbx 1 i enter phase id, select town from drop drown and enter the phase name. when i click on submit i give me the follwoing error
Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Phase_Town". The conflict occurred in database "pda_rtc", table "dbo.Town", column 'TownID'.
The statement has been terminated.
how do i solve this problem????
Loading
Suthish NairPosted May 27, 2011, 1:43 PM
saifullah khanPosted May 27, 2011, 1:41 PM
Suthish NairPosted May 27, 2011, 1:09 PM
Praneeth KumarPosted May 27, 2011, 12:59 PM
aspx
aspx.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[2].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("insert into Phase values(4,'Phase','" + TownName.SelectedValue + "')",conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
saifullah khanPosted May 27, 2011, 12:26 PM
DataValueField="TownID" Height="19px" Width="121px" AutoPostBack="True" >
SelectCommand="SELECT * FROM [Town]">
1.aspx.cs
con.Open();
cmd = new SqlCommand("insert into Phase values('"+PhaseID.Text+"','" + PhaseName.Text + "','" + TownName.SelectedValue + "')", con);
cmd.ExecuteNonQuery();
Praneeth KumarPosted May 27, 2011, 12:18 PM
saifullah khanPosted May 27, 2011, 11:01 AM
Posted May 27, 2011, 9:50 AM
So it is better to store town id in the phase instead of storing town name in phase table.
From the dropdownlist, you can get the SelectedItem value which will have the townid .Pass this townid to the phase table to store.
saifullah khanPosted May 27, 2011, 9:32 AM
Praneeth KumarPosted May 27, 2011, 9:11 AM
saifullah khanPosted May 27, 2011, 9:02 AM
i want to pass the townname to the database from dropdown(but remember the composite key is town id).
Posted May 27, 2011, 2:50 AM
Are you passing the TownID or the TownName (from Dropdown) to the database ?
if possible please post the code.