Results 1 to 7 of 7

Thread: Populate Data from SQL

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    14

    Populate Data from SQL

    What I would like to do is have data populate in a Textbox2 based on data in Textbox1. Now the data it will be populating is in a SQL table and I want it to Pull data from Column B by looking for the Data from the Column A using whats in Textbox1. The other kicker is Textbox1 is not part of the table I am pulling the data for Textbox2.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populate Data from SQL

    Quote Originally Posted by RWJ_2006 View Post
    The other kicker is Textbox1 is not part of the table I am pulling the data for Textbox2.
    There must be some sort of relationship between them but if you keep it a secret then we can't provide a specific solution.

    I think that the first consideration is how much data there is in total. If there's not too much then you'd probably just retrieve it all into a DataTable and then work with it locally. If there is quite a bit then you'd probably want to query the database each time you needed new data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    14

    Re: Populate Data from SQL

    The data in Textbox1 is the same as in Column A just stored in a different table. Column A is an ID Number and Column B is a name.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Populate Data from SQL

    You didn't address this:
    Quote Originally Posted by jmcilhinney View Post
    I think that the first consideration is how much data there is in total. If there's not too much then you'd probably just retrieve it all into a DataTable and then work with it locally. If there is quite a bit then you'd probably want to query the database each time you needed new data.
    Also, how is the data getting into TextBox1? If the user is entering it then it's not coming from any table but rather it's user input. If it is actually coming from another table then how does it get into TextBox1? What you're asking for could be achieved in various different ways but the best way depends on specific details that you haven't provided. I have a suspicion of exactly what's going on but it's not something that I should have to guess. Are these two Textboxes the only controls involved or are there others? If there are others then exactly what do they contain and where does the data come from? We need a FULL and CLEAR description of the problem to be sure that we are providing a relevant and optimal solution. If we guess then we might get it right or we might end up just wasting our time and yours.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    14

    Re: Populate Data from SQL

    Sorry I am very new at this. The form I am using is to view and modify maintenance work orders. When the work order is created the employee creating it enters there ID. What I want is when Maint. opens this form the employees name populates on the form next to the text box with there ID. The initial data is entered on another screen. Textbox1 is not editable it's just pulling the data from the SQL column in the work order table. The table I want to use to cross reference and pull the name is the employee table. I already have the work order edit form pulling up all the data from the work order table using the SQL system in VB 2008. I just want it to cross reference the employee ID and display the name.

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    14

    Re: Populate Data from SQL

    As for the data it's just a nvarchar(50) field for the employee name.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: Populate Data from SQL

    try this:

    Code:
    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim conn As New SqlConnection("connection string")
            Dim cmd As New SqlCommand("SELECT [Name] FROM employee WHERE employeeID=@employeeID", conn)
            'which of these 2 parameters you use depends on employeeID field datatype
            cmd.Parameters.Add("@employeeID", SqlDbType.Int).Value = CInt(textbox1.text)
            'cmd.Parameters.Add("@employeeID", SqlDbType.VarChar).Value = textbox1.text
            Dim o As Object = cmd.ExecuteScalar
            TextBox2.text = If(o IsNot Nothing, o.ToString, "")
        End Sub
    End Class

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