none
C# Linq to SQL display data in objects RRS feed

  • Question

  • Hi i have a simple question i was wondering how to display the existing data in C# objects like Textbox, combobox or labels. At the moment a sql database file, one of the tables data is currently displayed in the datagridview control just fine.
    But what i would like to get is each row, for example column1, column2,column3 of row1 to be displayed in textbox1,textbox2,textbox3 respectively. and then when i press the next button i would like to go the next entry which means column1, column2,column3 of row2 still in textbox1,textbox2,textbox3
    This is something similar http://msdn.microsoft.com/en-us/library/aa984467%28VS.71%29.aspx to what i want to do but i am using LINQ so the article above is old and i am using LINQ and i am new to LINQ please help me out, i attached a file of my code as there is no point attaching the whole code here, and i am using the northwind database, the default Microsoft database as my default database, so it is easier for us to communicate...
    thanks in advance
    D.Boy
    Wednesday, August 18, 2010 11:59 AM

Answers

  • var myquery = from myrow in mycontext.Orders select new
     { 
       CID = myrow.CustomerID, EmpId = myrow.EmployeeID, OID = myrow.OrderID 
     };
    //This will retrieve all records from the db
     foreach (var valuesFromQuery in myquery)
      {
        textBox1.Text = valuesFromQuery.CID;
        textBox2.Text = valuesFromQuery.EmpId.ToString();
        textBox3.Text = valuesFromQuery.OID.ToString();
      }
    //This will iterate all elementes inside the query and set the Text inside the textboxes to those values, whoever the final values will be last values 
    

    You need for a way to access the objects in an indexed way, like first, second,... last

    You can do this with the enumerator but you need to control it manually or else use an indexed collection instead such as the alternatives I recommended.

    Regards

    Thursday, August 19, 2010 12:08 PM

All replies

  • There is little difference, I assume you already have the data connected

    //Create a datacontext, if you don't specifie the default connection string will be used

    DataContext mycontext=new DataContext();

    //Next query the database, I assume there are more than 3 columns

    var myquery= from myrow in mycontext.MyDatabase select new {Text1=myrow.Column1,Text2=myrow.Column2,Text3=myrow.Column3};

    The only thing for you to do now is execute the query and put the data in the actual textboxes

    If you add a ToList() statement it will be converted to a List and you only need to iterate the collection and put the data in the control

    I assume you know how to do this because your question was about linq not c#, if more help is needed please say so

    Regards

    Wednesday, August 18, 2010 12:20 PM
  • Tks for the reple Serguey this is the code that i was able to manage to get so far and i have 3 textboxes namely textbox1, textbox2 and textbox3 already  in the desginer, frankly speaking not very sure about the Tlist thing as well, take a look at the code below and advise further tks

    private void button1_Click(object sender, RoutedEventArgs e)
        {
          NorthwindLINQDataContext mycontext = new NorthwindLINQDataContext();
          var myquery = from myrow in mycontext.Orders select new { Text1 = myrow.CustomerID, Text2 = myrow.EmployeeID, Text3 = myrow.OrderID };
                            
        }
    


    D.Boy
    Wednesday, August 18, 2010 12:31 PM
  • Now, let me explain a bit was this do in broad terms, the thing is, unless you execute the query, you won't get the results, also you are using an anonymous type so its members are only accesible by enumeration.

    In your case perhaps it will be wiser to use a tuple http://msdn.microsoft.com/en-us/library/system.tuple.aspx

    or a lightweigh class to store the data retrieved from the database

    If you use ToList() you will get a list and in every element a row

    So accesing the nth row will be like mylist[n] (please remember that this is a 0 based index)

    And the only thing that you need is to increase the index every time the button is pushed, also bear in mind that this query will retrieve all elements in said columns, not the nth element so it will be wise to move this to other method and access the nth element using the list

    If you don't want to retrieve all elements you can use Skip and increase the amount of elements skipped to n-1 each time so you will get the n each time

    Regards

     

    Wednesday, August 18, 2010 1:01 PM
  • Well sorry to be a bit bother, but now i am officially confused. Yes it is true that i am not asking for help with C# language but in truth i am just a beginner and so i was wondering if you could please provide me with a code as i am better at understanding with code, i think u would prefer to say backward engineering, so this is the code that i have been trying and frankly speaking i have never come across the word tuple till now!.

    the code is what i am trying to do, please enhance it for me tks

     

     private
    
    
     void
    
    
     button1_Click(object
    
    
     sender, RoutedEventArgs e)
    {
    NorthwindLINQDataContext mycontext = new NorthwindLINQDataContext();
    var myquery = from myrow in mycontext.Orders select new { Text1 = myrow.CustomerID, Text2 = myrow.EmployeeID, Text3 = myrow.OrderID };
    List<string > queires = Convert.ToString(myquery.ToList());
    }

    EDIT

    This is the updated code and please see the result in the textbox below

     

    private void button1_Click(object sender, RoutedEventArgs e)
      {
       NorthwindLINQDataContext mycontext = new NorthwindLINQDataContext();
       var myquery =Tuple.Create( from myrow in mycontext.Orders select new { Text1 = myrow.CustomerID, Text2 = myrow.EmployeeID, Text3 = myrow.OrderID });
    
       textBox1.Text = myquery.ToString();
    
      }

    This is the result that i see in the textbox1

    (SELECT [t0].[CustomerID] AS [Text1], [t0].[EmployeeID] AS [Text2], [t0].[OrderID] AS [Text3]
    FROM [dbo].[Orders] AS [t0]
    )


    D.Boy
    Thursday, August 19, 2010 4:07 AM
  • Hello Priya,

    You need to just change the code to display values into textboxe's, Try like below

    private void button1_Click(object sender, RoutedEventArgs e)
     {
      NorthwindLINQDataContext mycontext = new NorthwindLINQDataContext();
      var myquery = from myrow in mycontext.Orders select new { CID = myrow.CustomerID, EmpId = myrow.EmployeeID, OID = myrow.OrderID };
    
      foreach(var valuesFromQuery in myquery)
      {
       textBox1.Text = valuesFromQuery.CID;
       textBox2.Text = valuesFromQuery.EmpId;
       textBox3.Text = valuesFromQuery.OID ;
      } 
    
     }
    

     


    Nanda - Misys Software Solutions,Bangalore
    Thursday, August 19, 2010 7:46 AM
  • TKS I think this is shaping up, tks just for others who might find this post useful if you use the above code blindly u will get error like conversion of the different data type so the modified code is below

    private
    
    
     void
    
    
     button1_Click(object
    
    
     sender, RoutedEventArgs e)
    {
    NorthwindLINQDataContext mycontext = new NorthwindLINQDataContext();
    var myquery = from myrow in mycontext.Orders select new { CID = myrow.CustomerID, EmpId = myrow.EmployeeID, OID = myrow.OrderID };

    foreach (var valuesFromQuery in myquery)
    {
    textBox1.Text = valuesFromQuery.CID;
    textBox2.Text = valuesFromQuery.EmpId.ToString();
    textBox3.Text = valuesFromQuery.OID.ToString();
    }

    }

    D.Boy
    Thursday, August 19, 2010 9:00 AM
  • Hi, Venkat anychance could you enlighten me  how to like press button2 then go to next record?

    i remember doing it something like position=-1 or something like that but not really sure on how to  do please advise it would be of great help


    D.Boy
    Thursday, August 19, 2010 9:15 AM
  • var myquery = from myrow in mycontext.Orders select new
     { 
       CID = myrow.CustomerID, EmpId = myrow.EmployeeID, OID = myrow.OrderID 
     };
    //This will retrieve all records from the db
     foreach (var valuesFromQuery in myquery)
      {
        textBox1.Text = valuesFromQuery.CID;
        textBox2.Text = valuesFromQuery.EmpId.ToString();
        textBox3.Text = valuesFromQuery.OID.ToString();
      }
    //This will iterate all elementes inside the query and set the Text inside the textboxes to those values, whoever the final values will be last values 
    

    You need for a way to access the objects in an indexed way, like first, second,... last

    You can do this with the enumerator but you need to control it manually or else use an indexed collection instead such as the alternatives I recommended.

    Regards

    Thursday, August 19, 2010 12:08 PM
  • Since you already get it with a grid, then you just want to do it with

    FormView.

    That class works sort of like a grid, but with inserttemplate etc. also. Pretty sure paging also works there.

    And linqdatasource populate it.

    Thursday, August 19, 2010 12:33 PM
  • Vey very thanka mr......................... tHANK YOU
    Wednesday, August 24, 2011 12:33 PM