Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspdotnet_website_programming thread: populating asp:dropdownlist with datareader from sql stored procedure


Message #1 by "John Tyson" <jtyson@t...> on Wed, 24 Apr 2002 14:36:49 -0700
Hi,

I hope someone can help me here: I am learning ASP.NET and finally wrote
some code that looked promising, but the code did not populate the
dropdown list on my Web page.  I am trying to drop data from a stored
procedure into a datareader and then populate a dropdownlist web control
with that data.

Here is my stored proc:

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

CREATE Procedure sproc_GetProds
as
select * from tblPayerProd order by product;
GO

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

And following is my .aspx page code.  Any suggestions would be greatly
appreciated (I feel like I'm almost there, it's so frustrating ;-)  My
uid has access to execute the stored proc, I've checked for
misspellings...

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

<%@ Page Language=3D"vb"%>
<%@ Import Namespace=3D"System.Data" %>
<%@ Import Namespace=3D"System.Data.SqlClient" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

   <head>
      <title>Northwest Rehab Alliance (NWRA): Prior Advice
System</title>
   </head>

   <script language=3D"VB" %>

      Sub Page_Load(s As Object, e As EventArgs)

      'Only run this code the first time the page is loaded.

      If Not Page.IsPostBack Then   

         'Create a Connection object.

         Dim MyConnection As SqlConnection =3D New SqlConnection _
		("this info is correct")

         'Create a Command object, and set the connection.

         Dim MyCommand As SqlCommand =3D New SqlCommand()

         With MyCommand.SelectCommand
            .Connection =3D MyConnection
            .CommandText =3D "sproc_GetProds"
            .CommandType =3D CommandType.StoredProcedure
         End With

         Dim MyDataReader As SqlDataReader

         Try
            MyConnection.Open()

            MyDataReader =3D
MyCommand.ExecuteReader(CommandBehavior.CloseConnection)

            ListPayerProd.DataSource =3D MyDataReader
            ListPayerProd.DataBind()

         Catch myException As Exception
            Response.Write(An error has occurred: " &
myException.ToString())

         Finally
           If Not MyDataReader is Nothing Then
             MyDataReader.Close()
           End If

           ListPayerProd.Items.Insert(0, "Select a Product")
             ListPayerProd.SelectedIndex =3D 0
         End Try
      End If

      End Sub

   </script>

   <body>

      <form runat=3D"server">
         <asp:DropDownList id=3D"ListPayerProd" 
DataValueField=3D"productID"
DataTextField=3D"product" runat=3D"server" />
      </form>

   </body>

</html>
Message #2 by came4u dear <came4udear@y...> on Wed, 24 Apr 2002 23:18:46 -0700 (PDT)
Hi John Tyson,

Though I am a novice with ASP.net but with my begining
knowledge what i feel that you have not added
runat="server" attribute with your <script> tag.

I hope that this shud make the script run at server
and it shud get populted.

Do let me know it it works.

Bye 
Manoj

--- John Tyson <jtyson@t...> wrote:
> Hi,
> 
> I hope someone can help me here: I am learning
> ASP.NET and finally wrote
> some code that looked promising, but the code did
> not populate the
> dropdown list on my Web page.  I am trying to drop
> data from a stored
> procedure into a datareader and then populate a
> dropdownlist web control
> with that data.
> 
> Here is my stored proc:
> 
> **********************************************
> 
> CREATE Procedure sproc_GetProds
> as
> select * from tblPayerProd order by product;
> GO
> 
> **********************************************
> 
> And following is my .aspx page code.  Any
> suggestions would be greatly
> appreciated (I feel like I'm almost there, it's so
> frustrating ;-)  My
> uid has access to execute the stored proc, I've
> checked for
> misspellings...
> 
> **********************************************
> 
> <%@ Page Language="vb"%>
> <%@ Import Namespace="System.Data" %>
> <%@ Import Namespace="System.Data.SqlClient" %>
> 
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
> Transitional//EN">
> 
> <html>
> 
>    <head>
>       <title>Northwest Rehab Alliance (NWRA): Prior
> Advice
> System</title>
>    </head>
> 
>    <script language="VB" %>
> 
>       Sub Page_Load(s As Object, e As EventArgs)
> 
>       'Only run this code the first time the page is
> loaded.
> 
>       If Not Page.IsPostBack Then    
> 
>          'Create a Connection object.
> 
>          Dim MyConnection As SqlConnection = New
> SqlConnection _
> 		("this info is correct")
> 
>          'Create a Command object, and set the
> connection.
> 
>          Dim MyCommand As SqlCommand = New
> SqlCommand()
> 
>          With MyCommand.SelectCommand
>             .Connection = MyConnection
>             .CommandText = "sproc_GetProds"
>             .CommandType 
> CommandType.StoredProcedure
>          End With
> 
>          Dim MyDataReader As SqlDataReader
> 
>          Try
>             MyConnection.Open()
> 
>             MyDataReader 
>
MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
> 
>             ListPayerProd.DataSource = MyDataReader
>             ListPayerProd.DataBind()
> 
>          Catch myException As Exception
>             Response.Write(An error has occurred: "
> &
> myException.ToString())
> 
>          Finally
>            If Not MyDataReader is Nothing Then
>              MyDataReader.Close()
>            End If
> 
>            ListPayerProd.Items.Insert(0, "Select a
> Product")
>              ListPayerProd.SelectedIndex = 0
>          End Try
>       End If
> 
>       End Sub
> 
>    </script>
> 
>    <body>
> 
>       <form runat="server">
>          <asp:DropDownList id="ListPayerProd"
> DataValueField="productID"
> DataTextField="product" runat="server" />
>       </form>
> 
>    </body>
> 
> </html>
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/
Message #3 by "John Tyson" <jtyson@t...> on Thu, 25 Apr 2002 06:37:02 -0700
Hi Manoj,

Yes, that was the problem...I spent so much time looking for other
things that I missed the obvious.  Thank you.

John

-----Original Message-----
From: came4u dear [mailto:came4udear@y...]
Sent: Wednesday, April 24, 2002 11:19 PM
To: Website Programming with ASP.NET
Subject: [aspdotnet_website_programming] Re: populating asp:dropdownlist
with datareader from sql stored procedure

Hi John Tyson,

Though I am a novice with ASP.net but with my begining
knowledge what i feel that you have not added
runat=3D"server" attribute with your <script> tag.

I hope that this shud make the script run at server
and it shud get populted.

Do let me know it it works.

Bye
Manoj

--- John Tyson <jtyson@t...> wrote:
> Hi,
>
> I hope someone can help me here: I am learning
> ASP.NET and finally wrote
> some code that looked promising, but the code did
> not populate the
> dropdown list on my Web page.  I am trying to drop
> data from a stored
> procedure into a datareader and then populate a
> dropdownlist web control
> with that data.
>
> Here is my stored proc:
>
> **********************************************
>
> CREATE Procedure sproc_GetProds
> as
> select * from tblPayerProd order by product;
> GO
>
> **********************************************
>
> And following is my .aspx page code.  Any
> suggestions would be greatly
> appreciated (I feel like I'm almost there, it's so
> frustrating ;-)  My
> uid has access to execute the stored proc, I've
> checked for
> misspellings...
>
> **********************************************
>
> <%@ Page Language=3D"vb"%>
> <%@ Import Namespace=3D"System.Data" %>
> <%@ Import Namespace=3D"System.Data.SqlClient" %>
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
> Transitional//EN">
>
> <html>
>
>    <head>
>       <title>Northwest Rehab Alliance (NWRA): Prior
> Advice
> System</title>
>    </head>
>
>    <script language=3D"VB" %>
>
>       Sub Page_Load(s As Object, e As EventArgs)
>
>       'Only run this code the first time the page is
> loaded.
>
>       If Not Page.IsPostBack Then   
>
>          'Create a Connection object.
>
>          Dim MyConnection As SqlConnection =3D New
> SqlConnection _
> 		("this info is correct")
>
>          'Create a Command object, and set the
> connection.
>
>          Dim MyCommand As SqlCommand =3D New
> SqlCommand()
>
>          With MyCommand.SelectCommand
>             .Connection =3D MyConnection
>             .CommandText =3D "sproc_GetProds"
>             .CommandType =3D
> CommandType.StoredProcedure
>          End With
>
>          Dim MyDataReader As SqlDataReader
>
>          Try
>             MyConnection.Open()
>
>             MyDataReader =3D
>
MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
>
>             ListPayerProd.DataSource =3D MyDataReader
>             ListPayerProd.DataBind()
>
>          Catch myException As Exception
>             Response.Write(An error has occurred: "
> &
> myException.ToString())
>
>          Finally
>            If Not MyDataReader is Nothing Then
>              MyDataReader.Close()
>            End If
>
>            ListPayerProd.Items.Insert(0, "Select a
> Product")
>              ListPayerProd.SelectedIndex =3D 0
>          End Try
>       End If
>
>       End Sub
>
>    </script>
>
>    <body>
>
>       <form runat=3D"server">
>          <asp:DropDownList id=3D"ListPayerProd"
> DataValueField=3D"productID"
> DataTextField=3D"product" runat=3D"server" />
>       </form>
>
>    </body>
>
> </html>
>


__________________________________________________
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/

Message #4 by came4u dear <came4udear@y...> on Fri, 26 Apr 2002 04:22:50 -0700 (PDT)
Hello John

It was nice to know that ur problem is solved and that
it shows us the importance of runat parameter.

Like you i also struggled for more than one day but
now i check for this parameter first for every
control.

Bye 
Manoj
--- John Tyson <jtyson@t...> wrote:
> Hi Manoj,
> 
> Yes, that was the problem...I spent so much time
> looking for other
> things that I missed the obvious.  Thank you.
> 
> John
> 
> -----Original Message-----
> From: came4u dear [mailto:came4udear@y...] 
> Sent: Wednesday, April 24, 2002 11:19 PM
> To: Website Programming with ASP.NET
> Subject: [aspdotnet_website_programming] Re:
> populating asp:dropdownlist
> with datareader from sql stored procedure
> 
> Hi John Tyson,
> 
> Though I am a novice with ASP.net but with my
> begining
> knowledge what i feel that you have not added
> runat="server" attribute with your <script> tag.
> 
> I hope that this shud make the script run at server
> and it shud get populted.
> 
> Do let me know it it works.
> 
> Bye 
> Manoj
> 
> --- John Tyson <jtyson@t...> wrote:
> > Hi,
> > 
> > I hope someone can help me here: I am learning
> > ASP.NET and finally wrote
> > some code that looked promising, but the code did
> > not populate the
> > dropdown list on my Web page.  I am trying to drop
> > data from a stored
> > procedure into a datareader and then populate a
> > dropdownlist web control
> > with that data.
> > 
> > Here is my stored proc:
> > 
> > **********************************************
> > 
> > CREATE Procedure sproc_GetProds
> > as
> > select * from tblPayerProd order by product;
> > GO
> > 
> > **********************************************
> > 
> > And following is my .aspx page code.  Any
> > suggestions would be greatly
> > appreciated (I feel like I'm almost there, it's so
> > frustrating ;-)  My
> > uid has access to execute the stored proc, I've
> > checked for
> > misspellings...
> > 
> > **********************************************
> > 
> > <%@ Page Language="vb"%>
> > <%@ Import Namespace="System.Data" %>
> > <%@ Import Namespace="System.Data.SqlClient" %>
> > 
> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
> > Transitional//EN">
> > 
> > <html>
> > 
> >    <head>
> >       <title>Northwest Rehab Alliance (NWRA):
> Prior
> > Advice
> > System</title>
> >    </head>
> > 
> >    <script language="VB" %>
> > 
> >       Sub Page_Load(s As Object, e As EventArgs)
> > 
> >       'Only run this code the first time the page
> is
> > loaded.
> > 
> >       If Not Page.IsPostBack Then    
> > 
> >          'Create a Connection object.
> > 
> >          Dim MyConnection As SqlConnection = New
> > SqlConnection _
> > 		("this info is correct")
> > 
> >          'Create a Command object, and set the
> > connection.
> > 
> >          Dim MyCommand As SqlCommand = New
> > SqlCommand()
> > 
> >          With MyCommand.SelectCommand
> >             .Connection = MyConnection
> >             .CommandText = "sproc_GetProds"
> >             .CommandType 
> > CommandType.StoredProcedure
> >          End With
> > 
> >          Dim MyDataReader As SqlDataReader
> > 
> >          Try
> >             MyConnection.Open()
> > 
> >             MyDataReader 
> >
>
MyCommand.ExecuteReader(CommandBehavior.CloseConnection)
> > 
> >             ListPayerProd.DataSource 
> MyDataReader
> >             ListPayerProd.DataBind()
> > 
> >          Catch myException As Exception
> >             Response.Write(An error has occurred:
> "
> > &
> > myException.ToString())
> > 
> >          Finally
> >            If Not MyDataReader is Nothing Then
> >              MyDataReader.Close()
> >            End If
> > 
> >            ListPayerProd.Items.Insert(0, "Select a
> > Product")
> >              ListPayerProd.SelectedIndex = 0
> >          End Try
> >       End If
> > 
> >       End Sub
> > 
> >    </script>
> > 
> >    <body>
> > 
> >       <form runat="server">
> >          <asp:DropDownList id="ListPayerProd"
> > DataValueField="productID"
> > DataTextField="product" runat="server" />
> >       </form>
> > 
> >    </body>
> > 
> > </html>
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Games - play chess, backgammon, pool and more
> http://games.yahoo.com/
> 
> 


__________________________________________________
Do You Yahoo!?
Yahoo! Games - play chess, backgammon, pool and more
http://games.yahoo.com/

  Return to Index