none
Linq Database RRS feed

  • Question

  • I need update,Insert and delete query for this database but i don't how to make ?

     

     

    public partial class frmArtikujtdg : Form
      {
        RestDBDataContext database = new RestDBDataContext();
      
        
    
        public frmArtikujtdg()
        {
          InitializeComponent();
        }
    
        private void frmArtikujtdg_Load(object sender, EventArgs e)
        {
          RestDBDataContext database = new RestDBDataContext();
          var artikujt = from Artikujt in database.Artikujts
    
                  select new
                  {
                    Artikujt.ID,
                    Artikujt.Barkodi,
                    Artikujt.Emri,
                    Artikujt.Kategoria,
                    Artikujt.Cmimi,
    
    
                  };
          dataGridView1.DataSource = artikujt;
     
        }
    
        private void btnAdd_Click(object sender, EventArgs e)
        {
          
        }
    
        private void btnUpdate_Click(object sender, EventArgs e)
        {
          
        }
    
        private void btnDelete_Click(object sender, EventArgs e)
        {
    
        }
    
        
      }
    }
    

     


    Agron Sverkaj
    • Moved by Martin_Xie Tuesday, August 16, 2011 1:17 AM Move it to LINQ Forum for better support. (From:Visual C# General)
    Monday, August 15, 2011 10:37 AM

Answers

  • Hi AgronSverkaj;

    You posted the following query in your question. The results of that query will NOT be tracked for any edits or deletes by your DataContext (Linq To SQL) / ObjectContext (Linq to Entity Framework) because the result set of this query is a list of Anonymous type.

    var artikujt = from Artikujt in database.Artikujts
            select new
            {
             Artikujt.ID,
             Artikujt.Barkodi,
             Artikujt.Emri,
             Artikujt.Kategoria,
             Artikujt.Cmimi,
           };
    

    In order for your DataContext / ObjectContext to track changes you need to return full records of the type you are querying. In order to track entities of Artikujts as the above query you need to return entities of Artikujts and the way to do that is as follows:

    var artikujt = from Artikujt in database.Artikujts
            select Artikujt;
    

    which will return all columns of the Artikujts table for each record it returns.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 8:38 PM
    Monday, August 15, 2011 2:40 PM
  • What is your answer to the question "RestDBDataContext Is it Linq to SQL?"

    Is it Linq to SQL

    or

    Is it Linq to Entity Framework

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 8:59 AM
    Monday, August 15, 2011 10:23 PM
  • Hi AgronSverkaj;

    This shows one way to do the CRUD operations using Linq to SQL

    public partial class Form1 : Form
    {
      public Form1( )
      {
        InitializeComponent( );
      }
    
      // The Primary Key of the Record to be modified
      private int ArtikujtID = 0;
      // Used to hold the Cell the user clicked on in the datagrid
      // for the record to be modified
      private DataGridViewCell clickedCell;
    
      private void Form1_Load( object sender, EventArgs e )
      {
        // Load the dataGridView with the data from the database
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          var artikujt = from Artikujt in database.Artikujts
                  select new
                  {
                    Artikujt.ID,
                    Artikujt.Barkodi,
                    Artikujt.Emri,
                    Artikujt.Kategoria,
                    Artikujt.Cmimi
                  };
    
          // Bind data to data grid view
          dataGridView1.DataSource = artikujt ;
        }
      }
    
    
      private void btnAdd_Click( object sender, EventArgs e )
      {
        // Create a DataContext to add a new record
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          Artikujt artikujt = new Artikujt();
          artikujt.Barkodi = "Assign a value";
          artikujt.Emri = "Assign a value";
          artikujt.Kategoria = "Assign a value";
          artikujt.Cmimi = "Assign a value";
          // Assign other values to artikujt as needed
    
          // Insert the new record into the DataContext
          database.Artikujts.InsertOnSubmit( artikujt );
          // Update the Database with the info in the DataContext
          database.SubmitChanges();
        }
      }
    
      private void btnUpdate_Click( object sender, EventArgs e )
      {
        // Checks to see if the user right clicked on a row to modify
        // that record.
        if( ArtikujtID == 0 ) return;
                    
        // Create a DataContext to Update a existing record
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          Contact artikujt = ( from Artikujt in database.Artikujts
                     where Artikujt.ID == ArtikujtID
                     select Artikujt ).First( );
    
          // Make a change to the record As needed
          artikujt.Barkodi = "Assign a value";
          //...
          
          // Update the database
          database.SubmitChanges();
          // Get ready for the next record to modify
          ArtikujtID = 0;
        }
    
      }
    
      private void btnDelete_Click( object sender, EventArgs e )
      {
        // Checks to see if the user right clicked on a row to Delete
        // that record.
        if( ArtikujtID == 0 ) return;
    
        // Create a DataContext to Delete a existing record
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          Contact artikujt = ( from Artikujt in database.Artikujts
                     where Artikujt.ID == ArtikujtID
                     select Artikujt ).First( );
    
          database.Artikujts.DeleteOnSubmit( artikujt );
          database.SubmitChanges( );
          ArtikujtID = 0;
        }
      }
    
      private void dataGridView1_MouseDown( object sender, MouseEventArgs e )
      {
        // If the user right-clicks a row, store the cell with the primary key 
        // for use by CRUD operations.
        if( e.Button == MouseButtons.Right )
        {
          DataGridView.HitTestInfo hit = dataGridView1.HitTest( e.X, e.Y );
          if( hit.Type == DataGridViewHitTestType.Cell || hit.Type == DataGridViewHitTestType.RowHeader )
          {
            // Get the Cell in the row with the Primary Key
            clickedCell = dataGridView1.Rows[ hit.RowIndex ].Cells[ "ID" ];
            // Save the Primary Key Value
            ArtikujtID = (int) clickedCell.Value;
          }
        }
      }
    
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 3:52 PM
    Tuesday, August 16, 2011 5:07 AM
  • Hi AgronSverkaj;

    Not a problem, glad I was able to help.

    When you marked this question as the answer you select one of my post that asked you a question. Can you please mark my last post as the answer to the question so that when others search for a solution they will see the correct solution marked as the answer.

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 3:52 PM
    Tuesday, August 16, 2011 1:21 PM

All replies

  • LINQ itself is a query technology, not updating and deleting.  Your data context type would be responsible for the modifications. 

    If you're using LINQ to SQL then your data context probably derives from System.Data.Linq.DataContext.  It is configured to handle change detection for you.  All you have to do is add/modify the objects that it returns and then call SubmitChanges.  It'll determine what was added, modified and removed and do the appropriate commands.

    If you're using Entity Framework then there is a similar type called System.Data.Objects.ObjectContext.  As with LINQ to SQL it'll track changes automatically for you in most cases.  It does have an AddObject method for adding objects.  To save the changes you'll call SaveChanges.

    I recommend that you use the various articles on MSDN on how to save changes based upon which technology you're using.

    Michael Taylor - 8/15/2011
    http://msmvps.com/blogs/p3net

    Monday, August 15, 2011 1:27 PM
  • Hi AgronSverkaj;

    You posted the following query in your question. The results of that query will NOT be tracked for any edits or deletes by your DataContext (Linq To SQL) / ObjectContext (Linq to Entity Framework) because the result set of this query is a list of Anonymous type.

    var artikujt = from Artikujt in database.Artikujts
            select new
            {
             Artikujt.ID,
             Artikujt.Barkodi,
             Artikujt.Emri,
             Artikujt.Kategoria,
             Artikujt.Cmimi,
           };
    

    In order for your DataContext / ObjectContext to track changes you need to return full records of the type you are querying. In order to track entities of Artikujts as the above query you need to return entities of Artikujts and the way to do that is as follows:

    var artikujt = from Artikujt in database.Artikujts
            select Artikujt;
    

    which will return all columns of the Artikujts table for each record it returns.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 8:38 PM
    Monday, August 15, 2011 2:40 PM
  • to load date i don't have problem  but i ned update inser and delet comand ?

    thanks;


    Agron Sverkaj
    Monday, August 15, 2011 8:41 PM
  • What type is this object, RestDBDataContext ? Is it Linq to SQL or Linq to Entity Framework. Linq to SQL creates a file in Solution Explorer with an extension of dbml and Linq to Entity Framework creates one with edmx extension. Which do you have?

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, August 15, 2011 9:02 PM
  • public partial class frmArtikujtdg : Form
     {
      RestDBDataContext database = new RestDBDataContext();
     
      
    
      public frmArtikujtdg()
      {
       InitializeComponent();
      }
    
      private void frmArtikujtdg_Load(object sender, EventArgs e)
      {
       RestDBDataContext database = new RestDBDataContext();
       var artikujt = from Artikujt in database.Artikujts
    
           select new
           {
            Artikujt.ID,
            Artikujt.Barkodi,
            Artikujt.Emri,
            Artikujt.Kategoria,
            Artikujt.Cmimi,
    
    
           };
       dataGridView1.DataSource = artikujt;
     
      }
    
      private void btnAdd_Click(object sender, EventArgs e)
      {
       I NEED HERE Code
      }
    
      private void btnUpdate_Click(object sender, EventArgs e)
      {
       I NEED HERE Code
    
      }
    
      private void btnDelete_Click(object sender, EventArgs e)
      {
         And NEED HERE Code
    
      }
    
      
     }
    }
    
     
    
    
    --------------------------------------------------------------------------------
    

    RestDBDataContext   Is it Linq to SQL
    Agron Sverkaj
    Monday, August 15, 2011 9:33 PM
  • What is your answer to the question "RestDBDataContext Is it Linq to SQL?"

    Is it Linq to SQL

    or

    Is it Linq to Entity Framework

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 8:59 AM
    Monday, August 15, 2011 10:23 PM
  • Hi AgronSverkaj;

    This shows one way to do the CRUD operations using Linq to SQL

    public partial class Form1 : Form
    {
      public Form1( )
      {
        InitializeComponent( );
      }
    
      // The Primary Key of the Record to be modified
      private int ArtikujtID = 0;
      // Used to hold the Cell the user clicked on in the datagrid
      // for the record to be modified
      private DataGridViewCell clickedCell;
    
      private void Form1_Load( object sender, EventArgs e )
      {
        // Load the dataGridView with the data from the database
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          var artikujt = from Artikujt in database.Artikujts
                  select new
                  {
                    Artikujt.ID,
                    Artikujt.Barkodi,
                    Artikujt.Emri,
                    Artikujt.Kategoria,
                    Artikujt.Cmimi
                  };
    
          // Bind data to data grid view
          dataGridView1.DataSource = artikujt ;
        }
      }
    
    
      private void btnAdd_Click( object sender, EventArgs e )
      {
        // Create a DataContext to add a new record
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          Artikujt artikujt = new Artikujt();
          artikujt.Barkodi = "Assign a value";
          artikujt.Emri = "Assign a value";
          artikujt.Kategoria = "Assign a value";
          artikujt.Cmimi = "Assign a value";
          // Assign other values to artikujt as needed
    
          // Insert the new record into the DataContext
          database.Artikujts.InsertOnSubmit( artikujt );
          // Update the Database with the info in the DataContext
          database.SubmitChanges();
        }
      }
    
      private void btnUpdate_Click( object sender, EventArgs e )
      {
        // Checks to see if the user right clicked on a row to modify
        // that record.
        if( ArtikujtID == 0 ) return;
                    
        // Create a DataContext to Update a existing record
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          Contact artikujt = ( from Artikujt in database.Artikujts
                     where Artikujt.ID == ArtikujtID
                     select Artikujt ).First( );
    
          // Make a change to the record As needed
          artikujt.Barkodi = "Assign a value";
          //...
          
          // Update the database
          database.SubmitChanges();
          // Get ready for the next record to modify
          ArtikujtID = 0;
        }
    
      }
    
      private void btnDelete_Click( object sender, EventArgs e )
      {
        // Checks to see if the user right clicked on a row to Delete
        // that record.
        if( ArtikujtID == 0 ) return;
    
        // Create a DataContext to Delete a existing record
        using( RestDBDataContext database = new RestDBDataContext( ) )
        {
          Contact artikujt = ( from Artikujt in database.Artikujts
                     where Artikujt.ID == ArtikujtID
                     select Artikujt ).First( );
    
          database.Artikujts.DeleteOnSubmit( artikujt );
          database.SubmitChanges( );
          ArtikujtID = 0;
        }
      }
    
      private void dataGridView1_MouseDown( object sender, MouseEventArgs e )
      {
        // If the user right-clicks a row, store the cell with the primary key 
        // for use by CRUD operations.
        if( e.Button == MouseButtons.Right )
        {
          DataGridView.HitTestInfo hit = dataGridView1.HitTest( e.X, e.Y );
          if( hit.Type == DataGridViewHitTestType.Cell || hit.Type == DataGridViewHitTestType.RowHeader )
          {
            // Get the Cell in the row with the Primary Key
            clickedCell = dataGridView1.Rows[ hit.RowIndex ].Cells[ "ID" ];
            // Save the Primary Key Value
            ArtikujtID = (int) clickedCell.Value;
          }
        }
      }
    
    }
    

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 3:52 PM
    Tuesday, August 16, 2011 5:07 AM
  • thank you very much Mr Fernandoo

    Agron Sverkaj
    Tuesday, August 16, 2011 9:00 AM
  • Hi AgronSverkaj;

    Not a problem, glad I was able to help.

    When you marked this question as the answer you select one of my post that asked you a question. Can you please mark my last post as the answer to the question so that when others search for a solution they will see the correct solution marked as the answer.

    Thank you.


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by AgronSverkaj Tuesday, August 16, 2011 3:52 PM
    Tuesday, August 16, 2011 1:21 PM
  • Sory I am a beginner in forums I don't know for this

    thank you


    Agron Sverkaj
    Tuesday, August 16, 2011 3:54 PM
  • Not a problem, thanks for the correction.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, August 16, 2011 4:26 PM