Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question - Populating Text Fields

Status
Not open for further replies.

DotNetDunce

Programmer
Aug 23, 2001
47
US
I am in the process of creating an update page that I would like to look like the insert page I created. I wouild like to query the database and bring back my one record and populate my text fields, but I cannot figure out how to do this with the existing data controls. How do I populate a form with values retrieved from the database without using GridView, FormView, etc.

Any help is appreciated!
 
In v2.0 of the framework, a textbox is not considred bindable anymore. I have no idea why, kind of stupid since they have been in previous versions.

You will have to set the text property to the value you want in your dataset or datatable.

ex: Me.TextBox1.Text = MyDS.Tables(0).Rows(0)("TheColumn").ToString
 
Hi Mellow.
What you're looking for is the SQL Data Reader. Here's a snippet of some code I use:

Dim scmSelect As New SqlCommand("SELECT FirstName, LastName, FROM users WHERE Username=@USERNAME", scnSQL)
scmSelect.Parameters.AddWithValue("@USERNAME", txtUsername.Text)
Dim sdrSelect As SqlDataReader
scnSQL.Open()
sdrSelect = scmSelect.ExecuteReader()
While sdrSelect.Read()
FirstName = sdrSelect(0).ToString
LastName = sdrSelect(1).ToString
End While
sdrSelect.Close()
scnSQL.Close()

With the reader, you simply set whatever value you need (like one of your text boxes) equal to the location of it from the SQL query. Mine stores the data into variables, but if you use textbox.Text, you'll be good to go. Give that a try and let us know how it works out (or if my explanation is terrible). Thanks!
 
Thanks! I'll give it a shot and see what happens.
I appreciate your help!
 
Okay, as I said before I am a newbie so please have patience with me. I'm learning EVERYTHING, so with that in mind could you tell me in more detail how I need to implement either of your suggestions. I just can't seem to get it right. Just a little explanation of what your are suggesting.

Thanks again for all your help!
 
here is an example of some code that I use to do what you are asking. Like you I am a new to this. This goes in the codebehind under the event heading Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load:

now what this does is takes an id value (CompanyID) that is coming via querystring on a form, and uses it to openup a connection to my database on SQL server (called myDatabase), finds the legal name of the company, and sticks it in a label on a form called lblProducer Name

Code:
Dim intCompanyID As Integer
        intCompanyID = Request.QueryString("CompanyID")

        Dim connectionString As String = "Data Source=localhost;Initial Catalog=MyDatabase;Integrated Security=True"
        Dim QueryString As String

        QueryString = "SELECT CompanyID, CompanyLegalName "
        QueryString = QueryString & "FROM COMPANY "
        QueryString = QueryString & "WHERE CompanyID =" & intCompanyID

        Using connection As New SqlConnection(connectionString)
            Dim command As New SqlCommand(QueryString, connection)
            connection.Open()
            Dim reader As SqlDataReader = command.ExecuteReader()
            Try
                While reader.Read()
                    Me.lblProducerName.Text = reader.Item("CompanyLegalName")
                End While
            Finally
                ' Always call Close when done reading.
                reader.Close()
            End Try
        End Using
 
Thank you for your help! Maybe I can get something to click in my brain now. The people on here are wonderful!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top