Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Save in related table ...

Save in related table ...

Scheduled Pinned Locked Moved C#
announcement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nassimnastaran
    wrote on last edited by
    #1

    Hi there ! I have two table (mainTable & SubTable) with these fileds : mainTable --> IdMetal , NameMetal , Formula , ... SubTable --> IDDetails,IdMetal , Company , ... also mainTable.IdMetal=SubTable.IdMetal in myForm , I have some TextBox that filled by MainTable Fields and Datagrid that filled by subTable Fields when I want to create New row(Save) in main table , at the same time i want to fill and save in datagrid , here is my code :

    try
    {
    // for MainTable ...
    OleDbConnection cnnSave=new OleDbConnection(myConnection);
    OleDbCommand cmd__AddRows = new OleDbCommand();
    cmd__AddRows.CommandText =
    @"INSERT INTO MainRegTable (IDMetal,NameMetal,FormulMetal, GroupMetal, CodeMetal,Comments)
    VALUES (@p1,@p2,@p3,@p4,@p5,@p6)";
    cmd__AddRows.Parameters.Clear();
    cmd__AddRows.Parameters.AddWithValue("@p1", txtID.Text);
    ....
    //for subTable ...
    OleDbDataAdapter dagrd = new OleDbDataAdapter(@"Select * From SubTblMetal
    WHERE IDMetal=
    "+int.Parse(txtID.Text),
    cnnSave);
    DataSet dsGrd = new DataSet();
    dagrd.Fill(dsGrd, "SubTblMetal");
    dsGrd.GetChanges();
    dagrd.Update(dsGrd);
    dsGrd.AcceptChanges();

    but , no changes in subtable ... thanks for Reply

    power is knowledge ...

    OriginalGriffO 1 Reply Last reply
    0
    • N nassimnastaran

      Hi there ! I have two table (mainTable & SubTable) with these fileds : mainTable --> IdMetal , NameMetal , Formula , ... SubTable --> IDDetails,IdMetal , Company , ... also mainTable.IdMetal=SubTable.IdMetal in myForm , I have some TextBox that filled by MainTable Fields and Datagrid that filled by subTable Fields when I want to create New row(Save) in main table , at the same time i want to fill and save in datagrid , here is my code :

      try
      {
      // for MainTable ...
      OleDbConnection cnnSave=new OleDbConnection(myConnection);
      OleDbCommand cmd__AddRows = new OleDbCommand();
      cmd__AddRows.CommandText =
      @"INSERT INTO MainRegTable (IDMetal,NameMetal,FormulMetal, GroupMetal, CodeMetal,Comments)
      VALUES (@p1,@p2,@p3,@p4,@p5,@p6)";
      cmd__AddRows.Parameters.Clear();
      cmd__AddRows.Parameters.AddWithValue("@p1", txtID.Text);
      ....
      //for subTable ...
      OleDbDataAdapter dagrd = new OleDbDataAdapter(@"Select * From SubTblMetal
      WHERE IDMetal=
      "+int.Parse(txtID.Text),
      cnnSave);
      DataSet dsGrd = new DataSet();
      dagrd.Fill(dsGrd, "SubTblMetal");
      dsGrd.GetChanges();
      dagrd.Update(dsGrd);
      dsGrd.AcceptChanges();

      but , no changes in subtable ... thanks for Reply

      power is knowledge ...

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Well...you haven't inserted anything into the sub table. So I'm not surprised that it contains no records. If you want to do this, I would recommend that your either use a transaction round both assignments, or better create a stored procedure to create a transaction, and do both inserts - this has the advantage of keeping it all within the DB. But you are going to need two inserts whichever way you go.

      Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      N 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Well...you haven't inserted anything into the sub table. So I'm not surprised that it contains no records. If you want to do this, I would recommend that your either use a transaction round both assignments, or better create a stored procedure to create a transaction, and do both inserts - this has the advantage of keeping it all within the DB. But you are going to need two inserts whichever way you go.

        Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952) Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)

        N Offline
        N Offline
        nassimnastaran
        wrote on last edited by
        #3

        Yes , but I want that the User have an option to Fill datagrid(subTable) or not , so it can be Blank ... thanks

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups