Solved

Populating a ASP.net form a SQL query

Posted on 2011-02-22
2
302 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - TypeInitializer Error 25 27
tutorial for ebay api 3 34
HTML - Color not displaying correctly in EMAIL. 6 38
Whats wrong in this query - Select * from tableA,tableA 11 31
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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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