none
retrieve and display data in textbox vb express newbie RRS feed

  • Question

  • Hi I am using vb express and I want to use linq to retrieve one single piece of data in a table named Income_Names with two columns
    Income_ID
    Income_Name
    I have set up my datacontext called Databasedatacontext.
    I want to retrieve an income name from the table and show it in just the one textbox called textbox1.  I'm sure this is easier than I am thinking it is, I just can't find any examples anywhere.  Only examples showing full table data in datagridviews. Please forgive my ignorance and thank you to anyone who helps at all.
    B
    Thursday, September 4, 2008 5:20 PM

Answers

  • It is simple - this function will do what you want: I've used .Single to get a single record for the given ID. If the ID does not exist this will throw an exception.

     

    Code Snippet

    Function GetIncomeName(Income_ID As Integer) As String

      ' create datacontext

      Dim dc = New Databasedatacontext()

      ' request single record

      Dim result = (From x In dc.Income_Names _

                          Where x.Income_ID = Income_ID _

                          Select x.Income_Name).Single()

      Return result

    End Function

     

     

     

     

    If you don't want an exception for nonexistent values, use SingleOrDefault() - this will return Nothing if the Income_ID is not found.

    Friday, September 5, 2008 7:11 AM
    Answerer
  • The function returns the string you want to put in, so in the form code:

    Code Snippet

     

    Me.Textbox1.Text = GetIncomeName(income_ID)

     

     

     

     

    Saturday, September 6, 2008 8:01 AM
    Answerer

All replies

  • It is simple - this function will do what you want: I've used .Single to get a single record for the given ID. If the ID does not exist this will throw an exception.

     

    Code Snippet

    Function GetIncomeName(Income_ID As Integer) As String

      ' create datacontext

      Dim dc = New Databasedatacontext()

      ' request single record

      Dim result = (From x In dc.Income_Names _

                          Where x.Income_ID = Income_ID _

                          Select x.Income_Name).Single()

      Return result

    End Function

     

     

     

     

    If you don't want an exception for nonexistent values, use SingleOrDefault() - this will return Nothing if the Income_ID is not found.

    Friday, September 5, 2008 7:11 AM
    Answerer
  • Thanks Howard that looks great but how do return the value to the text box on load?
    Do I just put this function in the forms load and
    result=Textbox1.Text ?
    Thanks so much.
    B
    Friday, September 5, 2008 1:12 PM
  • The function returns the string you want to put in, so in the form code:

    Code Snippet

     

    Me.Textbox1.Text = GetIncomeName(income_ID)

     

     

     

     

    Saturday, September 6, 2008 8:01 AM
    Answerer

  •   I am getting an error saying that income_ID was not declared in the forms load and the code
    won't run.
    Me.Textbox1.Text = GetIncomeName(income_ID)
    Thank you for your help.
    B

    Sunday, September 7, 2008 7:36 PM
  • ok I see if I put the actual number (id number) into those parenthesis then it works. Thanks so much Howard.
    B
    Sunday, September 7, 2008 11:59 PM