Click here to Skip to main content
16,016,605 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am unable to read the GridView "DropDown" Values when i am updating the Row i.e (grdDeptEmp_RowUpdating)
I am getting one error is below.
"'cmbFrm' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value"
What is the Error. i am unable to find exact error.
pls. help

ASPX Code
----------
<asp:GridView ID="grdDeptEmp" runat="server" AutoGenerateColumns="False" DataKeyNames="emp_number"
                   class="tabulardata" width="100%" HorizontalAlign="Center" OnRowDataBound="grdDeptEmp_RowDataBound"
                   OnRowEditing="grdDeptEmp_RowEditing" AllowPaging="true" OnRowUpdating="grdDeptEmp_RowUpdating" >
                             <columns>
                               <asp:TemplateField HeaderText="Sl.No" ItemStyle-HorizontalAlign="Center"
                                        ItemStyle-Width="2%">
                                        <itemtemplate>
                                            <asp:Label ID="Sno" runat="server">
                                        </itemtemplate>
                                        <itemstyle horizontalalign="Center" />

                                <asp:TemplateField HeaderText="EmpNumber"  HeaderStyle-HorizontalAlign="Left">
                                   <edititemtemplate>
                                       <asp:Label ID="lblEmpNo" runat="server" Text='<%# Bind("emp_number") %>'>
                                   </edititemtemplate>
                                   <itemtemplate>
                                       <asp:Label ID="lblEmpNo" runat="server" Text='<%# Bind("emp_number") %>'>
                                   </itemtemplate>

                               <%-- <asp:BoundField HeaderText="Emp_Number" Visible="true" DataField="emp_number"
                                        ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="emp_number">
                                        <itemstyle horizontalalign="Center" width="5%" />
                                --%>
                               <asp:BoundField HeaderText="Name" DataField="name"
                                        ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="name">
                                        <itemstyle horizontalalign="Center" width="17%" />

                                <asp:BoundField HeaderText="Designation" DataField="emp_designation"
                                        ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="emp_designation">
                                        <itemstyle horizontalalign="Center" width="17%" />

                               <asp:TemplateField HeaderText="From Period" HeaderStyle-HorizontalAlign="Left">
                                   <edititemtemplate>
                                       <asp:DropDownList ID="cmbFrm" runat="server" DataTextField="FromPeriod" DataValueField="FromPeriod">
                                   </edititemtemplate>
                                   <itemtemplate>
                                       <asp:Label ID="lblFrm" runat="server" Text='<%# Eval("FromPeriod") %>'>
                                   </itemtemplate>

                                <asp:TemplateField HeaderText="To Period" HeaderStyle-HorizontalAlign="Left">
                                   <edititemtemplate>
                                       <asp:DropDownList ID="cmbTo" runat="server" DataTextField="ToPeriod" DataValueField="ToPeriod">
                                   </edititemtemplate>
                                   <itemtemplate>
                                       <asp:Label ID="lblTo" runat="server" Text='<%# Eval("ToPeriod") %>'>
                                   </itemtemplate>

                               <asp:TemplateField HeaderText="Edit" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
                                   <edititemtemplate>
                                       <asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Update">
                                       <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel">
                                   </edititemtemplate>
                                   <itemtemplate>
                                       <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit">
                                   </itemtemplate>

                               <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
                           </columns>

ASPX.VB Code
-------------
Protected Sub gvEngDesig_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvEngDesig.RowCreated
      If e.Row.RowType = DataControlRowType.DataRow Then
          Dim ctrl As SqlDataSource = TryCast(e.Row.FindControl("sqlDsEngDesig"), SqlDataSource)
          If ctrl IsNot Nothing AndAlso e.Row.DataItem IsNot Nothing Then
              ctrl.SelectParameters("Manpower_Division_ID").DefaultValue = DirectCast(e.Row.DataItem, DataRowView)("Manpower_Division_ID").ToString()
          End If
      End If
  End Sub
  Protected Sub grdDeptEmp_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles grdDeptEmp.RowCancelingEdit
      grdDeptEmp.EditIndex = -1
      FillGrid()
  End Sub
  Protected Sub grdDeptEmp_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdDeptEmp.RowCommand
      'Dim EmpNumber As Integer = Convert.ToString(e.CommandArgument)
  End Sub
  Protected Sub grdDeptEmp_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdDeptEmp.RowDataBound
      Dim sno As New Label
      Dim SrNo As Integer
      If e.Row.RowType = DataControlRowType.DataRow Then
          SrNo = (grdDeptEmp.PageIndex + 1) * 10 - 9
          sno = e.Row.FindControl("Sno")
          sno.Text = e.Row.RowIndex + SrNo
          sno.DataBind()
      End If
      StrQry = ""
      StrQry = "Select distinct V.emp_number as emp_number,emp_name+' '+emp_initial as name,emp_designation,Manpower_Division_ID,FromPeriod,"
      StrQry &= " ToPeriod from idpeapp.dbo.view_employee V inner join OtherDeptEmp O on V.emp_number = O.emp_number order by name"
      SQLDA = New SqlDataAdapter(StrQry, Conn)
      dttable = New DataTable
      SQLDA.Fill(dttable)
      'To Fetch the From Period
      If e.Row.RowType = DataControlRowType.DataRow Then
          Dim lblFrm As Label = DirectCast(e.Row.FindControl("lblFrm"), Label)
          If lblFrm IsNot Nothing Then
              Dim typeId As String = Convert.ToString(lblFrm.Text)
              'lblFrm.Text = DirectCast(SQLDA.GetTypeById(typeId), String)
          End If
          Dim cmbFrm As DropDownList = DirectCast(e.Row.FindControl("cmbFrm"), DropDownList)
          If cmbFrm IsNot Nothing Then
              cmbFrm.DataSource = dttable
              cmbFrm.DataTextField = "FromPeriod"
              cmbFrm.DataValueField = "FromPeriod"
              cmbFrm.DataBind()
              cmbFrm.SelectedValue = grdDeptEmp.DataKeys(e.Row.RowIndex).Values(0).ToString()
          End If
      End If
      'To Fetch the To Period
      If e.Row.RowType = DataControlRowType.DataRow Then
          Dim lblTo As Label = DirectCast(e.Row.FindControl("lblTo"), Label)
          If lblTo IsNot Nothing Then
              Dim typeId As String = Convert.ToString(lblTo.Text)
              'lblFrm.Text = DirectCast(SQLDA.GetTypeById(typeId), String)
          End If
          Dim cmbTo As DropDownList = DirectCast(e.Row.FindControl("cmbTo"), DropDownList)
          If cmbTo IsNot Nothing Then
              cmbTo.DataSource = dttable
              cmbTo.DataTextField = "ToPeriod"
              cmbTo.DataValueField = "ToPeriod"
              cmbTo.DataBind()
              cmbTo.SelectedValue = grdDeptEmp.DataKeys(e.Row.RowIndex).Values(0).ToString()
          End If
      End If
  End Sub
  Protected Sub grdDeptEmp_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdDeptEmp.RowEditing
      grdDeptEmp.EditIndex = e.NewEditIndex
      FillGrid()
  End Sub
  Public Sub FillGrid()
      Conn.Open()
      StrQry = "Select distinct V.emp_number as emp_number,emp_name+' '+emp_initial as name,emp_designation,Manpower_Division_ID,FromPeriod,"
      StrQry &= " ToPeriod from idpeapp.dbo.view_employee V inner join OtherDeptEmp O on V.emp_number = O.emp_number order by name"
      SQLDA = New SqlDataAdapter(StrQry, Conn)
      dttable = New DataTable
      SQLDA.Fill(dttable)
      If dttable.Rows.Count > 0 Then
          grdDeptEmp.DataSource = dttable
          grdDeptEmp.DataBind()
      Else
          dttable.Rows.Add(dttable.NewRow())
          grdDeptEmp.DataSource = dttable
          grdDeptEmp.DataBind()
          Dim TotalColumns As Integer = grdDeptEmp.Rows(0).Cells.Count
          grdDeptEmp.Rows(0).Cells.Clear()
          grdDeptEmp.Rows(0).Cells.Add(New TableCell())
          grdDeptEmp.Rows(0).Cells(0).ColumnSpan = TotalColumns
          grdDeptEmp.Rows(0).Cells(0).Text = "No Record Found"
      End If
      Conn.Close()
  End Sub
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not IsPostBack Then
          FillGrid()
      End If
  End Sub
  Protected Sub grdDeptEmp_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdDeptEmp.RowUpdating
      'Dim EmpNumber As Integer = Convert.ToInt32(e.CommandArgument)
      'Dim lblEmpNo As Label = DirectCast(grdDeptEmp.Rows(e.RowIndex).FindControl("lblEmpNo"), Label)
      Dim cmbFrm As DropDownList = DirectCast(grdDeptEmp.Rows(e.RowIndex).FindControl("cmbFrm"), DropDownList)
      Dim cmbTo As DropDownList = DirectCast(grdDeptEmp.Rows(e.RowIndex).FindControl("cmbTo"), DropDownList)
      Dim lblEmpNo As Label = TryCast(grdDeptEmp.Rows(e.RowIndex).FindControl("lblEmpNo"), Label)
      Dim EmpNo As String = lblEmpNo.Text
      ' you are getting Primary Key,

      'To Fetch the data based on the Emp Number and update
      StrQry = "Select distinct emp_number,FromPeriod,ToPeriod from OtherDeptEmp where emp_number= '" & lblEmpNo.Text & "' "
      SQLDA = New SqlDataAdapter(StrQry, Conn)
      Dim dsDeptEmp As System.Data.DataSet = New System.Data.DataSet
      SQLDA.Fill(dsDeptEmp)
      'dsDeptEmp.Tables("OtherDeptEmp").Rows(0)("FromPeriod") = cmbFrm
      'dsDeptEmp.Tables("OtherDeptEmp").Rows(1)("ToPeriod") = cmbTo
      'SQLDA.Update(cmbFrm.SelectedValue, cmbTo.SelectedValue, Convert.ToInt32(lblEmpNo.Text))
      'SQLDA.Update(dsDeptEmp, "OtherDeptEmp")
      Conn.Open()
      StrQry = ""
      StrQry = "update OtherDeptEmp set FromPeriod='" & cmbFrm.SelectedValue & "',ToPeriod='" & cmbTo.SelectedValue & "' where emp_number= '" & lblEmpNo.Text & "' "
      Cmd.Connection = Conn
      Cmd.CommandText = StrQry
      Cmd.ExecuteNonQuery()
      grdDeptEmp.EditIndex = -1
      FillGrid()
      'Conn.Close()
  End Sub
Posted
Updated 11-Jul-11 1:13am
v2
Comments
gani7787 11-Jul-11 8:47am    
Hi,

Still i am unable to read dropdwon values while updating.

what is the problem. pls. update immediately.

See my code.



ASPX Code
----------
<asp:GridView ID="grdDeptEmp" runat="server" AutoGenerateColumns="False"
DataKeyNames="emp_number,FromPeriod,ToPeriod" Cssclass="tabulardata" DataSourceID="dsOtherDept1"
class="tabulardata" width="100%" HorizontalAlign="Center" OnRowCancelingEdit="grdDeptEmp_RowCancelingEdit"
OnRowDataBound="grdDeptEmp_RowDataBound" OnRowEditing="grdDeptEmp_RowEditing" OnRowUpdating="grdDeptEmp_RowUpdating"
OnRowCommand="grdDeptEmp_RowCommand" ShowFooter="True"
OnRowDeleting="grdDeptEmp_RowDeleting" AllowPaging="True">
<columns>
<asp:TemplateField HeaderText="Sl.No" ItemStyle-HorizontalAlign="Center"
ItemStyle-Width="2%">
<itemtemplate>
<asp:Label ID="Sno" runat="server">

<itemstyle horizontalalign="Center">

<asp:TemplateField HeaderText="EmpNumber" HeaderStyle-HorizontalAlign="Left">
<edititemtemplate>
<asp:Label ID="lblEmpNo" runat="server" Text='<%# Bind("emp_number") %>'>

<itemtemplate>
<asp:Label ID="lblEmpNo" runat="server" Text='<%# Bind("emp_number") %>'>

<HeaderStyle HorizontalAlign="Left" />

<%-- <asp:BoundField HeaderText="Emp_Number" Visible="true" DataField="emp_number"
ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="emp_number">
<itemstyle horizontalalign="Center" width="5%">
--%>
<asp:BoundField HeaderText="Name" DataField="name"
ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="name">
<itemstyle horizontalalign="Center" width="17%">

<asp:BoundField HeaderText="Designation" DataField="emp_designation"
ItemStyle-HorizontalAlign="center" ReadOnly="true" SortExpression="emp_designation">
<itemstyle horizontalalign="Center" width="17%">

<asp:TemplateField HeaderText="From Period" HeaderStyle-HorizontalAlign="Left">
<edititemtemplate>
<asp:DropDownList ID="cmbFrom" runat="server" Style="width: 150px;"
DataSourceID="dsOtherDept1" DataTextField="FromPeriod" DataValueField="emp_number"
AppendDataBoundItems="true">
<asp:ListItem Text="Please Select" Value="">


<itemtemplate>
<asp:Label ID="lblFrm"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900