Skip to content

Using Primary Key to update another table

Andy Dominguez

Asked Feb 16, 2011, 5:56 AM EST4 answers
Share
LinkedInX
Hi
How do I declare a variable with a value from a database. What I want to do is when the user logs in that I get the primary key that is associated with the user name. Once I have the id what I want to do is when they click on the row in a database that it adds a records using that primary key.

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    Using Sessions.

    Search for articles here.
    0
  • 0
    Hi Suthish
    Thanks for replying. I have been looking on the session thing but I am not understanding how to set this up. Do I have to have a textbox on the form to get the value that I want? The other problem I am having is that i have been reading that this only works for text and nothing else is that true?
    0
  • Suthish Nair
    0
    If this a web application, do a search for Session here (http://www.c-sharpcorner.com)

    You will get articles with examples.
    0
  • 0
    so I found this code. On the datacontract I am not sure how this would work with Linq TO SQL. Would I even need this. When they are putting this on the page load and they put values in there why is that? Would I need to do that when I am pulling the information back from the database?

    With this attribute, you can now use ASP.NET Session in your service. Now create a class which is marked as DataContract and its properties marked as DataMember, so this object can be serialized and sent to client:
    [DataContract]
    public class MyObject
    {
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public int Age { get; set; }
    }
     
    In your ASP.NET page's code behind, create such an object and store it in Session. Note you must do so in Page.Load or an earlier event or the Session variable may not be ready when you invoking the web service:
     protected void Page_Load(object sender, EventArgs e)
    {
    HttpContext.Current.Session["MySessionObject"] = new MyObject() { Name = "Lante", Age = 23 };
    }
     
    In the service method, you return this Session object:
    [OperationContract]
    public MyObject DoWork()
    {
    return (MyObject)HttpContext.Current.Session["MySessionObject"];
    }
     
    Now add a service reference in your Silverlight project, and call the web service:
    public Page()
    {
    InitializeComponent();
    ServiceClient client = new ServiceClient();
    client.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted);
    client.DoWorkAsync();
    }
    0