Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: dynamic field population


Message #1 by "Tim Farrell" <timothy.farrell@c...> on Tue, 28 May 2002 18:25:28
I have a form that contains a field that I would like to have dynamically 
populated from a seperate table on my database.  I have been searching 
most of my morning looking for some sort of tutorial on how to produce 
this functionality using VS.Net to no avail.

Can someone point me in the right direction.  It used to be pretty simple 
in VS6.0 (right click on form field and select db-table-column) but now 
things have changed.

Thank you very much for your thoughts.

Sincerely,

Tim
Message #2 by Imar Spaanjaars <Imar@S...> on Tue, 28 May 2002 20:08:18 +0200
Hi Tim,

It's still quite easy. You could do it through the IDE: create connections, 
commands etc and then use the property sheet to assign the DataTextField 
and DataValueField.
Below, however, you'll find an example that does hand coded. It could be 
done more efficient (who knows) but it will at least do what you want:

First, add a drop down to your ASPX page:

<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

Then, in the code behind, use the following code inside the Page_Load event:

             If Not Page.IsPostBack Then
                 ' Dim and create a dataset, a connection, a command and an 
adapter
                 Dim ds As DataSet = New DataSet()
                 Dim conn As SqlConnection = New 
SqlConnection(YourSQLConnectionStringOrMethodThatReturnsAStringHere)
                 Dim cmd1 As SqlCommand = New SqlCommand("SELECT ID, Name 
FROM MyTable ORDER BY Name", conn)
                 Dim myDA As SqlDataAdapter = New SqlDataAdapter()

                 ' Set the Command and CommandType
                 myDA.SelectCommand = cmd1
                 myDA.SelectCommand.CommandType = CommandType.Text

                 conn.Open()

                 ' Fill the dataset
                 myDA.Fill(ds, "MyTable")

                 ' Assign the datasource and the fields from your query to 
the value and display fields
                 DropDownList1.DataSource = ds.Tables("MyTable")
                 DropDownList1.DataTextField = "Name"
                 DropDownList1.DataValueField = "ID"

                 ' Bind the whole thingy to the DropDown
                 DropDownList1.DataBind()
                 conn.Close()
             End If


There are some great examples in the (on-line) documentation from both the 
framework and Visual Studio that show how to do this in other ways and in 
other scenarios as well.

HtH

Imar


At 06:25 PM 5/28/2002 +0000, you wrote:
>I have a form that contains a field that I would like to have dynamically
>populated from a seperate table on my database.  I have been searching
>most of my morning looking for some sort of tutorial on how to produce
>this functionality using VS.Net to no avail.
>
>Can someone point me in the right direction.  It used to be pretty simple
>in VS6.0 (right click on form field and select db-table-column) but now
>things have changed.
>
>Thank you very much for your thoughts.
>
>Sincerely,
>
>Tim


Message #3 by "Farrell, Timothy" <Timothy.Farrell@C...> on Tue, 28 May 2002 14:31:02 -0400
Thanks Imar!

Although I do prefer to do it through code and not IDE, do you know of good
resource for learning the IDE method as well?

Thank you again for your help.  I have learned a lot from your examples and
patience.

Sincerely,

Tim

-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Tuesday, May 28, 2002 2:08 PM
To: aspx_beginners
Subject: [aspx_beginners] Re: dynamic field population


Hi Tim,

It's still quite easy. You could do it through the IDE: create connections, 
commands etc and then use the property sheet to assign the DataTextField 
and DataValueField.
Below, however, you'll find an example that does hand coded. It could be 
done more efficient (who knows) but it will at least do what you want:

First, add a drop down to your ASPX page:

<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

Then, in the code behind, use the following code inside the Page_Load event:

             If Not Page.IsPostBack Then
                 ' Dim and create a dataset, a connection, a command and an 
adapter
                 Dim ds As DataSet = New DataSet()
                 Dim conn As SqlConnection = New 
SqlConnection(YourSQLConnectionStringOrMethodThatReturnsAStringHere)
                 Dim cmd1 As SqlCommand = New SqlCommand("SELECT ID, Name 
FROM MyTable ORDER BY Name", conn)
                 Dim myDA As SqlDataAdapter = New SqlDataAdapter()

                 ' Set the Command and CommandType
                 myDA.SelectCommand = cmd1
                 myDA.SelectCommand.CommandType = CommandType.Text

                 conn.Open()

                 ' Fill the dataset
                 myDA.Fill(ds, "MyTable")

                 ' Assign the datasource and the fields from your query to 
the value and display fields
                 DropDownList1.DataSource = ds.Tables("MyTable")
                 DropDownList1.DataTextField = "Name"
                 DropDownList1.DataValueField = "ID"

                 ' Bind the whole thingy to the DropDown
                 DropDownList1.DataBind()
                 conn.Close()
             End If


There are some great examples in the (on-line) documentation from both the 
framework and Visual Studio that show how to do this in other ways and in 
other scenarios as well.

HtH

Imar


At 06:25 PM 5/28/2002 +0000, you wrote:
>I have a form that contains a field that I would like to have dynamically
>populated from a seperate table on my database.  I have been searching
>most of my morning looking for some sort of tutorial on how to produce
>this functionality using VS.Net to no avail.
>
>Can someone point me in the right direction.  It used to be pretty simple
>in VS6.0 (right click on form field and select db-table-column) but now
>things have changed.
>
>Thank you very much for your thoughts.
>
>Sincerely,
>
>Tim






The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it. 

Message #4 by "Farrell, Timothy" <Timothy.Farrell@C...> on Tue, 28 May 2002 15:59:42 -0400
Imar,

Sorry to trouble you with this but you take a look at my code, I cannot seem
to get any data to the dropdownlist I created.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then

            'Dim and create a dataset, a connection, a command and an
adapter
            Dim ds As DataSet = New DataSet()
            Dim Conn As SqlConnection = New
SqlConnection("server=ntphoenix;database=access;uid=sa;pwd=;")
            Dim cmd1 As SqlCommand = New SqlCommand("select AccessID,
AccessLevel from UserAccess", Conn)
            Dim myDA As SqlDataAdapter = New SqlDataAdapter()

            'Set the Command and CommandType
            myDA.SelectCommand = cmd1
            myDA.SelectCommand.CommandType = CommandType.Text

            Conn.Open()

            'Fill the dataset
            myDA.Fill(ds, "UserAccess")

            'Assign the datasource and the fields from the query to the
value and display fields
            DropDownList1.DataSource = ds.Tables("UserAccess")
            DropDownList1.DataTextField = "AccessLevel"
            DropDownList1.DataValueField = "AccessID"

            'Bind the Whole thing
            DropDownList1.DataBind()
            Conn.Close()
        End If

    End Sub

*********************************
The code for the drop down is:
<asp:DropDownList id="DropDownlist1" runat="server"></asp:DropDownList>

Thank you again for all your help!

Sincerely,
Tim

-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Tuesday, May 28, 2002 2:08 PM
To: aspx_beginners
Subject: [aspx_beginners] Re: dynamic field population


Hi Tim,

It's still quite easy. You could do it through the IDE: create connections, 
commands etc and then use the property sheet to assign the DataTextField 
and DataValueField.
Below, however, you'll find an example that does hand coded. It could be 
done more efficient (who knows) but it will at least do what you want:

First, add a drop down to your ASPX page:

<asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>

Then, in the code behind, use the following code inside the Page_Load event:

             If Not Page.IsPostBack Then
                 ' Dim and create a dataset, a connection, a command and an 
adapter
                 Dim ds As DataSet = New DataSet()
                 Dim conn As SqlConnection = New 
SqlConnection(YourSQLConnectionStringOrMethodThatReturnsAStringHere)
                 Dim cmd1 As SqlCommand = New SqlCommand("SELECT ID, Name 
FROM MyTable ORDER BY Name", conn)
                 Dim myDA As SqlDataAdapter = New SqlDataAdapter()

                 ' Set the Command and CommandType
                 myDA.SelectCommand = cmd1
                 myDA.SelectCommand.CommandType = CommandType.Text

                 conn.Open()

                 ' Fill the dataset
                 myDA.Fill(ds, "MyTable")

                 ' Assign the datasource and the fields from your query to 
the value and display fields
                 DropDownList1.DataSource = ds.Tables("MyTable")
                 DropDownList1.DataTextField = "Name"
                 DropDownList1.DataValueField = "ID"

                 ' Bind the whole thingy to the DropDown
                 DropDownList1.DataBind()
                 conn.Close()
             End If


There are some great examples in the (on-line) documentation from both the 
framework and Visual Studio that show how to do this in other ways and in 
other scenarios as well.

HtH

Imar


At 06:25 PM 5/28/2002 +0000, you wrote:
>I have a form that contains a field that I would like to have dynamically
>populated from a seperate table on my database.  I have been searching
>most of my morning looking for some sort of tutorial on how to produce
>this functionality using VS.Net to no avail.
>
>Can someone point me in the right direction.  It used to be pretty simple
>in VS6.0 (right click on form field and select db-table-column) but now
>things have changed.
>
>Thank you very much for your thoughts.
>
>Sincerely,
>
>Tim






The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it. 

Message #5 by Imar Spaanjaars <Imar@S...> on Tue, 28 May 2002 22:32:45 +0200
Wrox has released a book called "Effective Visual Studio .NET". I haven't 
read it yet, but I did read some pretty positive reviews.

Imar


At 02:31 PM 5/28/2002 -0400, you wrote:
>Thanks Imar!
>
>Although I do prefer to do it through code and not IDE, do you know of good
>resource for learning the IDE method as well?
>
>Thank you again for your help.  I have learned a lot from your examples and
>patience.
>
>Sincerely,
>
>Tim


Message #6 by Imar Spaanjaars <Imar@S...> on Tue, 28 May 2002 22:41:39 +0200
Hi Tim,

Did you add an Imports statement for the SQL Client to your code?

Add this to the top of your code behind page:

Imports System.Data.SqlClient


HtH

Imar


At 03:59 PM 5/28/2002 -0400, you wrote:
>Imar,
>
>Sorry to trouble you with this but you take a look at my code, I cannot seem
>to get any data to the dropdownlist I created.
>
>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Load
>         If Not Page.IsPostBack Then
>
>             'Dim and create a dataset, a connection, a command and an
>adapter
>             Dim ds As DataSet = New DataSet()
>             Dim Conn As SqlConnection = New
>SqlConnection("server=ntphoenix;database=access;uid=sa;pwd=;")
>             Dim cmd1 As SqlCommand = New SqlCommand("select AccessID,
>AccessLevel from UserAccess", Conn)
>             Dim myDA As SqlDataAdapter = New SqlDataAdapter()
>
>             'Set the Command and CommandType
>             myDA.SelectCommand = cmd1
>             myDA.SelectCommand.CommandType = CommandType.Text
>
>             Conn.Open()
>
>             'Fill the dataset
>             myDA.Fill(ds, "UserAccess")
>
>             'Assign the datasource and the fields from the query to the
>value and display fields
>             DropDownList1.DataSource = ds.Tables("UserAccess")
>             DropDownList1.DataTextField = "AccessLevel"
>             DropDownList1.DataValueField = "AccessID"
>
>             'Bind the Whole thing
>             DropDownList1.DataBind()
>             Conn.Close()
>         End If
>
>     End Sub
>
>*********************************
>The code for the drop down is:
><asp:DropDownList id="DropDownlist1" runat="server"></asp:DropDownList>
>
>Thank you again for all your help!
>
>Sincerely,
>Tim


Message #7 by "Farrell, Timothy" <Timothy.Farrell@C...> on Wed, 29 May 2002 09:18:10 -0400
WROX!  I should have known.  I must have at least 6 of their books.  Great
authors who write in plain english and use relevant examples.

Thanks again!

Tim

-----Original Message-----
From: Imar Spaanjaars [mailto:Imar@S...]
Sent: Tuesday, May 28, 2002 4:33 PM
To: aspx_beginners
Subject: [aspx_beginners] Re: dynamic field population


Wrox has released a book called "Effective Visual Studio .NET". I haven't 
read it yet, but I did read some pretty positive reviews.

Imar


At 02:31 PM 5/28/2002 -0400, you wrote:
>Thanks Imar!
>
>Although I do prefer to do it through code and not IDE, do you know of good
>resource for learning the IDE method as well?
>
>Thank you again for your help.  I have learned a lot from your examples and
>patience.
>
>Sincerely,
>
>Tim






The contents of this e-mail are intended for the named addressee only. It
contains information that may be confidential. Unless you are the named
addressee or an authorized designee, you may not copy or use it, or disclose
it to anyone else. If you received it in error please notify us immediately
and then destroy it. 

Message #8 by "Tim Farrell" <timothy.farrell@c...> on Wed, 29 May 2002 17:58:08
Imar,

Here is the code you requested:

First Page - InputForm.aspx:
<!--#include file="Styles.css" -->
<form runat="server">
<input type="hidden" runat="server" id="hiddenID">
<table cellspacing="5">
  <tr class="tabletext">
  <td align="right">Last Name:</td>
  <td>
<asp:textbox id="LastName" columns="30" maxlength="40" 
runat="server" /></td>
</tr>
<tr class="tabletext">
<td align="right">First Name:</td>
<td>
<asp:textbox id="FirstName" columns="30" maxlength="40" 
runat="server" /></td>
</tr>
<tr class="tabletext">
<td align="right">Initials:</td>
<td>
<asp:textbox id="Initials" columns="3" maxlength="4" runat="server" /></td>
</tr>
<tr class="tabletext">
<td align="right">Username:</td>
<td>
<asp:textbox id="uid" columns="3" maxlength="4" runat="server" /></td>
</tr>
<tr class="tabletext">
<td align="right">Password:</td>
<td>
<asp:textbox id="pwd" columns="5" maxlength="8" runat="server" /></td>
</tr>
<tr class="tabletext">
<td align="right">Secuirty_Level:</td>
<td>
<asp:DropDownList id="security" AutoPostBack="True" runat="server" /></td>
</tr>
<tr class="tabletext">
<td colspan="2" align="middle">
<input type="submit" name="btnSubmit" runat="server" value="Save"> <input 
type="reset" name="btnReset" runat="server" value="Clear">
</td>
</tr>
</table>
</form>

Here is the code-behind page:

Imports System.Data.SqlClient
Public Class InputForm
    Inherits System.Web.UI.Page
    Protected WithEvents LastName As System.Web.UI.WebControls.TextBox
    Protected WithEvents FirstName As System.Web.UI.WebControls.TextBox
    Protected WithEvents Initials As System.Web.UI.WebControls.TextBox
    Protected WithEvents uid As System.Web.UI.WebControls.TextBox
    Protected WithEvents pwd As System.Web.UI.WebControls.TextBox
    Protected WithEvents security As System.Web.UI.WebControls.DropDownList


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub 
InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then

            'Dim and create a dataset, a connection, a command and an 
adapter
            Dim ds As DataSet = New DataSet()
            Dim conn As SqlConnection = New SqlConnection
("server=ntphoenix;database=plan;uid=sa;pwd=;")
            Dim cmd1 As SqlCommand = New SqlCommand("Select AccessID, 
AccessLevel from UserAccess", conn)
            Dim myDA As SqlDataAdapter = New SqlDataAdapter()

            'Set the Command and CommandType
            myDA.SelectCommand = cmd1
            myDA.SelectCommand.CommandType = CommandType.Text

            conn.Open()

            'Fill the dataset
            myDA.Fill(ds, "UserAccess")

            'Assign the datasource and the fields from the query to the 
value and display fields
            security.DataSource = ds.Tables("UserAccess")
            security.DataTextField = "AccessLevel"
            security.DataValueField = "AccessID"

            'Bind the Whole thing
            security.DataBind()
            conn.Close()
        End If

    End Sub

End Class

***************************

Thank you for your thoughts.

Sincerely,

Tim
Message #9 by Imar Spaanjaars <Imar@S...> on Wed, 29 May 2002 19:53:18 +0200
Hi Tim,


Hmmm, can't remember requesting any code for this thread. You're mixing up 
threads as well?? ;-)

Does it work, the way your code is presented here? The code you sent should 
work. Does your code return any data from the database when you run it in 
the SQL Server Query Analyzer? Does SA indeed have no password?

What error message are you getting? What exactly doesn't work?

Imar


At 05:58 PM 5/29/2002 +0000, you wrote:
>Imar,
>
>Here is the code you requested:

<snip>

>Sincerely,
>
>Tim



  Return to Index