Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 February 13th, 2004, 10:03 AM
Authorized User
 
Join Date: Feb 2004
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default Defult value to DropDownList.

Hi..
This is new problem for me.
I am populating a DropDownList dynamically , but I also asign a one defult value to the same dropdownlist. what should i have to do..
In advance thanks!
aashish

Aashish Shah
__________________
Aashish Shah
 
Old February 13th, 2004, 10:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Try this:

Dim lst As ListItem = New ListItem
lst.Text = "This Value"
lst.Value = "1"
lst.Selected = True
DropDownList1.Items.Add(lst)

J
 
Old February 13th, 2004, 11:14 AM
Authorized User
 
Join Date: Feb 2004
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this is not working if gives me error!!!!
is there any other way!!!!
thanks
aashish

Aashish Shah
 
Old February 13th, 2004, 11:29 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
Default

How are you doing it? Are you using a code-behind file? Where are you trying add your listbox? Post some code so we can help you out.

J
 
Old February 13th, 2004, 11:45 AM
Authorized User
 
Join Date: Feb 2004
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Function GetLastUpdated(strTableName)
                       Dim strSQL1 As String
                       Dim strUpdated as string


                              strSQL1 = "select to_char((max(ATTEND_DATE)),'mm/dd/yyyy')dt from ALT_ED_ATTENDANCE"

                                  Dim myConnection As OleDbConnection = New OleDbConnection("PROVIDER=MSDAORA;;Data Source=;Persist Security Info=true")
                                  Dim myCommand As OleDbCommand = New OleDbCommand(strSQL1, myConnection)


                                  Try
                                      myConnection.Open()
                                      Dim result As OleDbDataReader = myCommand.ExecuteReader()

                                      result.Read()
                                      strUpdated = result("dt")

                                        result.Close()


                                  Catch err as Exception
                                      strUpdated="Error getting date."
                                  Finally

                                  If (Not myConnection is Nothing) Then
                                       myConnection.Close()
                                  End If

                                  end try


                      return strUpdated


                  End Function

         Function GetDataSource()
                      Dim dt As New DataTable()
                      Dim dr As DataRow
                      Dim defult as String

                       Dim strSQL2 as String

                     If Not IsPostBack Then




                              strSQL2 = "select descr Reason, absence_cd code from absence_cd"


                     end if
                      Dim myConnection2 As OleDbConnection = New OleDbConnection("PROVIDER=MSDAORA.1;Password=hs;Pe rsist Security Info=true")
                      Dim myCommand As OleDbCommand = New OleDbCommand(strSQL2, myConnection2)

                        myConnection2.Open ()

                        Dim result As OleDbDataReader = myCommand.ExecuteReader

                        dt.Columns.Add(New DataColumn("Code", GetType(String)))
                        dt.Columns.Add(New DataColumn("Reason", GetType(String)))




                        Do While result.Read()

                                  dr = dt.NewRow()

                                  dr(0) = result("code")
                                  dr(1) = result("Reason")
                                   dt.Rows.Add(dr)
                         Loop

                         result.Close()

                         myConnection2.Close ()

                          Dim dv As New DataView(dt)
                          Return dv

              End Function

              Protected strCourseCode As String


              Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)


                     Dim strLastUpdated As String

                    Dim strSQL As String


                    strSQL= "select a.attend_date, a.permnum,a.lastname || ', ' || firstname student_name, 'Taft High School ' alternate_schoolnum, a.sending_schoolnum,asch.name scn from asch,alt_ed_attendance a ,(select to_char((max(ATTEND_DATE)),'mm/dd/yyyy') max_dt from alt_ed_attendance) b where asch.schoolnum = a.sending_schoolnum and a.alternate_schoolnum=410 and to_char((a.ATTEND_DATE),'mm/dd/yyyy')= max_dt"




                 Dim myConnection As OleDbConnection = New OleDbConnection("PROVIDER=MSDAORA;;Persist Security Info=true")

                  Dim myCommand2 As OleDbCommand = New OleDbCommand(strSQL, myConnection)
                              Try
                                  myConnection.Open()
                                  Dim result As OleDbDataReader = myCommand2.ExecuteReader()

                                  MyList.DataSource = result
                                  MyList.DataBind()


                                  MyList.Columns(1).ItemStyle.HorizontalAlign=Horizo ntalAlign.left
                                     MyList.Columns(2).ItemStyle.HorizontalAlign=Horizo ntalAlign.Center
                                     MyList.Columns(3).ItemStyle.HorizontalAlign=Horizo ntalAlign.Center
                                     MyList.Columns(4).ItemStyle.HorizontalAlign=Horizo ntalAlign.Center

                                  result.Close()

                             Catch err as Exception


                                  end try

                             strLastUpdated = GetLastUpdated("LOAD alt_ed_attendance")

                                  lblUpdated.Text = "DATE: " & strLastUpdated



                              If (Not myConnection is Nothing) Then

                                   myConnection.Close()

                              End If



           Dim myConnection3 As OleDbConnection = New OleDbConnection("PROVIDER=MSDAORA.1;;Persist Security Info=true")
                                  Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection3)


                                  myConnection3.Open ()





              end Sub


              Sub reason_SelectedIndexChanged(sender As Object, e As EventArgs)

               Dim strSQLreason As String


                    strSQLreason= "select absence_cd,descr,type from from absence_cd"




                 Dim myConnection As OleDbConnection = New OleDbConnection("PROVIDER=MSDAORA;;Persist Security Info=true")

                  Dim myCommand2 As OleDbCommand = New OleDbCommand(strSQLreason, myConnection)
                              Try
                                  myConnection.Open()

                              catch

                              end try
              End Sub
              '

         Sub DataGrid1_SelectedIndexChanged(sender As Object, e As EventArgs)

         End Sub
    '<asp:DropDownList id="txtReason" runat="server" width="150"></asp:DropDownList>

</script>

<html>
<head>
</head>
<body bottommargin="0" bgcolor="#fffff8" leftmargin="0" topmargin="0" scroll="yes" rightmargin="0" marginheight="0" marginwidth="0">
    <p align="center">
        <img height="77" src="cps.gif" width="420" align="center" />
    </p>
    <p>
        <br />
        <b>Alternate Education Location: </b>
    </p>
    <b>
    <p>
        <asp:Label class="Normal" id="lblUpdated" visible="true" align="right" runat="Server">Label</asp:Label>
    </p>
    <p>
    </p>
    </b>
    <table height="100%" cellspacing="0" cellpadding="0" width="800" align="center">
        <tbody>
            <form runat="server">
                <tr valign="top">
                    <td width="1500">

                        <asp:DataGrid id="MyList" runat="server" Font-Names="Verdana" GridLines="BOTH" AutoGenerateColumns="false" AlternatingItemStyle-CssClass="CartListItemAlt" ItemStyle-CssClass="CartListItem" FooterStyle-CssClass="cartlistfooter" HeaderStyle-CssClass="CartListHead" Font-Size="8pt" Font-Name="Verdana" cellspacing="0" cellpadding="4" BorderColor="black" width="100%" HorizontalAlign="Center">
                            <FooterStyle cssclass="cartlistfooter"></FooterStyle>
                            <HeaderStyle font-bold="True" cssclass="CartListHead" backcolor="#80FFFF" horizontalalign="Center"></HeaderStyle>
                            <AlternatingItemStyle cssclass="CartListItemAlt"></AlternatingItemStyle>
                            <ItemStyle font-bold="False" cssclass="CartListItem"></ItemStyle>
                            <Columns>
                                <asp:BoundColumn Visible="False" DataField="attend_date" HeaderText="Date"></asp:BoundColumn>
                                <asp:BoundColumn DataField="permnum" HeaderText="StudentID"></asp:BoundColumn>
                                <asp:BoundColumn DataField="student_name" HeaderText="Student Name"></asp:BoundColumn>
                                <asp:BoundColumn DataField="scn" HeaderText="Current School"></asp:BoundColumn>
                                <asp:TemplateColumn Headertext="Reason">
                                    <itemTemplate>
                                        <asp:DropDownList id="txtReason" runat="server" width="150" dataTextField="Reason" DataValueField="code" datasource='<%#GetDataSource()%>' ></asp:DropDownList>
                                    </itemTemplate>
                                </asp:TemplateColumn>
                            </Columns>
                        </asp:DataGrid>
                        <div align="center">
                        </div>
                        <div align="center">
                        </div>
                        <div class="Submit" id="butUpdate" title="Save" align="center">
                            <asp:Button id="Button1" runat="server" BorderColor="#00C000" BackColor="#004000" ForeColor="White" Text="Save"></asp:Button>
                        </div>
                        </td>
                </tr>
            </form>
        </tbody>
    </table>
this is entire code and it is not getting in which I am populating the the DropDownList with one defult value "Present" and rest of the data should come from the database ..

please help me out..
thanks
aashish

Aashish Shah
 
Old February 13th, 2004, 02:23 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aashish, How are you?
Here's the story: you need to feed the SelectedIndex to your datagrid. The problem is finding the index of the selected item...

Let's say you are trying to select the state that a student lives in and you have a database of states (we're just pretending). Here's what will happen:
1. You are going to feed the state you want selected to the function
2. You'll get your list of states from the database - in alpha order, of course
3. the dataset will loop through the records and compare the value you fed to the database to the value of each row
4. When it finds a value that matches, it will return a value of 'x' which has, as if by magic, been incrementing along with the rows
5. x is your index. For example, Alabama = 0, Alaska = 1...

Call this function in the selectedIndex property of your dropdownlist:
Code:
    Function findIndex(ByVal sStr As String)
        Dim x As Integer = 0
        Dim ds As New DataSet
        Dim sqlStr As String = "select YourValue from YourTable"
        Dim da As New SqlDataAdapter(sqlStr, conn) '(or whatever your connection is)
        da.Fill(ds)
        Dim dr As DataRow
        For Each dr In ds.tables(0).rows
            If sStr = dr(0) Then
                Return x
                Exit For
                x += 1
            End If
        Next
    End Function
I didn't think of this by myself, I got if from someone online. Actually, I might have got it from Scott Mitchell's ASP.Net Data Web Controls book. A most excellent read.
Let me know if this works.
mark
 
Old February 13th, 2004, 03:00 PM
Authorized User
 
Join Date: Feb 2004
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi mark thanks..
I have to fit one assigined value e.g. Paesent.
in the dynemically populated dropdownlist. I have to put first value of the droupdownlist "Present" which is not comming from database. But I want to insert my self.
this will be grate help for me.
I have tried all the things..
If you want to look at the code whole page code is posted above..
thanks
aashish

Aashish Shah
 
Old February 13th, 2004, 03:23 PM
Authorized User
 
Join Date: Jan 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey,
It looks like you could just do
Code:
dr = dt.newrow()
dr(0) = "pres"
dr(1) = "Present"
dt.rows.add(dr)
right before you do
Code:
 Do While result.Read()
dr = dt.NewRow()
dr(0) = result("code")
dr(1) = result("Reason")
dt.Rows.Add(dr)
Loop
Let me know if this works - I just did something similar.
mark

 
Old February 13th, 2004, 03:41 PM
Authorized User
 
Join Date: Feb 2004
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Mark thanks for the help..
it is working when I am Putting that inside the loop..
by the way it was nice help..
thanks
have nice weekend!
aashish

Aashish Shah
 
Old February 13th, 2004, 03:45 PM
Authorized User
 
Join Date: Feb 2004
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry Mark it is woking..
as you told me ..

if I put it inside loop it just repeate same value for whole data set.
thanks
aashish

Aashish Shah





Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlist sumith ASP.NET 1.0 and 1.1 Professional 1 February 23rd, 2007 09:11 AM
DropdownList Venkatesan ASP.NET 1.0 and 1.1 Basics 1 December 11th, 2006 11:03 AM
how to get value from dropdownlist vandat ASP.NET 1.0 and 1.1 Basics 3 November 28th, 2005 12:47 AM
DropDownList chiraagb VS.NET 2002/2003 5 June 15th, 2004 03:12 AM





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