Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populating a ASP.net form a SQL query

Posted on 2011-02-22
2
Medium Priority
?
308 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
[X]
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
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

609 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