Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 20th, 2004, 02:11 AM
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default populating a dropdownlist within a datagrid

Hi, I'm trying to dynamically populate a dropdownlist within a footer template of a datagrid using vb.net, with no success.

I've tried different methods, here's my latest attempt.

If objArgs.CommandSource.CommandName = "addnew" then
            Dim theddl As DropDownList = CType(objArgs.Item.FindControl("sotdd"), DropDownList)
            strSQL = "SELECT * FROM production_tbl"
            Dim objConnection as New SqlConnection(strConnection)
            Dim objCommand as New SqlCommand(strSQL, objConnection)
            Dim objDataReader as System.Data.SqlClient.SqlDataReader
            response.write(strSQL & "<br>")

            try
            objConnection.Open()
            objCommand.Connection = objConnection
            objCommand.CommandText = "SELECT * FROM production_tbl"
            objDataReader = objCommand.ExecuteReader()
            response.write(objDataReader("client"))

            theddl.DataSource = objDataReader
            theddl.DataTextField = "sotClient"
            theddl.DataValueField = "sotID"
            theddl.DataBind()
             catch when err.number <> 0

                    Response.write(err.description.tostring)

            end try

            objDataReader.Close()
            objConnection.Close()
            exit sub
        End If

I already have a static dropdownlist named "sotdd" sitting in the footer template of the datagrid which looks like this:

<FooterTemplate>
<asp:DropDownList id="sotdd" runat="server" />
</FooterTemplate>

The error I get is object not set to an instance of an object, or no data. I know theres data being pulled from the database, ref. the response.write(objDataReader("client")).

Help! I've being trying for days to get this to work, I've searched everywhere for an answer.

 
Old November 22nd, 2004, 01:02 AM
Registered User
 
Join Date: Nov 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the answer to my own question.

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SQLClient" %>

<script runat="server">

    dim strSQL as string
    dim strConnection = ConfigurationSettings.AppSettings("DSN")

    Sub Page_Load(sender as object, e as eventargs)

        If not Page.IsPostback then
            BindData()
            PopulateList()
        End If

    End Sub


    Sub BindData()

        strSQL = "SELECT * FROM test_tbl"
           dim objDataSet As New DataSet()
          dim objConnection As New SqlConnection(strConnection)
          dim objDataAdapter As New SqlDataAdapter(strSQL, objConnection)

          objDataAdapter.Fill(objDataSet, "TempInfo")

        DataGrid1.DataSource = objDataSet
        DataGrid1.DataBind()

    End Sub


    Function PopulateList()

        strSQL = "SELECT * FROM sot_tbl"
           dim objDataSet As New DataSet()
          dim objConnection As New SqlConnection(strConnection)
          dim objDataAdapter As New SqlDataAdapter(strSQL, objConnection)

          objDataAdapter.Fill(objDataSet, "TempInfo")

          return objDataSet

    End Function

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" Width="306px">
            <Columns>
                <asp:BoundColumn DataField="sotID" HeaderText="sotID"></asp:BoundColumn>
                <asp:TemplateColumn HeaderText="sotClient">
                    <ItemTemplate>
                        <asp:DropDownList id="DropDownList1" DataSource="<%# PopulateList %>" DataTextField="sotClient" DataValueField="sotID" runat="server"></asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateColumn>
            </Columns>
        </asp:DataGrid>

    </form>
</body>
</html>

Easier than I thought. I hope this will help others trying to acheive the same result.






Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: DATAGRID POPULATING emeka VB Databases Basics 0 April 28th, 2007 08:06 PM
Populating DataGrid emeka VB Databases Basics 0 April 28th, 2007 07:13 PM
Populating Dropdownlist in Datagrid Dynamically anup_daware .NET Framework 1.x 1 March 27th, 2006 03:38 PM
Populating DropDownList from multiple sources hawkfb63 ASP.NET 2.0 Professional 2 February 14th, 2006 06:42 PM
Populating DropDownList [email protected] ASP.NET 1.0 and 1.1 Professional 1 November 14th, 2003 03:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.