G
Guest
Can someone tell me how I get the correct selected office in my drop down
list when I enter edit mode. At the moment it just defaults to the office at
the top of the drop down list rather than picking the office that has
actually been selected previously!
I hope this is clear...
Here is my code...
Thanks
''''''''''''''''''''''Problem - not reading the correct office when in
edit mode
Function GetSelectedIndex(ByVal PageID As String) As Integer
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim myReader As SqlDataReader = cmd.ExecuteReader()
If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If
myReader.Close()
Myconn.Close()
End Function
Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
Dim pageID = CInt(CType(e.Item.FindControl("pageID"), TextBox).Text)
'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
If e.Item.ItemType = ListItemType.EditItem Then
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim add_Office As DropDownList
'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex(pageID)
Myconn.Close()
Else
If e.Item.ItemType = ListItemType.Footer Then
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim add_Office As DropDownList
'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub
list when I enter edit mode. At the moment it just defaults to the office at
the top of the drop down list rather than picking the office that has
actually been selected previously!
I hope this is clear...
Here is my code...
Thanks
''''''''''''''''''''''Problem - not reading the correct office when in
edit mode
Function GetSelectedIndex(ByVal PageID As String) As Integer
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim myReader As SqlDataReader = cmd.ExecuteReader()
If myReader.HasRows Then
Do While myReader.Read()
Console.WriteLine(vbTab & "{0}" & vbTab & "{1}",
myReader.GetInt32(0), myReader.GetString(1))
Loop
Else
Console.WriteLine("No rows returned.")
End If
myReader.Close()
Myconn.Close()
End Function
Sub populateDDL_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
Dim pageID = CInt(CType(e.Item.FindControl("pageID"), TextBox).Text)
'Construct the Database Connection
Dim Myconn As New
SqlConnection(ConfigurationSettings.AppSettings("strConn"))
Dim cmd As New SqlCommand("SelectOffice", Myconn)
If e.Item.ItemType = ListItemType.EditItem Then
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim add_Office As DropDownList
'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLeditOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.SelectedIndex = GetSelectedIndex(pageID)
Myconn.Close()
Else
If e.Item.ItemType = ListItemType.Footer Then
cmd.CommandType = CommandType.StoredProcedure
Myconn.Open()
Dim add_Office As DropDownList
'Populates Office DropDownList with office names
add_Office = CType(e.Item.FindControl("DDLaddOffice"),
DropDownList)
add_Office.DataSource =
cmd.ExecuteReader(CommandBehavior.CloseConnection)
add_Office.DataTextField = "offName"
add_Office.DataValueField = "officeID"
add_Office.DataBind()
add_Office.Items.Insert(0, "Select One")
add_Office.Items.FindByText("Select One").Value = 0 'insert
don't create a value, but we need a value during defaults
add_Office.SelectedIndex = 0
Myconn.Close()
End If
End If
End Sub