Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Populating a ASP.net form a SQL query

Posted on 2011-02-22
2
304 Views
Last Modified: 2012-05-11
I have a basic web form that has 40 text field controls.  This form is populated via a SQL select statement procedure.  Currently I use the following VB code to populate the form, however there must be a better way that doesn't use so much code.  Any suggestions?

How can I link SqlDataSource and SQL procedure directly with the controls without having to write code for each text control?


ASP File -
<asp:SqlDataSource ID="sp_GetData" runat="server"
          ConnectionString="<%$ ConnectionStrings:GetDataConnectionString %>"
          SelectCommand="sp_GetData" SelectCommandType="StoredProcedure">
          <SelectParameters>
              <asp:Parameter Name="UserLgn" Type="String" />
                        </SelectParameters>
</asp:SqlDataSource>



ASPX.VB - 

Dim userLgn As String = Request.ServerVariables("LOGON_USER")
            sp_GetMYConsData.SelectParameters("UserLgn").DefaultValue = userLgn
           
Dim matrixDv As DataView = sp_GetMYConsData.Select(DataSourceSelectArguments.Empty)
            If matrixDv.Count > 0 Then
                For Each matrixDr As DataRowView In matrixDv
                    If Not IsDBNull(matrixDr("Activity1")) Then
                        Activity1.Text = matrixDr("Activity1")
                    End If
                    If Not IsDBNull(matrixDr("Activity2")) Then
                        Activity2.Text = matrixDr("Activity2")
                    End If
                    If Not IsDBNull(matrixDr("Activity3")) Then
                        Activity3.Text = matrixDr("Activity3")
                    End If
                    If Not IsDBNull(matrixDr("Activity4")) Then
                        Activity4.Text = matrixDr("Activity4")
                    End If
                    If Not IsDBNull(matrixDr("Activity5")) Then
                        Activity5.Text = matrixDr("Activity5")
                    End If

.... etc...etc.. for 40 variables... How can I link SqlDataSource and SQL procedure directly with the controls without having to write code for each control?

Open in new window

0
Comment
Question by:TedgCl
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34958670
This would be slightly slower because of the FindControl() call but it would reduce the amount of code:
Dim tb As TextBox
For Each matrixDr As DataRowView In matrixDv
    For i As Integer = 1 To 20
        If Not IsDBNull(matrixDr("Activity" & i.ToString())) Then
            tb =CType(FindControl("Activity" & i.ToString()), TextBox)
            tb.Text = matrixDr("Activity" & i.ToString())
        End If
    Next
    ...
Next

Open in new window

0
 
LVL 7

Expert Comment

by:mr_nadger
ID: 34961464
you could use any of the data display objects, e.g.
gridview
formview
detailsview

If you're updating the database with changes in the text boxes, using this approach will significantly reduce the coding required there too
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
insert value of checklistbox checked 4 32
Generate Numbers in JQuery file 11 66
Error message when using PrintSocket.Send 7 18
Access/Visual Basic Question 3 25
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question