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

Posted May 26, 2011, 10:50 PM EDT
Are you passing the TownID or the TownName (from Dropdown) to the database ?
if possible please post the code.
saifullah khanRank #4,887 · 320 reputationPosted May 27, 2011, 5:02 AM EDT
i want to pass the townname to the database from dropdown(but remember the composite key is town id).
Praneeth KumarRank #4,422 · 344 reputationPosted May 27, 2011, 5:11 AM EDT
saifullah khanRank #4,887 · 320 reputationPosted May 27, 2011, 5:32 AM EDT
Posted May 27, 2011, 5:50 AM EDT
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 khanRank #4,887 · 320 reputationPosted May 27, 2011, 7:01 AM EDT
Praneeth KumarRank #4,422 · 344 reputationPosted May 27, 2011, 8:18 AM EDT
saifullah khanRank #4,887 · 320 reputationPosted May 27, 2011, 8:26 AM EDT
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 KumarRank #4,422 · 344 reputationPosted May 27, 2011, 8:59 AM EDT
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();
}
}
Suthish NairRank #92 · 22.8K reputationPosted May 27, 2011, 9:09 AM EDT
saifullah khanRank #4,887 · 320 reputationPosted May 27, 2011, 9:41 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted May 27, 2011, 9:43 AM EDT