Hello I Created an editable datagrid with 5 columns as follows:
Course ID, Department, CourseName, Component,CourseStatus
CourseID is read only
Department is read only
Coursename - in edit mode is a DropdownList
Component - in edit mode is a DropdownList
CourseStatus - in edit mode is a Dropdownlist
My problem is During database update in edit mode periodically the value in the CourseStatus dropdown list loses its value and defaults to the first item in the list. Sometimes it works fine and other times the Coursestatus value defaults to the first item.
I have a similar page with only one dropdown list and it works fine.
Is there a problem in my code and or is there something I could do differently to ensure that the Dropdownlists maintain thier values.
Code to update database
Code to popoulate Dropdownlists
Code to populate datagrid
Course ID, Department, CourseName, Component,CourseStatus
CourseID is read only
Department is read only
Coursename - in edit mode is a DropdownList
Component - in edit mode is a DropdownList
CourseStatus - in edit mode is a Dropdownlist
My problem is During database update in edit mode periodically the value in the CourseStatus dropdown list loses its value and defaults to the first item in the list. Sometimes it works fine and other times the Coursestatus value defaults to the first item.
I have a similar page with only one dropdown list and it works fine.
Is there a problem in my code and or is there something I could do differently to ensure that the Dropdownlists maintain thier values.
Code to update database
VB.NET:
Sub dgCourseComp_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles dgCourseComp.UpdateCommand
Dim oConn As New SqlConnection(MyConnection)
Dim oComm As New SqlCommand
Dim oSqlDataAdapter As New SqlDataAdapter
Dim CourseComponentIDE As Int32
Dim CourseNameE As String
Dim ComponentE As String
Dim CourseStatusE As String
'Get row iD
CourseComponentIDE = Convert.ToInt32(dgCourseComp.DataKeys(CInt(e.Item.ItemIndex)))
CourseNameE = Convert.ToString(CType(e.Item.FindControl("ddlCompCourseE"), DropDownList).SelectedItem.Value)
ComponentE = Convert.ToString(CType(e.Item.FindControl("ddlCourseCompComponentE"), DropDownList).SelectedItem.Value)
CourseStatusE = Convert.ToString(CType(e.Item.FindControl("ddlCourseCompStatusE"), DropDownList).SelectedItem.Value)
oComm.Connection = oConn
oComm.CommandType = CommandType.StoredProcedure
oComm.CommandText = "dbo.ccCourseSetup_Update"
Dim pCourseComponentID As New SqlParameter("@CourseComponentID", SqlDbType.Int)
pCourseComponentID.Direction = ParameterDirection.Input
oComm.Parameters.Add(pCourseComponentID)
pCourseComponentID.Value = CourseComponentIDE
Dim pCourseNameE As New SqlParameter("@CourseID", SqlDbType.Int)
pCourseNameE.Direction = ParameterDirection.Input
oComm.Parameters.Add(pCourseNameE)
pCourseNameE.Value = CourseNameE
Dim pComponentE As New SqlParameter("@ComponentID", SqlDbType.Int)
pComponentE.Direction = ParameterDirection.Input
oComm.Parameters.Add(pComponentE)
pComponentE.Value = ComponentE
Dim pCourseStatusE As New SqlParameter("@CourseStatusID", SqlDbType.Int)
pCourseStatusE.Direction = ParameterDirection.Input
oComm.Parameters.Add(pCourseStatusE)
pCourseStatusE.Value = CourseStatusE
Try
oConn.Open()
oComm.ExecuteNonQuery()
oComm.Connection.Close()
oConn.Close()
Catch ex As Exception
Response.Write(ex.ToString())
End Try
' Quit in-line-editing mode.
dgCourseComp.EditItemIndex = -1
'Reread expense table
readCrseComp()
'Datagrid in add mode, show footer section
dgCourseComp.ShowFooter = True
End Sub
Code to popoulate Dropdownlists
VB.NET:
Private Sub dgCourseComp_ItemDataBound(ByVal sender As Object, _
ByVal e As [URL="http://www.dotnetjunkies.com/Forums/ShowPost.aspx?PostID=11755#"]System[/URL].Web.UI.WebControls.DataGridItemEventArgs) _
Handles dgCourseComp.ItemDataBound
Dim oConn As New SqlConnection(MyConnection)
Dim oComm As New SqlCommand
Dim oSqlParameter As SqlParameter
'Create Dropdown Lists in edit mode
If e.Item.ItemType = ListItemType.EditItem Then
oComm.Connection = oConn
oComm.CommandType = CommandType.StoredProcedure
Dim ddlCompCourse As DropDownList
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim currValueCompCourseE As String
oComm.CommandText = "ccCourseList"
oConn.Open()
ddlCompCourse = CType(e.Item.FindControl("ddlCompCourseE"), DropDownList)
ddlCompCourse.DataSource = oComm.ExecuteReader (CommandBehavior.CloseConnection)
ddlCompCourse.DataBind()
currValueCompCourseE = CType(drv("CourseID"), String)
ddlCompCourse.Items.FindByValue(currValueCompCourseE).Selected = True
Dim ddlCompComponent As DropDownList
Dim currValueCompComponentE As String
oComm.CommandText = "ccComponentList"
oConn.Open()
ddlCompComponent = CType(e.Item.FindControl("ddlCourseCompComponentE"), DropDownList)
ddlCompComponent.DataSource = oComm.ExecuteReader(CommandBehavior.CloseConnection)
ddlCompComponent.DataBind()
currValueCompComponentE = CType(drv("ComponentID"), String)
ddlCompComponent.Items.FindByValue(currValueCompComponentE).Selected = True
Dim ddlCompStatusE As DropDownList
Dim currValueCompStatusE As String
oComm.CommandText = "ccCourseStatusList"
oConn.Open()
ddlCompStatusE = CType(e.Item.FindControl("ddlCourseCompStatusE"), DropDownList)
ddlCompStatusE.DataSource = oComm.ExecuteReader(CommandBehavior.CloseConnection)
ddlCompStatusE.DataBind()
currValueCompStatusE = CType(drv("CourseStatusID"), String)
ddlCompStatusE.Items.FindByValue(currValueCompStatusE).Selected = True
End If
End Sub
Code to populate datagrid
VB.NET:
Sub readCrseComp()
Dim oConn As New SqlConnection(MyConnection)
Dim oComm As New SqlCommand
Dim oSqlDataAdapter As New SqlDataAdapter
oComm.Connection = oConn
oComm.CommandType = CommandType.StoredProcedure
oComm.CommandText = "dbo.CourseCompAdmin"
Dim objParameter As New SqlParameter("@DeptID", SqlDbType.Int)
objParameter.Direction = ParameterDirection.Input
oComm.Parameters.Add(objParameter)
objParameter.Value = Me.ddlCrseCompDept.SelectedValue()
oConn.Open()
oSqlDataAdapter = New SqlDataAdapter
oSqlDataAdapter.TableMappings.Add("Table", "CourseComp")
oSqlDataAdapter.SelectCommand = oComm
dsCourseComp = New DataSet("CourseComp")
oSqlDataAdapter.Fill(dsCourseComp)
Dim dvCourseComp As DataView
dvCourseComp = New DataView(dsCourseComp.Tables("CourseComp"))
dvCourseComp.Sort = viewstate("sortexpression") & viewstate("sortdirection")
dgCourseComp.DataSource = dvCourseComp
dgCourseComp.DataBind()
oConn.Close()
End Sub
Last edited by a moderator: