Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to set dropdownlist to a vaule from a sqldatasource

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I am teaching myself asp.net 2.0 using VB.NET 2005.
I want a dropdown to be set to the default week start date for the week when the web page is loaded. I have a dropdown list that has all the weeks for the year. Populated by one datasource. I have two data sources, the other has the current week starting date, which for this week is 7/25/09 Saturday. how do I set the dropdown list to default to the sql datasources sqlTmShtCurrentWeekEndDate WeekStartDate column?
Code:
    <form id="form1" runat="server">
             <asp:SqlDataSource
                id="sqlTmShtCurrentWeekStartDate" runat="server" ConnectionString="<%$ ConnectionStrings:webdb1ConnectionString %>"
                SelectCommand="SELECT Top 1 WeekStartDate, WeekEndDate, WeekNumber, PayPeriodDate, PayPeriod FROM [Timesheet PayPeriods] WHERE (WeekEndDate >= getdate())">
             </asp:SqlDataSource>
        
            <asp:SqlDataSource
                id="sqlTmShtAllPayPeriods" runat="server" ConnectionString="<%$ ConnectionStrings:webdb1ConnectionString %>"
                SelectCommand="SELECT WeekStartDate, WeekEndDate, WeekNumber, PayPeriodDate, PayPeriod FROM [Timesheet PayPeriods]">
             </asp:SqlDataSource>
                 
  
          
           <asp:DropDownList
                id="DropDownList2"
                runat="server"
                DataTextField="WeekStartDate"
                DataSourceID="sqlTmShtAllPayPeriods" style="z-index: 101; left: 384px; position: absolute; top: 48px" AutoPostBack="True" 
                DataTextFormatString= ???  <<<<<< this to format as 7/25/09 currently its 7/25/2009 12:00:00 AM
                DataValueField= ?? <<<<< and this to be on 7/25/09
                />
  

        <asp:Label ID="lblWeekEndDate" runat="server" Style="z-index: 103; left: 232px; position: absolute;
            top: 48px" Text="Choose Week Start Date"></asp:Label>

    </form>


DougP
[r2d2] < I Built one
 
why do not you avoid using sqldatasource. Eventually you will end up with a bid headace. here is a sample that might help you to start with
Code:
 Protected Sub btnCallFinder_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCallFinder.Click

        Dim connectionString As String = ConnectionStrings("costEstimating").ConnectionString
        Dim oOracleConn As OracleConnection = New OracleConnection(connectionString)

        Dim strStringBuilder As StringBuilder
        strStringBuilder = New StringBuilder

        With strStringBuilder
            .Append("  SELECT substr(letting,3,2)||'/'||substr(letting, 5,2)||'/'||substr(letting,1,2)  ""Letting Date"", ")
            .Append("  l.letting ""Letting Id"", CALL ""Call Number"", contid ""Contract Id"", ")
            .Append("  initcap(cdescr) ""Job Description"", initcap(clocat1) ""Location""")
            .Append("  FROM letprop l, proposal p")
            .Append("  WHERE l.lcontid = p.contid AND cprojnum  like :SPNUMBER ")
            .Append("  AND SUBSTR(L.LCONTID,4,4) < '5' ")
        End With


        Dim cmdGetSP As OracleCommand = New OracleCommand()
        cmdGetSP.Parameters.AddWithValue(":SPNUMBER", txtSp.Text & "%")
        cmdGetSP.Connection = oOracleConn
        cmdGetSP.CommandType = CommandType.Text
        cmdGetSP.CommandText = strStringBuilder.ToString

        Dim adGetSP As New OracleDataAdapter(cmdGetSP)
        Dim GetCallContractNumber As New DataSet
        adGetSP.Fill(GetCallContractNumber, "GetCallNumber")
        gvContract.DataSource = GetCallContractNumber
        gvContract.DataBind()

        If gvContract.Rows.Count = 0 Then
            lblResult.Visible = True
            gvContract.Visible = False
        Else
            lblResult.Visible = False
            gvContract.Visible = True
        End If


    End Sub
 
Hi,
AFAIK, his code does not require an <ASP: tag becaues it is not a control but a subroutine.



[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
The <ASP: is a declaration of a .net control. The example taree posted is actual VB code which goes in the code behind page.
 
Code:
Here is the asp: part.....it is not complete but It shoud give you a little idea how to bid data

 <table style="width: 40%">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Enter S.P. Number" Width="189px" Font-Size="Small"></asp:Label></td>
                <td >
                </td>
                <td >
                </td>
            </tr>
            <tr>
                <td >
                    <asp:TextBox ID="txtSp" runat="server" Width="201px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtSp"
                        ErrorMessage="Please enter State Project number">*</asp:RequiredFieldValidator></td>
                <td >
                </td>
                <td >
                </td>
            </tr>
            <tr>
                <td >
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" />
                </td>
                <td >
                </td>
                <td >
                </td>
            </tr>
            <tr>
                <td >
                    <asp:Button ID="btnCallFinder" runat="server" Text="Get Call and Contract Id" /></td>
                <td >
                </td>
                <td >
                </td>
            </tr>
        </table>
    </div>
    <table style="width: 100%">
        <tr>
            <td style="height: 21px" colspan="2" >
                <asp:Label ID="lblResult" runat="server" Text="No row returned.Please enter a valid S.P. Number."
                    Visible="False" Width="617px" Font-Italic="True" ForeColor="Red"></asp:Label></td>
        </tr>
        <tr>
            <td colspan="2" >
                <asp:GridView ID="gvContract" SkinID="gridviewnopagingsorting" runat="server" AutoGenerateColumns="False">
                    <Columns>
                    <asp:BoundField DataField="Letting Id" HeaderText="Letting Id" />
                        <asp:BoundField DataField="Contract Id" HeaderText="Contract Id" />
                        <asp:BoundField DataField="Call Number" HeaderText="Call Number" />
                        <asp:BoundField DataField="Letting Date" HeaderText="Letting Date" />
                        <asp:BoundField DataField="Job Description" HeaderText="Job Description" />
                        <asp:BoundField DataField="Location" HeaderText="Location" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>
 
Ok back to the Original question: how do you set a dropdown list to some value?

in the dropdown list If I dates
1/1/09
1/15/09
1/29/09
etc
how do I set it to
7/29/09 whih is today?




DougP
[r2d2] < I Built one
 
Did you look in the VS help files? There are several ways:
ddl.selectedindex = some int
you can loop though the values and set a listitem.selected = true
In your case you want FindByValue or FindByText

ddl.Items.FindByValue("some value").Selected = True
 
Code:
Just create a procedure like and in you page load event do like this. I know it is not exactly what you asked for but it should be good enough for you to get going.

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
  
            FedProjYear()
          End If
  
        
  End Sub

 Sub FedProjYear()
        Dim intYear As Integer
        For intYear = DateTime.Now.Year To DateTime.Now.Year + 9
            ddlLettYear.Items.Add(intYear)
        Next
        ddlLettYear.Items.Insert(0, New System.Web.UI.WebControls.ListItem("--Year--", 0))
        ddlLettYear.Items.FindByValue(0).Selected = True

    End Sub
 
Ok getting closer now
here is my code
Code:
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            DropDownList1.Equals("07/24/2009")
            DropDownList1.Items.FindByValue("07/24/2009").Selected = True
            DropDownList1.Items.Item(12).Selected = True
        End If
    End Sub
    
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Me.TextBox1.Text = Me.DropDownList1.Text
        End If
    End Sub
    
    Protected Sub DropDownList1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not IsPostBack Then
            Me.TextBox1.Text = Me.DropDownList1.Text
        End If

    End Sub
    
    </script>
none of the above code does anything, so what am I missing now? in the page load this does nothing at all > DropDownList1.Equals("07/24/2009")
the other two give errors one of which is attached

DougP
[r2d2] < I Built one
www.robots2000.com
 
 http://www.pcsupportguru.com/error.jpg
DropDownList1.Equals("07/24/2009") will do nothing, it is a comparison operator, not an assignment operator.

You have to first bind data to your ddl before you can find a value in it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top