The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Ads_Members". RRS feed

  • Question

  • User1836733422 posted


    I get the same exact error. Except I get the error when I click on respond to ad.

    I have the following code in BBL/Ads.cs:


    public static bool SendResponse(int adId, string senderName, string senderAddress, string comments)
    			bool sent = false;
    			SiteSettings s = SiteSettings.GetSharedSettings();
    			AdsDataComponent.AdsRow ad = GetAdById(adId);
    			if (ad != null)
    				MembershipUser adUser = Membership.GetUser(ad.MemberName);
    				if (adUser != null)
    					StringBuilder sb = new StringBuilder();
    					sb.AppendFormat("A user has sent a response to your Ad '{0}' at {1}", ad.Title, s.SiteName);
    					sb.AppendLine("The contact information of the user is below:");
    					sb.AppendLine("Name: " + senderName);
    					sb.AppendLine("Email: " + senderAddress);
    					sb.AppendLine("User comments/questions:");
    					sb.AppendLine("You can respond to the user by using the Reply feature of your email client.");
                        string from = String.Format("{0} <{1}>", s.SiteName, s.SiteEmailAddress);
                            MailMessage m = new MailMessage();
                            m.From = new MailAddress(from, s.SiteEmailAddress);
                            m.Subject = "Response for Ad: " + ad.Title;
                            m.Body = sb.ToString();
                            m.ReplyTo = new MailAddress(String.Format("{0} <{1}>", senderName, senderAddress));
                            SmtpClient client = new SmtpClient();
                            client.Credentials = new System.Net.NetworkCredential("info@company.com", "password}");
                            client.EnableSsl = true;
    						sent = true;
    						// Consider customizing the message for the EmailNotSentPanel in the ShowAds page.
    						sent = false;
    			return sent;
    Any idea what I'm doing worng please?
    thanx for your help
    Thursday, September 3, 2009 9:23 PM

All replies

  • User116058376 posted


    I could not find a insert code in the snippet that you have provioded, can you provide the entire code. also how is this finction called?

    Friday, September 4, 2009 12:33 AM
  • User1827200373 posted

    You are trying to insert values in a table , in which you have defined a column as Foreign Key. First of all, Insert the value in the table in which you have defined your primary key and then insert the corresponding records in the foreign key table.

    Friday, September 4, 2009 12:47 AM
  • User1836733422 posted

     Thank you for your responses. That issue is solved. It seemed the application was not in sync with the DB. After I added a user via registration, the error went away.



    Friday, September 4, 2009 12:59 AM