• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

MSSQL data to many textbox using a loop

Very new to asp.net but very well versed with classic asp. Need assistance to pull data from mssql and fill many textboxes, looking for a loop that goes thought the columns and fills the textbox that have the same name. also in this group are pull down menus that need to change based on the data . In my web.config file I have a connection String named CLLDdataSQLConnectionString

Please remember very new to asp.net and need the connection code to accompany the solution
  • 4
  • 2
1 Solution
See http://www.connectionstrings.com/ to set up connection string.
crundleAuthor Commented:
I actually have the below code but wanted to get ride of all the textbox = filed's to clean up and make more managable
 Protected Sub Page_Load(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.Load
        ' If Not Page.IsPostBack Then
        '  FillAuthorList()
        'End If
        ' Create a Select statement that searches for a record
        ' matching the specific author ID from the Value property.
        Dim selectSQL As String
        selectSQL = "SELECT * FROM tblshipment "
        selectSQL &= "WHERE ShipmentID= 1499"
        ' Define the ADO.NET objects.
        Dim con As New SqlConnection(connectionString)
        Dim cmd As New SqlCommand(selectSQL, con)
        Dim reader As SqlDataReader
        ' Try to open database and read information.
            reader = cmd.ExecuteReader()
            ' Fill the controls.
            'For x = 0 as integer
            '  For x to 100
            MBL.Text = reader("MBL").ToString()
            'CLID.Text = reader("CLID").ToString()
            MBL.Text = reader("MBL").ToString()
            HBL.Text = reader("HBL").ToString()
            SUB_BL.Text = reader("Sub_BL").ToString()
            Client_Name.Text = reader("CLIENT_NAME").ToString()
            'CLIENT_ID.Text = reader("CLIENT_ID").ToString()
            Shipper_Name.Text = reader("Shipper_Name").ToString()
            Shipper_Address.Text = reader("Shipper_Address").ToString()
            Shipper_address2.Text = reader("Shipper_address2").ToString()
            Shipper_address3.Text = reader("Shipper_address3").ToString()
            FA_Name.Text = reader("FA_Name").ToString()
            FA_Address.Text = reader("FA_Address").ToString()
            FA_address2.Text = reader("FA_address2").ToString()
            FA_address3.Text = reader("FA_address3").ToString()
            Consignee_Name.Text = reader("Consignee_Name").ToString()
            Consignee_Address.Text = reader("Consignee_Address").ToString()
            Consignee_Address2.Text = reader("Consignee_address2").ToString()
            Consignee_Address3.Text = reader("Consignee_address3").ToString()
            Notify_Name.Text = reader("Notify_Name").ToString()
            Notify_Address.Text = reader("Notify_Address").ToString()
            Notify_address2.Text = reader("Notify_address2").ToString()
            Notify_address3.Text = reader("Notify_address3").ToString()
            Broker.Text = reader("Broker").ToString()
            'SSLType_of_Move.Text = reader("SSLType_of_Move").ToString()
            'Railmove.Text = reader("Railmove").ToString()
            'CUSTSSLType_of_Move.Text = reader("CUSTSSLType_of_Move").ToString()
            POINT_OF_ORIGIN.Text = reader("POINT_OF_ORIGIN").ToString()
            Voyage_Flight.Text = reader("Voyage_Flight").ToString()
            Vessel_Airline.Text = reader("Vessel_Airline").ToString()
            Carrier.Text = reader("Carrier").ToString()
            Port_of_Loading.Text = reader("Port_of_Loading").ToString()
            Port_of_Discharge.Text = reader("Port_of_Discharge").ToString()
            Rail_destination.Text = reader("Rail_destination").ToString()
            Place_of_Delivery.Text = reader("Place_of_Delivery").ToString()
            IT_Number.Text = reader("IT_Number").ToString()
            FirmCode.Text = reader("FirmCode").ToString()
            'active.Text = reader("active").ToString()
            'EnteredBy.Text = reader("EnteredBy").ToString()
            'Lastupdateby.Text = reader("Lastupdateby").ToString()
            'seawaybl.Text = reader("seawaybl").ToString()
            ProDescription.Text = reader("ProDescription").ToString()
            IT_Number_Date.Text = reader("IT_Number_Date").ToString()
            'EnteredDate.Text = reader("EnteredDate").ToString()
            ' LastUpdate.Text = reader("LastUpdate").ToString()
            ' chkContract.Checked = CType(reader("contract"), Boolean)
            ' lblResults.Text = ""
        Catch Err As Exception
            '  lblResults.Text = "Error getting author. "
            ' lblResults.Text &= Err.Message
        End Try
    End Sub

Open in new window

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Here's one option:

Instead of doing a select *.. explicitly mention the column names in the select statement. I'll give an example for just 3 fields.

Say your SELECT statement looks like this:
"SELECT MBL, CLID, HBL FROM tblshipment WHERE ShipmentID= 1499"

Create an array of textboxes:

List<TextBox> tbs = new List<TextBox>();
'Add the textboxes in the same order as the columns in your SELECT statement.

For i = 0 to tbs.Count
   tbs(i).Text = reader(i).ToString()
Next i
crundleAuthor Commented:
I copy the above but tbs, list  is not declaired. is that VB code or c#. sorry very new to asp.net
My apologies... I had mixed up C# and vb.net. Here's the right version:

Dim tbs As New List(Of TextBox)()  
'Add the textboxes in the same order as the columns in  your SELECT statement.  
For i As Integer = 0  To tbs.Count - 1  
    tbs(i).Text = reader(i).ToString()  


Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now