Results 1 to 8 of 8

Thread: [RESOLVED] How to pass the result of the linq to textbox?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Resolved [RESOLVED] How to pass the result of the linq to textbox?

    How can I pass the result of the linq to a textbox?

    Code:
    string search = txtSearchPO.Text;
    
                IPSBLL.TransactionInfo dbInfo = new IPSBLL.TransactionInfo();
                var q = from info in dbInfo.GetTransactionInfo()
                        where info.PONumber == search
                        select info;
    
                txtPONumber.Text = q;
    I need to access the column data something like this:

    Code:
    DataTable.Rows[index].columnName
    How can I do that in LINQ?

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: How to pass the result of the linq to textbox?

    Try the

    Code:
    txtPONumber.Text = q.FirstOrDefault();
    Please mark you thread resolved using the Thread Tools as shown

  3. #3
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: How to pass the result of the linq to textbox?

    FirstOrDefault can return null so you should check if it's null value.
    also if GetTransactionInfo return an instance of a class then it should be

    Code:
    txtPONumber.Text = q.ProperyName.ToString();
    if ProperyName is a string by default you can remove the "ToString()" part.
    Last edited by motil; Sep 27th, 2010 at 06:46 AM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to pass the result of the linq to textbox?

    Are you expecting multiple rows to be returned from the query? If so, outputting this directly to a TextBox doesn't really make sense. It won't display the results in a usable format.

    Gary

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: How to pass the result of the linq to textbox?

    Yap I am expecting multiple rows result from the query of linq. I tried to pass it into a DataTable and here is my code:

    Code:
                IPSBLL.TransactionInfo db = new IPSBLL.TransactionInfo();
                DataTable dtTransInfo = new DataTable();
                dtTransInfo = db.GetTransactionInfo();
    
                
                var q = from info in dtTransInfo.AsEnumerable()
                        where info.Field<string>("PONumber") == search
                        select info;
                        
                dtTransInfo = q.CopyToDataTable();
    
                txtPONumber.Text = dtTransInfo.Rows[0].Field<string>("PONumber");
    This one works!

    But unfortunately, if I will try to modify my linq query like this:

    Code:
    var q = from info in dtTransInfo.AsEnumerable()
                        where info.Field<string>("PONumber") == search
                        select new { 
                            TransactionInfo = info.Field<int>("TransactionInfoID"), 
                            PONumber = info.Field<string>("PONumber") 
                        };
    The CopyToDataTable(); not valid anymore;

    dtTransInfo = q.CopyToDataTable();

    How can I fix this?

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to pass the result of the linq to textbox?

    Hey,

    I am by no means an expert on LINQ, I have been meaning to read a book on it, but just haven't got round to it.

    Having said that, I think the reason that you are seeing this is due to the inferred type of q, following each query. In the first LINQ query, you are returning a number of different rows, in which case, the inferred type has the CopyToDataTable() method. However, in the second, you are no longer returning a number of different rows from the query, instead, you are returning a new instance of a class, in which case, there isn't the CopyToDataTable() method.

    If you run the debugger, and set a watch on q, in both cases, you should hopefully see what I mean.

    Gary

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2007
    Posts
    240

    Re: [RESOLVED] How to pass the result of the linq to textbox?

    Thank you guys for the help, I have now a clear understanding...

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] How to pass the result of the linq to textbox?

    Glad to hear it!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width