stephen_rota
asked on
trying to avoid System.ArgumentOutOfRangeException dropdown list in a datagrid
hello dear experts,
i have a problem with a dropdown list in a datagrid, basically i am trying to avoid the:
System.ArgumentOutOfRangeE xception: 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(DataBinde r.Eval(Con tainer, "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.ConnectionS tring = dbConn;
dataCommand.Connection = dataConnection;
dataCommand.CommandText = "GetMaterials";
dataCommand.CommandType = System.Data.CommandType.St oredProced ure;
SqlDataAdapter MaterialDA = new SqlDataAdapter(dataCommand );
MaterialDA.Fill(DS,"Materi al");
MaterialDT = DS.Tables["Material"];
SqlParameter reportIDprm;
reportIDprm= new SqlParameter("@reportID",1 );
dataCommand.Parameters.Add (reportIDp rm);
dataCommand.CommandText = "GetBOM";
dataCommand.CommandType = System.Data.CommandType.St oredProced ure;
BOMDA = new SqlDataAdapter(dataCommand );
BOMDA.Fill(DS,"BOM");
BOMDataGrid.DataSource = DS.Tables["BOM"];
BOMDataGrid.DataBind(); <-----------------------er ror here
/*
}
catch(ArgumentOutOfRangeEx ception 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.
i have a problem with a dropdown list in a datagrid, basically i am trying to avoid the:
System.ArgumentOutOfRangeE
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
......
<asp:DropDownList id="MaterialDDL" runat="server" DataSource='<%# MaterialDT%>' DataTextField="Material" DataValueField="ID" SelectedValue='<%# GetSelectedValue(DataBinde
</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.ConnectionS
dataCommand.Connection = dataConnection;
dataCommand.CommandText = "GetMaterials";
dataCommand.CommandType = System.Data.CommandType.St
SqlDataAdapter MaterialDA = new SqlDataAdapter(dataCommand
MaterialDA.Fill(DS,"Materi
MaterialDT = DS.Tables["Material"];
SqlParameter reportIDprm;
reportIDprm= new SqlParameter("@reportID",1
dataCommand.Parameters.Add
dataCommand.CommandText = "GetBOM";
dataCommand.CommandType = System.Data.CommandType.St
BOMDA = new SqlDataAdapter(dataCommand
BOMDA.Fill(DS,"BOM");
BOMDataGrid.DataSource = DS.Tables["BOM"];
BOMDataGrid.DataBind(); <-----------------------er
/*
}
catch(ArgumentOutOfRangeEx
{
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER