English 中文(简体)
问题与伙伴关系在db中插入。 NET
原标题:Problem with insert in db with ASP.NET

i 存在问题。

如下文所示,我有两张表格,分别来自Dzieckos和Opikuns。 Dzieckos i有idOpiekun。 如何在下文中添加这种阴道,如在FK中,宇宙使用一号有误。

    protected void btDodaj_Click(object sender, EventArgs e)
{
    DataClassesDataContext db = new DataClassesDataContext();
    Dziecko dz = new Dziecko();
    dz.imie = this.tbImieDz.Text;
    dz.nazwisko = this.tbNazwiskoDz.Text;
    dz.nrGrupy = Convert.ToInt32(this.dropGrupa.SelectedValue);
    Opiekun op = new Opiekun();
    op.imie = this.tbImieRodz.Text;
    op.nazwisko = this.tbNazwiskoRodz.Text;
    op.telefon = Convert.ToInt32(this.tbTel.Text);
    dz.idOpiekun = op.idOpiekun;  **//error line with FK**
    db.Dzieckos.InsertOnSubmit(dz);
    db.Opiekuns.InsertOnSubmit(op);
    db.SubmitChanges();
    Label2.Text = "Dodano " + this.tbImieDz.Text.ToString() + " " + this.tbNazwiskoDz.Text.ToString();  
}
最佳回答

你们应该能够这样做:

dz.Opiekun = op;

并删除这一行文:

db.Opiekuns.InsertOnSubmit(op); 
问题回答

如果你适当配置了处理外国钥匙的 d,这应当做到:

protected void btDodaj_Click(object sender, EventArgs e)
{
    using(DataClassesDataContext db = new DataClassesDataContext())
    {
      Dziecko dz = new Dziecko();
      dz.imie = this.tbImieDz.Text;
      dz.nazwisko = this.tbNazwiskoDz.Text;
      dz.nrGrupy = Convert.ToInt32(this.dropGrupa.SelectedValue);

      Opiekun op = new Opiekun();
      dz.Opiekun.Add(op);   //Linq to Sql will handle it
      op.imie = this.tbImieRodz.Text;
      op.nazwisko = this.tbNazwiskoRodz.Text;
      op.telefon = Convert.ToInt32(this.tbTel.Text);
      db.Dzieckos.InsertOnSubmit(dz);
      db.SubmitChanges();

      Label2.Text = "Dodano " + this.tbImieDz.Text.ToString() + " " + this.tbNazwiskoDz.Text.ToString(); 
    } 
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签