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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Access

Status
Not open for further replies.

NaiDanac

Programmer
Mar 25, 2004
96
US
Forgive me for asking a primitive question, but I've been searching/reading for a while, and I'm just getting more confused.

I've got a textbox, who's value is coming form a table from the database (SQL2K). How can I do that? I read about dataset, datalists, datagrids, etc...and I'm confused!

I think it should be simple!

Appreciate the help.
 
Nail - if its just one value, and not a list box, or repeater, you can just pass through a data reader and assign the value to the textbox, e.g.,

Code:
...
Do While myreader.Read()
 txtA.Text = myreader("ContactID")
 ...          
End While 
...

..alternatively, if you are populating repeated textboxes, you might use a variation of the following (using a Template column):

Code:
...
<asp:Textbox id="txtA" runat="server" Text = '<%# DataBinder.Eval(Container.DataItem, "MeetingDate")%>'/>
...

Of course there are many ways, depending on the exact details, you could retrieve the value from Session or Cache. But if it is simply a single textbox you can use the one pass reader approach. In a scheme similar to the repeater example, you might achieve simple bind by using something like:

Code:
...
<asp:Textbox id="txtA" runat="server" Text="<%# ContactID %>"/>
...

...someone may drop by and add to this, just a few thoughts.

 
Can you elaborate on how I can connect to the db?

Thanks
 
Open up a connection, something along the lines of:

Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            Dim sqlconn As SqlClient.SqlConnection
            Dim cmdCantone As SqlClient.SqlCommand
            sqlconn = New SqlClient.SqlConnection("Server=appdata.auburn.edu;uid=yourid;pwd=yourpw;database=yourdb")
            cmdCantone = New SqlClient.SqlCommand("SELECT DISTINCT tblCantones.PB3Name, tblCantones.PB3ID FROM tblCantones JOIN tblParroquias ON tblCantones.PB3ID = tblParroquias.PB3ID JOIN tblCommunities ON tblParroquias.PB2ID = tblCommunities.PB2ID JOIN tblSites ON tblCommunities.PB1ID = tblSites.PB1ID JOIN tblChemData ON tblSites.SiteCode = tblChemData.SiteCode WHERE tblCantones.PB4ID =" & "'" & Request.QueryString("PB4ID") & "'" & "AND tblChemData.SiteCode Is Not Null ORDER BY PB3Name", sqlconn)
            Try
                sqlconn.Open()
                ddCantone.DataSource = cmdCantone.ExecuteReader()
                ddCantone.DataTextField = "PB3Name"
                ddCantone.DataValueField = "PB3ID"
                ddCantone.DataBind()
                sqlconn.Close()
            Catch objException As SqlClient.SqlException
                Dim objError As SqlClient.SqlError
                For Each objError In objException.Errors
                    Response.Write("<li>" & objError.Message)
                Next
            Finally
                sqlconn.Close()
            End Try
        End If
    End Sub

In this example a connection is made to an SQL database in order to populate a dropdown list. You can substitute in an SQL Reader.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top