• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 233
  • Last Modified:

URGENT: Problem assigning Datagrid value to form field problem: ArgumentOutOfRangeException

Hi, everyone,

I am using C# in my web app.

I am having a problem assigning the value of PlayerUnit field from a selected record in my datagrid to a form field.  This error only happens when the value in the datagrid cell is null, and it only happens on this one field.  I have looked for an embedded space in the record, but cannot find any.

This is the error I am getting:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name:  


This is my datagrid on the form:

<asp:datagrid id="dgUsers" runat="server"  OnSortCommand="SortColumn"                                                      AutoGenerateColumns="False" CellSpacing="2" CellPadding="3" AllowSorting="True">
<Columns>
      <asp:BoundColumn Visible="False" DataField="UserNumber"></asp:BoundColumn>
      <asp:TemplateColumn>
            <ItemTemplate>
                  <asp:Button id="CmdSelect" Text="Select" Runat="server" CommandName="IRecord"></asp:Button></ItemTemplate>
            </asp:TemplateColumn>
      <asp:BoundColumn DataField="Name" SortExpression="Name" HeaderText="Name"></asp:BoundColumn>
      <asp:BoundColumn DataField="Role" SortExpression="Role" HeaderText="Role" FooterText="Role"></asp:BoundColumn>
      <asp:BoundColumn DataField="UserDesc" SortExpression="UserDesc" HeaderText="User<br>Description" FooterText="User<br>Description"></asp:BoundColumn>
      <asp:BoundColumn DataField="PlayerUnit" SortExpression="PlayerUnit" HeaderText="Player Unit" FooterText="Player Unit"></asp:BoundColumn>
</Columns>
</asp:datagrid>

This is where I am filling the form field with the value from the datagrid cell:

PlayerUnit.SelectedValue=e.Item.Cells[5].Text;

This is my form field:

<TR><TD>Player Unit:</TD>
        <TD><asp:DropDownList id="PlayerUnit" runat="server" Width="250px">
      <asp:ListItem></asp:ListItem>
      <asp:ListItem Value="Port MCT">Port MCT</asp:ListItem>
      <asp:ListItem Value="Origin MCT">Origin MCT</asp:ListItem>
      </asp:DropDownList></TD></TR>

ptslv
0
ptslv
Asked:
ptslv
  • 3
  • 2
1 Solution
 
Raju SrivatsavayeSoftware EngineerCommented:
Does the text in the datagrid cell match that of the value in dropdown list??
0
 
Sam_JayanderCommented:
Hi,

can you specify in what event, you are calling the line,
   PlayerUnit.SelectedValue=e.Item.Cells[5].Text;

Is it placed in something like ItemCommand??

what does "e" refers to in this line??

--Sam.
0
 
Raju SrivatsavayeSoftware EngineerCommented:
Try this:

<TR><TD>Player Unit:</TD>
        <TD><asp:DropDownList id="PlayerUnit" runat="server" Width="250px">
     <asp:ListItem></asp:ListItem>       <----------------------------------------------------------------Take this out and try..
     <asp:ListItem Value="Port MCT">Port MCT</asp:ListItem>
     <asp:ListItem Value="Origin MCT">Origin MCT</asp:ListItem>
     </asp:DropDownList></TD></TR>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ptslvAuthor Commented:
Thanks for the quick responses.  I will answer them in order:

srivatsavaye - yes, the text matches the value in the dropdown.  As I said, it only happens with that field and only when there is a null value in that field in the record.  Also, If I remove the line you have marked, I get the same error.

Sam - here's my function to load the individual record into the form for editing:

private void LoadIndividualRecord(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
      if(e.CommandName=="IRecord")
      {
          AutoID = Convert.ToInt16(e.Item.Cells[0].Text);
          lblName.Text=e.Item.Cells[2].Text;
          Role.SelectedValue=e.Item.Cells[3].Text;
          UserDesc.SelectedValue=e.Item.Cells[4].Text;
          PlayerUnit.SelectedValue=e.Item.Cells[5].Text;
              }
               ....... do more stuff
}


The Parameter name that is pulling in is: &nbsp;

0
 
ptslvAuthor Commented:
I created a workaround by placing a default field value in the table of "N/A".  This does not throw an error.  Meanwhile, I would still like  to have a proper fix for this problem.  Apparently, all my select fields have default values.  

The problem I would like to fix is this:

Why can I not assign a datagrid cell value to a dropdown field in a form when the value of the field in the datagrid cell is null?

Thanks for the help.

ptslv
0
 
Raju SrivatsavayeSoftware EngineerCommented:
one solution for this would be:

if (e.Item.Cells[5].Text!=null)
 PlayerUnit.SelectedValue=e.Item.Cells[5].Text;

That might do the trick.. You cannot match a null value to dropdown list..
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now