DeveloperBarn Forums

DeveloperBarn

Programming & IT forum

populate datasource control from DDL

This is a discussion on populate datasource control from DDL within the .Net Development forums, part of the Programming & Scripting category; Hi I need to populate a DataSource control with an id from a ddl, this DataSource control the populates a ...

Go Back   DeveloperBarn Forums > Programming & Scripting > .Net Development

  #1  
Old June 30th, 2008, 04:17 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 280
Thanks: 31
Thanked 8 Times in 8 Posts
Rep Power: 2
Shem is on a distinguished road
Default populate datasource control from DDL

Hi

I need to populate a DataSource control with an id from a ddl, this
DataSource control the populates a ddl.

I have no clue how to pass the id to the DataSource control, I
have tried a few things but no luck yet.

my application is built on a 3 tier design, BL,BO,DA layers.
I'll post the code relevant for the DataSource control i'm trying
to populate.

DA: (in my DA i also have a class that holds my querystrings)
the query:
Code:
Public Class QueryZonesDB
        Public Shared strSQL

        Public Shared Function Zones(ByVal pid As Integer)
            strSQL = "SELECT "
            strSQL = strSQL & "idkey, "
            strSQL = strSQL & "project_id, "
            strSQL = strSQL & "userID, "
            strSQL = strSQL & "edited, "
            strSQL = strSQL & "name, "
            strSQL = strSQL & "description "
            strSQL = strSQL & "FROM tblzones "
            strSQL = strSQL & "WHERE idkey = " & pid & " "
            strSQL = strSQL & "ORDER BY name ASC"
            Return strSQL
        End Function
Code:
Public Class ZonesDB

Public Shared Function GetList(ByVal id As Integer) As ZonesList
            Dim tempList As ZonesList = Nothing
            'Using
            Dim myConnection As MySqlConnection = New MySqlConnection(myConfig.myConnection)
            Try
                Dim myCommand = New MySqlCommand(QueryZonesDB.Zones(id), myConnection)
                myConnection.Open()
                ' Using
                Dim myReader As MySqlDataReader = myCommand.ExecuteReader
                Try
                    If myReader.HasRows Then
                        tempList = New ZonesList
                        While myReader.Read
                            tempList.Add(FillDataRecord(myReader))
                        End While
                    End If
                    myReader.Close()
                Finally
                    CType(myReader, IDisposable).Dispose()
                End Try
            Finally
                CType(myConnection, IDisposable).Dispose()
            End Try
            Return tempList
        End Function

Private Shared Function FillDataRecord(ByVal myDataRecord As IDataRecord) As Zones
            Dim myZones As Zones = New Zones
            myZones.Idkey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("idkey"))
            myZones.ProjectID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("project_id"))
            myZones.UserID = myDataRecord.GetInt32(myDataRecord.GetOrdinal("userID"))
            myZones.Edited = myDataRecord.GetInt32(myDataRecord.GetOrdinal("edited"))
            myZones.Name = myDataRecord.GetString(myDataRecord.GetOrdinal("name"))
            myZones.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("description"))
            Return myZones
        End Function
BO:
Code:
Public Class Zones

        Private _idkey As Nullable(Of Integer)
        Private _project_id As Nullable(Of Integer)
        Private _user_id As Nullable(Of Integer)
        Private _edited_id As Nullable(Of Integer)
        Private _name As String = String.Empty
        Private _description As String = String.Empty

        <DataObjectFieldAttribute(True, True, False)> _
        Public Property Idkey() As Integer
            Get
                Return _idkey
            End Get
            Set(ByVal value As Integer)
                _idkey = value
            End Set
        End Property

        Public Property ProjectID() As Integer
            Get
                Return _project_id
            End Get
            Set(ByVal value As Integer)
                _project_id = value
            End Set
        End Property

        Public Property UserID() As Integer
            Get
                Return _user_id
            End Get
            Set(ByVal value As Integer)
                _user_id = value
            End Set
        End Property

        Public Property Edited() As Integer
            Get
                Return _edited_id
            End Get
            Set(ByVal value As Integer)
                _edited_id = value
            End Set
        End Property

        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Public Property Description() As String
            Get
                Return _description
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

    End Class
List collection in my BO:
Code:
    Public Class ZonesList

        Inherits List(Of Zones)

        Public Sub New()
        End Sub

    End Class
BL:
Code:
Public Class ZonesManager

        <DataObjectMethod(DataObjectMethodType.Select, True)> _
        Public Shared Function GetList(ByVal id As Integer) As ZonesList
            Return ZonesDB.GetList(id)
        End Function
my page with the DDL and DS controls:
Code:
<form id="form1" runat="server">
    <div>
        <p>
            <asp:Label ID="lblWelcome" runat="server"></asp:Label>&nbsp;|&nbsp;
            <asp:HyperLink ID="logout" runat="server" NavigateUrl="~/Default.aspx">LogOut</asp:HyperLink>
        </p>
    </div>
    <div>
        <p>
            <asp:DropDownList ID="ProjectDDL" runat="server" AutoPostBack="True" DataSourceID="ProjectDataSource"
                DataTextField="Name" DataValueField="Idkey">
            </asp:DropDownList>
            <asp:ObjectDataSource 
                ID="ProjectDataSource" 
                runat="server" 
                DataObjectTypeName="Keith.ProjectManager.BO.Projects"
                DeleteMethod="Delete" 
                InsertMethod="Save" 
                SelectMethod="GetList" 
                UpdateMethod="Update" 
                TypeName="Keith.ProjectManager.Bll.ProjectsManager">
                <SelectParameters>
                    <asp:Parameter Name="id" Type="Int32" />
                </SelectParameters>
            </asp:ObjectDataSource>
        </p>
    </div>
    <div>
        <p>
            <asp:Label ID="textmsg" runat="server"></asp:Label>&nbsp;
            <asp:DropDownList ID="ZonesDDL" runat="server" AutoPostBack="True" DataSourceID="ZonesDataSource"
                DataTextField="Name" DataValueField="Idkey" Visible="False">
                <asp:ListItem>Select Here</asp:ListItem>
            </asp:DropDownList>
            <asp:ObjectDataSource 
                ID="ZonesDataSource" 
                runat="server" 
                DataObjectTypeName="Keith.ProjectManager.BO.Zones"
                DeleteMethod="Delete" 
                InsertMethod="Save" 
                SelectMethod="GetList" 
                TypeName="Keith.ProjectManager.Bll.ZonesManager" 
                UpdateMethod="Update">
                <SelectParameters>
                    <asp:Parameter Name="id" />
                </SelectParameters>
            </asp:ObjectDataSource>
        </p>
    </div>
    </form>
any help would be great

thanks
Shem
Reply With Quote
  #2  
Old June 30th, 2008, 04:38 AM
richyrich's Avatar
Administrator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 876
Thanks: 46
Thanked 76 Times in 75 Posts
Blog Entries: 2
Rep Power: 5
richyrich is just really nicerichyrich is just really nicerichyrich is just really nicerichyrich is just really nice
Default

So basically you want to pass the selected value from ProjectDDL to ZonesDataSource which will populate ZonesDDL.

Is that about right?
Reply With Quote
  #3  
Old June 30th, 2008, 04:53 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 280
Thanks: 31
Thanked 8 Times in 8 Posts
Rep Power: 2
Shem is on a distinguished road
Default

exacto
Reply With Quote
  #4  
Old June 30th, 2008, 04:55 AM
richyrich's Avatar
Administrator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 876
Thanks: 46
Thanked 76 Times in 75 Posts
Blog Entries: 2
Rep Power: 5
richyrich is just really nicerichyrich is just really nicerichyrich is just really nicerichyrich is just really nice
Default

Try changing this:-
Code:
                <SelectParameters>
                    <asp:Parameter Name="id" />
                </SelectParameters>
to:-
Code:
                <SelectParameters>
                    <asp:ControlParameter Name="id" ControlID="ProjectDDL" />
                </SelectParameters>
Reply With Quote
  #5  
Old June 30th, 2008, 04:57 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 280
Thanks: 31
Thanked 8 Times in 8 Posts
Rep Power: 2
Shem is on a distinguished road
Default

tried that already no luck
Reply With Quote
  #6  
Old June 30th, 2008, 04:59 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 280
Thanks: 31
Thanked 8 Times in 8 Posts
Rep Power: 2
Shem is on a distinguished road
Default

is there a way that we can check if the ProjectDDL.SelectedValue is being
passed to ZonesDataSource?

Shem
Reply With Quote
  #7  
Old June 30th, 2008, 05:27 AM
richyrich's Avatar
Administrator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 876
Thanks: 46
Thanked 76 Times in 75 Posts
Blog Entries: 2
Rep Power: 5
richyrich is just really nicerichyrich is just really nicerichyrich is just really nicerichyrich is just really nice
Default

What actually happens when you try the change I posted?

Do you get any errors? Does the DDL just not get populated?

As far as I know that is the correct way to pass an id to datasource control. I don't use datasource controls so am guessing a little bit here.
Reply With Quote
  #8  
Old June 30th, 2008, 05:27 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 280
Thanks: 31
Thanked 8 Times in 8 Posts
Rep Power: 2
Shem is on a distinguished road
Default

did a response.write on:
Code:
Public Shared Function Zones(ByVal pid As Integer)
            strSQL = "SELECT "
            strSQL = strSQL & "idkey, "
            strSQL = strSQL & "project_id, "
            strSQL = strSQL & "userID, "
            strSQL = strSQL & "edited, "
            strSQL = strSQL & "name, "
            strSQL = strSQL & "description "
            strSQL = strSQL & "FROM tblzones "
            strSQL = strSQL & "WHERE idkey = " & pid & " "
            strSQL = strSQL & "ORDER BY name ASC"
            HttpContext.Current.Response.Write(strSQL)
            Return strSQL
        End Function
and it is getting a value..
so how come my ZonesDDL is blank

Shem
Reply With Quote
  #9  
Old June 30th, 2008, 05:38 AM
richyrich's Avatar
Administrator


 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 876
Thanks: 46
Thanked 76 Times in 75 Posts
Blog Entries: 2
Rep Power: 5
richyrich is just really nicerichyrich is just really nicerichyrich is just really nicerichyrich is just really nice
Default

My guess is an error in your GetList function. I add an error catching property to my BO classes to pass an errors to.

Try using a catch ex as exception in the try in your GetList function to catch any error that it's generating.
Should be something like
Code:
Try
.
.
.
Catch ex as Exception
templist = New ZonesList
templist.page_error = ex.tostring
finally
.
.
end try
<edit>Actually, using datasource controls, I guess you'd have to pass the error as a listitem of your dropdown.
Something like templist.add(ex.tostring)</edit>

<edit2>Actually, that wouldn't work either as the list would be expecting a Zones type, not a string...Hmmm...Tricky....Will have to have a think</edit2>

I said this ages ago, but I do sometimes find .NET a bit like using Dreamweaver, in that it puts all the code together for you and if it doesn't work as you anticipate it's a devil's own job to work out why...

Last edited by richyrich; June 30th, 2008 at 05:44 AM.
Reply With Quote
  #10  
Old June 30th, 2008, 05:38 AM
Shem's Avatar
Barn Enthusiast

 
Join Date: Mar 2008
Posts: 280
Thanks: 31
Thanked 8 Times in 8 Posts
Rep Power: 2
Shem is on a distinguished road
Default

ok it's getting values but as you can see below, the option name is blank?

Code:
<select name="ZonesDDL" onchange="javascript:setTimeout('__doPostBack(\'ZonesDDL\',\'\')', 0)" id="ZonesDDL">
	<option value="4"></option>
	<option value="5"></option>
	<option value="6"></option>

</select>
any ideas?
@RR: what do you use if ya don't use datasource control?
Reply With Quote
Reply

  DeveloperBarn Forums > Programming & Scripting > .Net Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
populate combobox from another Jaykappy Microsoft Access 4 May 7th, 2008 02:56 PM
WebCharts Control richyrich .Net Development 1 April 4th, 2008 11:14 AM


All times are GMT -4. The time now is 08:23 PM.


Copyright ©2008-2009, DeveloperBarn

Content Relevant URLs by vBSEO 3.3.0