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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Linq to Sql

Linq to Sql

Scheduled Pinned Locked Moved Database
databasecsharplinqtutorialquestion
2 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.
  • S Offline
    S Offline
    sindhuan
    wrote on last edited by
    #1

    Hi I wanna Insert a record into the database by fetching the current user and current date.I have to do this by Linq to sql..As I'm new to this can some one tell me how to do this?? user us= new user(); us.Date = DateTime.Now; us.name =""; dc.users.InsertOnSubmit("us"); Is this the right way to do?? us.name should be the login name..how to fetch the user name from db??

    K 1 Reply Last reply
    0
    • S sindhuan

      Hi I wanna Insert a record into the database by fetching the current user and current date.I have to do this by Linq to sql..As I'm new to this can some one tell me how to do this?? user us= new user(); us.Date = DateTime.Now; us.name =""; dc.users.InsertOnSubmit("us"); Is this the right way to do?? us.name should be the login name..how to fetch the user name from db??

      K Offline
      K Offline
      Kevin Marois
      wrote on last edited by
      #2

      sindhuan wrote:

      Is this the right way to do??

      No, because

      dc.users.InsertOnSubmit("us");

      won't even compile, because you're trying to insert a string "ur". Your code is expecting an entity of type user. So you want

      dc.users.InsertOnSubmit(us);

      So, you first want to query the user to get the name, then insert the new row.

      using (MyDataContext dc = new MyDataContext())
      {
      var userName = (from u in dc.tblUsers
      where u.Id = someId
      select u.UserName).FirstOrDefault();

      user us = new user
      {
          Date = DateTime.Now,
          name = userName
      };
      
      try
      {
          dc.users.InsertOnSubmit(us);
      }
      catch(Exception e)
      {
          // Handle exception here
      }
      

      }

      You will have to adjust the data context and table names and column names, but this should get you started.

      If it's not broken, fix it until it is

      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