Solved

trying to avoid System.ArgumentOutOfRangeException dropdown list in a datagrid

Posted on 2006-06-22
3
873 Views
Last Modified: 2007-12-19
hello dear experts,
i have  a problem with a dropdown list in a datagrid, basically i am trying to avoid the:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value
Source Error:
Line 67:                         
Line 68:                         BOMDataGrid.DataSource = DS.Tables["BOM"];
Line 69:                         BOMDataGrid.DataBind();      ----> marked red

after readings some replies here i added the GetSelectedValue function.

<asp:datagrid id="BOMDataGrid" runat="server" AutoGenerateColumns="False" OnEditCommand="Edit"             OnCancelCommand="Cancel" OnUpdateCommand="Update" OnDeleteCommand="Delete">
......
         <asp:DropDownList id="MaterialDDL" runat="server" DataSource='<%# MaterialDT%>' DataTextField="Material" DataValueField="ID" SelectedValue='<%# GetSelectedValue(DataBinder.Eval(Container, "DataItem.MaterialID"))%>'>
         </asp:DropDownList>

public String GetSelectedValue(object selVal)
{      
     if (selVal.Equals("") || selVal==null)
         return null;
     else
         return selVal.ToString();                              
}

i have a:
public DataTable MaterialDT; in the .cs

and am calling GetData() to populate and bind the datagrid and to poulate the datatable for the dropdownlist:MaterialDT.


            void GetData()
            {                  
                        SqlCommand dataCommand = new SqlCommand();
                        SqlConnection dataConnection = new SqlConnection();
                        dataConnection.ConnectionString = dbConn;
                        dataCommand.Connection = dataConnection;                  
                        
                        dataCommand.CommandText = "GetMaterials";
                        dataCommand.CommandType = System.Data.CommandType.StoredProcedure;      
                  
                        SqlDataAdapter MaterialDA = new SqlDataAdapter(dataCommand);
                        MaterialDA.Fill(DS,"Material");

                        MaterialDT = DS.Tables["Material"];
                        
                        SqlParameter reportIDprm;
                        reportIDprm= new SqlParameter("@reportID",1);                  
                        dataCommand.Parameters.Add(reportIDprm);

                        dataCommand.CommandText = "GetBOM";
                        dataCommand.CommandType = System.Data.CommandType.StoredProcedure;      

                        BOMDA = new SqlDataAdapter(dataCommand);
                        BOMDA.Fill(DS,"BOM");
                        
                        BOMDataGrid.DataSource = DS.Tables["BOM"];
                        BOMDataGrid.DataBind();      <-----------------------error here                  
                  /*
                  }
                  catch(ArgumentOutOfRangeException OutOfRange)
                  {
                        Label1.Text = OutOfRange.Message;
                  }
                  catch(Exception e)
                  {
                        Label1.Text = e.Message;
                  }
                                         */
            }
but am still getting the error on pressing the edit button in the datagrid on items that were added with the add material button:
which only has this at its end:

BOMDataGrid.EditItemIndex = -1;
GetData();      

hope you might help, 10x in advance.


 

0
Comment
Question by:stephen_rota
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
GENTP earned 500 total points
ID: 16960318
Have you tried changing from seting the edititemindex from -1 to setting it to 0?
0
 

Author Comment

by:stephen_rota
ID: 16962286
no i did not try that one.  My first impression was that the datagrid (i.e. due to the editItemIndex as you are noting) was causing the outofrange exception. but then on following some posts on your site I got the impression (and continued to convince myself of this after some testing) that the dropdown list was causing that excepiton since there were no itmes selected (something on the same argument as what happens with the HTML select or raedio buttons i.e. there is no value for the dropdown unless anything is selected) hence I introduced the GetSelectedValue function. However I will try setting edititemindex to 0 tomorrow morniing and reply with the outcome. -1 resets the datagrid to a no edit mode right? 10x.
0
 

Author Comment

by:stephen_rota
ID: 17051139
the prob was when have an unselected dropdown. solved this by defaulting the dropdown list to a selection of none. i am getting the value, text from a DB so i just added an entry of id: -1, desc: none. 10x.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 103
Why is enum singleton a better approach than static factory 3 67
Adding a countdown to HTA 12 116
Survey branching tutorial 11 78
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question