Solved

Populating a ASP.net form a SQL query

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now