Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

Iam creating dynamic textbox using grid
In my datatable i have below three columns and have three text boxes by name
desctb,itemNotb,qtytb

for the qtytb i am getting error us Specified argument was out of the range of valid values i have attached stack trace also

could you please chk

Thanks



DataTable dt = new DataTable();
        
        DataRow dr = null;    
        dt.Columns.Add(new DataColumn("ItemNo", typeof(System.Int32)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Qty", typeof(System.Int32)));
dr = dt.NewRow();

        dr["ItemNo"] = DBNull.Value;
        dr["Description"] = string.Empty;
        dr["Qty"] = DBNull.Value;  


this is the textbox declaration

TextBox box1 = (TextBox)ItemGv.Rows[rowIndex].Cells[1].FindControl("itemNotb");

                    TextBox box2 = (TextBox)ItemGv.Rows[rowIndex].Cells[2].FindControl("desctb");
                    TextBox box3 = (TextBox)ItemGv.Rows[rowIndex].Cells[3].FindControl("qtytb");
drCurrentRow = dtCurrentTable.NewRow();

                    drCurrentRow["ItemNo"] = box1.Text;
                    drCurrentRow["Description"] = box2.Text;
                    drCurrentRow["Qty"] = box3.Text;




System.ArgumentOutOfRangeException was unhandled by user code
Message="Specified argument was out of the range of valid values.\r\nParameter name: index"
Source="System.Web"
ParamName="index"
StackTrace:
at System.Web.UI.ControlCollection.get_Item(Int32 index)
at System.Web.UI.WebControls.TableCellCollection.get_Item(Int32 index)
at Admin_Pages_DO_Management_EditDo.AddNewRowToGrid() in c:\Inetpub\wwwroot\Invoice\Admin_Pages\DO_Management\EditDo.aspx.cs:line 84
at Admin_Pages_DO_Management_EditDo.ButtonAdd_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\Invoice\Admin_Pages\DO_Management\EditDo.aspx.cs:line 160
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
Posted
Comments
Timberbird 14-Nov-11 3:32am    
Are you sure Cells collection itemas are numbered correctly? Collections usually number from 0, not 1. Try changing indexes, replacing
TextBox box3 = (TextBox)ItemGv.Rows[rowIndex].Cells[3].FindControl("qtytb");
with
TextBox box3 = (TextBox)ItemGv.Rows[rowIndex].Cells[2].FindControl("qtytb");

(and in previous lines as well)
[no name] 14-Nov-11 3:32am    
Have you debugged your code?

Check if you have valid rowIndex value. And your grid contains atleast 4 columns (as you have accessed cells[3]).
 
Share this answer
 
Comments
Lancy.net 14-Nov-11 3:52am    
Thanks Prerak,Timber my first cell is BoundField and done some changes bit confused bacause of that thanks...
Prerak Patel 14-Nov-11 4:08am    
You are welcome. If your query is solved, mark the answer.
I suspec that it is because you have forgotten that C# arrays are zero based, not one.
So a three element array would have the elements:
VB
myArray[0]
myArray[1]
myArray[2]
Rather than:
VB
myArray[1]
myArray[2]
myArray[3]
Change your numbers!
 
Share this answer
 

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