Link to home
Start Free TrialLog in
Avatar of narmi2
narmi2

asked on

setting dropdownlist1.SelectedIndex help

i have a webform with a dropdown

the dropdownlist1 is populated from a database e.g.

dropdownlist1.DataTextField = objDataSet.Tables(0).Columns("Name").ColumnName.ToString()
dropdownlist1.DataValueField = objDataSet.Tables(0).Columns("Code").ColumnName.ToString()

that bit works fine

next bit I cant seem to do

it should set the selectedindex of the dropdownlist1 to the correct selectedindex e.g.

dropdownlist1.SelectedIndex = objDataSet.Tables(0).Rows(0).Item("Code").ToString()

But the "Code" is not an integer, its a string

can anyone help?

Kind of urgent 500 points
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of laotzi2000
laotzi2000

Like this?

dropdownlist1.SelectedIndex = Integer.Parse(objDataSet.Tables(0).Rows(0).Item("Code").ToString())
Avatar of narmi2

ASKER

that doesnt seem to work

however the following code might be what im looking for!!!!

textbox1.Text = dropdownlist1.Items.IndexOf(dropdownlist1.Items.FindByValue(objDataSet.Tables(0).Rows(0).Item("Code").ToString()))

this gives me the correct number for the index which is should select in the dropdownlist1 but if i do this

dropdownlist1.SelectedIndex = dropdownlist1.Items.IndexOf(dropdownlist1.Items.FindByValue(objDataSet.Tables(0).Rows(0).Item("Code").ToString()))

it does not work????????
what's the error then?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of narmi2

ASKER

laotzi2000 - no errors, it just always have the index 0 selected?

b1xml2 - that does not work either.  i get the feeling that i have contradicting code somewhere.

im gonna post up the code so someone can tell me what im doing wrong. :)

thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yeah, probably that is the case...
Here's a thought,  do you have your dropdownlist binding on a post back?  Is the dropdownlist's autopostback set to true?  One of those could be the problem

For example:

Page loads - Dataset Binds
DropDownList selectedindex is changed
DropDownList hits the Autopostback
Page Loads - Dataset Binds
etc...
Avatar of YZlat
Dim item As ListItem

If Not IsDBNull(objDataSet.Tables(0).Columns("Code").ColumnName.ToString()) Then
                    For Each item In dropdownlist1.Items
                        If item.Value.IndexOf(objDataSet.Tables(0).Columns("Code").ColumnName.ToString()) <> -1 Then
                            dropdownlist1.ClearSelection()
                            dropdownlist1.Items.FindByValue(item.Value).Selected = True
                        End If
                    Next
                End If
Avatar of narmi2

ASKER

here's the code so far!

<%@ Page Language="VB" runat="server" debug="true" explicit="true" aspcompat=true smartnavigation=true validateRequest=false%>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Register TagPrefix="aspx" TagName="CM1" src="../components/CM1.ascx" %>
<%@ Register TagPrefix="aspx" TagName="CM2" src="../components/CM2.ascx" %>
<%@ Register TagPrefix="aspx" TagName="CM3" src="../components/CM3.ascx" %>

<script language="VB" runat="server">
Dim strEntryPerson as string
Dim datEntryDate as date

'## DB CONNECTION VARIABLES ##
Dim objConnection1 As SqlConnection
Dim objConnection2 As SqlConnection
Dim objSelectDataAdapter As SqlDataAdapter
Dim objDataSet As DataSet
Dim strAddEditSQL As String
Dim objAddEditCommand As SqlCommand

sub Page_Prerender(sender as Object, e as EventArgs)
      SelectedDDL()
end sub

sub Page_Load(sender as Object, e as EventArgs)
      objConnection1 = New SqlConnection("MyConnection1")
      objConnection2 = New SqlConnection("MyConnection2")

      if not page.ispostback
            GetFunction1()
            GetFunction2()
            GetFunction3()      
      end if
end sub

sub SelectedDDL()
      if ddlCM2.SelectedItem.Text = "_New Employee" then
            Response.Redirect("../default.aspx")
      else
            objSelectDataAdapter = New SqlDataAdapter("SELECT job_title, start_date, contract_full_part_time, contract_type, " & _
                                                                              "contract_signed, salary, salary_effective_date, hours_of_work, " & _
                                                                              "location_office, department " & _
                                                                          "FROM MyTable1 " & _
                                                                          "WHERE (employee_id = '" & ddlCM2.SelectedItem.Value & "') " & _
                                                                          "AND (inactive = 0)", objConnection1)

            objDataSet = new DataSet()
            objSelectDataAdapter.Fill(objDataSet, "EmergencyDetails")
            
            if objDataSet.Tables(0).Rows.Count = 0 then
                  txtJobTitle.Text                              = ""
                  txtStartDate.Text                              = ""
                  txtFullPartTime.SelectedIndex            = 0
                  txtContractType.SelectedIndex            = 0
                  txtSigned.Checked                              = false
                  txtSalary.Text                                    = ""
                  txtEffectiveDate.Text                        = ""
                  txtHoursOfWork.Text                              = ""
                  txtLocationOffice.SelectedIndex            = 0
                  txtDepartment.SelectedIndex                  = 0

                  btnSaveUpdate.text = "Save"
            else
                  txtJobTitle.Text                              = objDataSet.Tables(0).Rows(0).Item("job_title").ToString()
                  txtStartDate.Text                              = objDataSet.Tables(0).Rows(0).Item("start_date").ToString()
                  txtFullPartTime.SelectedValue            = objDataSet.Tables(0).Rows(0).Item("contract_full_part_time").ToString()
                  txtContractType.SelectedValue            = objDataSet.Tables(0).Rows(0).Item("contract_type").ToString()
                  txtSigned.Checked                              = objDataSet.Tables(0).Rows(0).Item("contract_signed").ToString()
                  txtSalary.Text                                    = objDataSet.Tables(0).Rows(0).Item("salary").ToString()
                  txtEffectiveDate.Text                        = objDataSet.Tables(0).Rows(0).Item("salary_effective_date").ToString()
                  txtHoursOfWork.Text                              = objDataSet.Tables(0).Rows(0).Item("hours_of_work").ToString()
                  txtLocationOffice.SelectedValue            = objDataSet.Tables(0).Rows(0).Item("location_office").ToString()
                  txtDepartment.SelectedValue                  = objDataSet.Tables(0).Rows(0).Item("department").ToString()
                  
                  btnSaveUpdate.text = "Update"
            end if
            
            page.databind()
      end if
      
      ddlCM2.SelectedIndex = session("sesRememberILoop")
      ddlCM1.selectedindex = session("sesRememberMainTask")
End Sub

Sub GetFunction1()
      objSelectDataAdapter = New SqlDataAdapter("SELECT PERSNL_TYP_DESC, PERSNL_TYP_CODE " & _
                                                            "FROM MyTable2 " & _
                                                            "WHERE INACTIVE = 'N'", objConnection2)
                                                            
      objDataSet = New DataSet()
      objSelectDataAdapter.Fill(objDataSet, "ContractType")

      txtContractType.DataSource = objDataSet.Tables(0)
      txtContractType.DataTextField = objDataSet.Tables(0).Columns("PERSNL_TYP_DESC").ColumnName.ToString()
      txtContractType.DataValueField = objDataSet.Tables(0).Columns("PERSNL_TYP_CODE").ColumnName.ToString()
      txtContractType.DataBind()
End Sub

Sub GetFunction3()
      objSelectDataAdapter = New SqlDataAdapter("SELECT OFFC_DESC, OFFC_CODE " & _
                                                                    "FROM hbl_office", objConnection2)
                                                            
      objDataSet = New DataSet()
      objSelectDataAdapter.Fill(objDataSet, "OfficeLocation")

      txtLocationOffice.DataSource = objDataSet.Tables(0)
      txtLocationOffice.DataTextField = objDataSet.Tables(0).Columns("OFFC_DESC").ColumnName.ToString()
      txtLocationOffice.DataValueField = objDataSet.Tables(0).Columns("OFFC_CODE").ColumnName.ToString()
      txtLocationOffice.DataBind()
End Sub

Sub GetFunction2()
      '## POPULATE DEPARTMENT
      objSelectDataAdapter = New SqlDataAdapter("SELECT DEPT_NAME,DEPT_CODE,INACTIVE FROM MyTable3 WHERE ((DEPT_CODE IN (SELECT DISTINCT DEPT FROM HBA_OFFC_DEPT WHERE OFFC = 'GX') AND INACTIVE != 'Y')) ORDER BY DEPT_CODE", objConnection2)
      objDataSet = New DataSet()
      objSelectDataAdapter.Fill(objDataSet1, "Departments")
      
      txtDepartment.DataSource = objDataSet.Tables(0)
      txtDepartment.DataTextField = objDataSet.Tables(0).Columns("DEPT_NAME").ColumnName.ToString()
      txtDepartment.DataValueField = objDataSet.Tables(0).Columns("DEPT_CODE").ColumnName.ToString()
      txtDepartment.DataBind()
End Sub

Sub SaveUpdateRecord(Source as Object, E as EventArgs)
      '## USER ##
      Dim emp_code As String = "xxx"
      
      if btnSaveUpdate.text = "Save" then
                        strAddEditSQL = "SET DATEFORMAT dmy " & _                        
                                    "INSERT INTO MyTable1 " & _
                                          "(employee_id, job_title, start_date, contract_full_part_time, contract_type, " & _
                                          "contract_signed, salary, salary_effective_date, hours_of_work, location_office, " & _
                                          "department, entry_date, entry_person) " & _
                                    "VALUES (@employee_id, @job_title, @start_date, @contract_full_part_time, @contract_type, " & _
                                          "@contract_signed, @salary, @salary_effective_date, @hours_of_work,  " & _
                                          "@location_office, @department, @entry_date, @entry_person) " & _
                                    "SET DATEFORMAT mdy"

            objAddEditCommand = new SqlCommand(strAddEditSQL, objConnection1)
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@employee_id", SqlDbType.Int, 4))
            objAddEditCommand.Parameters("@employee_id").Value = ddlCM2.selecteditem.value

            objAddEditCommand.Parameters.Add(New SqlParameter("@job_title", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@job_title").Value = txtJobTitle.text

            objAddEditCommand.Parameters.Add(New SqlParameter("@start_date", SqlDbType.DateTime, 8))
            objAddEditCommand.Parameters("@start_date").Value = txtStartDate.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@contract_full_part_time", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@contract_full_part_time").Value = txtFullPartTime.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@contract_type", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@contract_type").Value = txtContractType.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@contract_signed", SqlDbType.Int, 8))
            objAddEditCommand.Parameters("@contract_signed").Value = txtSigned.Checked
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@salary", SqlDbType.NVarChar, 50))
            objAddEditCommand.Parameters("@salary").Value = txtSalary.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@salary_effective_date", SqlDbType.DateTime, 8))
            objAddEditCommand.Parameters("@salary_effective_date").Value = txtEffectiveDate.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@hours_of_work", SqlDbType.NVarChar, 50))
            objAddEditCommand.Parameters("@hours_of_work").Value = txtHoursOfWork.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@location_office", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@location_office").Value = txtLocationOffice.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@department", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@department").Value = txtDepartment.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@entry_date", SqlDbType.DateTime, 8))
            objAddEditCommand.Parameters("@entry_date").Value = datetime.now
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@entry_person", SqlDbType.NVarChar, 50))
            objAddEditCommand.Parameters("@entry_person").Value = trim(lcase(emp_code))
            
            objAddEditCommand.Connection.Open()
                  objAddEditCommand.ExecuteNonQuery
            objAddEditCommand.Connection.Close()
      else
            strAddEditSQL = "SET DATEFORMAT dmy " & _
                                    "UPDATE MyTable1 " & _
                                    "SET employee_id                        = @employee_id, " & _
                                          "job_title                              = @job_title, " & _
                                          "start_date                              = @start_date, " & _
                                          "contract_full_part_time      = @contract_full_part_time,  " & _
                                          "contract_type                        = @contract_type, " & _
                                          "contract_signed                  = @contract_signed, " & _
                                          "salary                                    = @salary, " & _
                                          "salary_effective_date            = @salary_effective_date,  " & _
                                          "hours_of_work                        = @hours_of_work, " & _
                                          "location_office                  = @location_office, " & _
                                          "department                              = @department, " & _
                                          "entry_change_date                  = @entry_change_date, " & _
                                          "entry_change_person            = @entry_change_person " & _
                                    "WHERE (employee_id                        = @employee_id) " & _
                                    "SET DATEFORMAT mdy"

            objAddEditCommand = new SqlCommand(strAddEditSQL, objConnection1)
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@employee_id", SqlDbType.Int, 4))
            objAddEditCommand.Parameters("@employee_id").Value = ddlCM2.selecteditem.value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@job_title", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@job_title").Value = txtJobTitle.text

            objAddEditCommand.Parameters.Add(New SqlParameter("@start_date", SqlDbType.DateTime, 8))
            objAddEditCommand.Parameters("@start_date").Value = txtStartDate.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@contract_full_part_time", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@contract_full_part_time").Value = txtFullPartTime.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@contract_type", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@contract_type").Value = txtContractType.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@contract_signed", SqlDbType.Int, 8))
            objAddEditCommand.Parameters("@contract_signed").Value = txtSigned.Checked
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@salary", SqlDbType.NVarChar, 50))
            objAddEditCommand.Parameters("@salary").Value = txtSalary.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@salary_effective_date", SqlDbType.DateTime, 8))
            objAddEditCommand.Parameters("@salary_effective_date").Value = txtEffectiveDate.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@hours_of_work", SqlDbType.NVarChar, 50))
            objAddEditCommand.Parameters("@hours_of_work").Value = txtHoursOfWork.text
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@location_office", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@location_office").Value = txtLocationOffice.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@department", SqlDbType.NVarChar, 1000))
            objAddEditCommand.Parameters("@department").Value = txtDepartment.SelectedItem.Value
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@entry_change_date", SqlDbType.DateTime, 8))
            objAddEditCommand.Parameters("@entry_change_date").Value = datetime.now
            
            objAddEditCommand.Parameters.Add(New SqlParameter("@entry_change_person", SqlDbType.NVarChar, 50))
            objAddEditCommand.Parameters("@entry_change_person").Value = trim(lcase(emp_code))

            objAddEditCommand.Connection.Open()
                  objAddEditCommand.ExecuteNonQuery()
            objAddEditCommand.Connection.Close()
            
            page.databind()
      end if
      
      ddlCM2.PopulateCM2
End Sub

</script>
<html>
      <head>
            <title></title>
            <link rel="stylesheet" type="text/css" href="../stylesheets/style.css" />
      </head>

      <body>
            <form id="form1" runat="server">
                  <table class="BorderColor" border="1" bordercolor="#203A76" cellspacing="3" cellpadding="0">
                        <tr class="RowColor" valign="top">

                              <td width="150">
                                    <table class="ColumnHeadings" border="0" cellpadding="3" cellspacing="0">
                                          <tr valign="top">
                                                <td class="PersonnelHeadings">
                                                      <h4>Title 1
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td class="PersonnelSubHeading">
                                                      <p>Title 2
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td>
                                                      <aspx:CM1 id="ddlCM1" runat="server" />
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td class="PersonnelSubHeading">
                                                      <p>Title 3
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td>
                                                      <aspx:CM2 id="ddlCM2" onselectedindexchanged="WhenSelectionIsChanged" runat="server" />
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td class="PersonnelSubHeading">
                                                      <p>Title 4
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td height="100%">
                                                      <aspx:CM3 id="CM3" runat="server" />
                                                </td>
                                          </tr>
                                    </table>
                              </td>

                              <td>
                                    <table class="ColumnHeadings" border="0" cellpadding="3" cellspacing="0" bgcolor="#DFE6F1">
                                          <tr valign="top">
                                                <td class="PersonnelHeadings">
                                                      <h4>Title 5
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td class="PersonnelSubHeading">
                                                      <p>&nbsp;
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td height="100%">
                                                      <table border="0">
                                                            <tr>
                                                                  <td colspan="2" class="SubCatFadeBlue">
                                                                        <b>Job Details</b>
                                                                  </td>
                                                                  <td>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Job Title:
                                                                  </td>
                                                                  <td>
                                                                        <asp:textbox id="txtJobTitle" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Start Date:
                                                                  </td>
                                                                  <td>
                                                                        <asp:textbox id="txtStartDate" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td colspan="2" class="SubCatFadeBlue">
                                                                        <b>Contract</b>
                                                                  </td>
                                                                  <td>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Full/Part Time:
                                                                  </td>
                                                                  <td>
                                                                        <asp:dropdownlist id="txtFullPartTime" font-size="8" runat="server" width="250">
                                                                              <asp:listItem Value="0">Full Time</asp:listitem>
                                                                              <asp:listItem Value="1">Part Time</asp:listitem>
                                                                        </asp:dropdownlist>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Contract Type:
                                                                  </td>
                                                                  <td>
                                                                        <asp:dropdownlist id="txtContractType" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Signed:
                                                                  </td>
                                                                  <td>
                                                                        <asp:checkbox id="txtSigned" font-size="8" runat="server" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td colspan="2" class="SubCatFadeBlue">
                                                                        <b>Salary</b>
                                                                  </td>
                                                                  <td>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Salary:
                                                                  </td>
                                                                  <td>
                                                                        <asp:textbox id="txtSalary" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Effective Date:
                                                                  </td>
                                                                  <td>
                                                                        <asp:textbox id="txtEffectiveDate" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td colspan="2" class="SubCatFadeBlue">
                                                                        <b>Other</b>
                                                                  </td>
                                                                  <td>
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Hours Of Work:
                                                                  </td>
                                                                  <td>
                                                                        <asp:textbox id="txtHoursOfWork" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Location Office:
                                                                  </td>
                                                                  <td>
                                                                        <asp:dropdownlist id="txtLocationOffice" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>
                                                            <tr>
                                                                  <td>
                                                                        Department:
                                                                  </td>
                                                                  <td>
                                                                        <asp:dropdownlist id="txtDepartment" font-size="8" runat="server" width="250" />
                                                                  </td>
                                                            </tr>

                                                      </table>
                                                </td>
                                          </tr>
                                          <tr valign="top">
                                                <td class="PersonnelFooter">
                                                      <asp:button id="btnSaveUpdate" text="Save" font-size="8" OnClick="SaveUpdateRecord" runat="server" />
                                                </td>
                                          </tr>
                                    </table>
                              </td>
                        </tr>
                  </table>
            </form>
      </body
</html>
Avatar of narmi2

ASKER

making progress here!

it now works when i click the update or save button. it adds the correct entries into the database and even remembers the correct item in the dropdown

however, if i click on another page and then go back the this page, it does not remember the correct dropdownitem???
That's normal, when you go back from another page, it is not postback,
so the dropdownlist will be rebind, the selected item is lost.

To remember this, you can consider using a session variable.
Avatar of narmi2

ASKER

I got it working!!!!!!!!!!

I simply removed the following from each of the Functions

txtLocationOffice.DataBind()

so the only time it binds is in the SelectedDLL sub!!!!!

it was binding too many times, once in the SelectedDLL sub and once in each of the functions!!!!

cant believe i didnt see that!!!