Onrowcreatedhttp://forums.asp.net/t/1480289.aspx/1?OnrowcreatedTue, 29 Dec 2009 04:41:49 -050014802893452698http://forums.asp.net/p/1480289/3452698.aspx/1?OnrowcreatedOnrowcreated <p>Hi,<br> Is there one working example for the captioned event for one gridview as I'm with the following error using the event below.<br> <em><strong>Stackoverflowexception</strong></em></p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void gv_ins(object sender, GridViewRowEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gv1.DataBind();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>I do expect to be able to create one new record there</p> 2009-10-12T02:34:28-04:003452775http://forums.asp.net/p/1480289/3452775.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>hi,</p> <p>why are calling&nbsp;&nbsp;gv1.DataBind(); in row_created event, i think it will end up indefinite recurisve calling of the row_created event,</p> <p>i.e in row_created event you are calling gv1.DataBind(); that means it will repeatedly call the row_created event without end<br> </p> 2009-10-12T03:57:10-04:003452779http://forums.asp.net/p/1480289/3452779.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p><br> Hi,<br> Seems like your code runs numerous times, and the stack keeps filling up since you are not releasing the resources.<br> <br> Since your are creating row in one fo the gridview event, can you tell me on which event you are inserting record.<br> <br> I guess&nbsp; following event gets called numerous times..<br> &nbsp;<br> &nbsp;<b> protected void gv_ins(object sender, GridViewRowEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gv1.DataBind();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</b></p> 2009-10-12T03:59:13-04:003452800http://forums.asp.net/p/1480289/3452800.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Thanks. After I've changed the event to be this, I'm still with the following error<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void gv_ins(object sender, GridViewRowEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int tot = gv1.Rows.Count;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int r = 0; r &lt; tot; r&#43;&#43;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (gv1.DataKeys[r].Values[1].ToString() == &quot;&quot;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gv1.EditIndex = r;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p><em>Argumentoutofrangeexception was unhandled by user code</em></p> 2009-10-12T04:17:51-04:003452811http://forums.asp.net/p/1480289/3452811.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>Do you have 2 DataKeyNames?</p> <p>If you only have ONE DataKeyName, then it should be <em>gv1.DataKeys[r].Values[<strong>0</strong>].ToString()</em></p> <p>Index always start from Zero.</p> <p>Seem like you are trying to do Insert using GridView.</p> <p>I would&nbsp;suggest place the new inserted row at first row.</p> <p>Hope it help</p> 2009-10-12T04:25:48-04:003452814http://forums.asp.net/p/1480289/3452814.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>to access datakey value can you try like below</p> <p><span class="" style="font-size:small; color:#1a1aff; font-family:"><span class="" style="font-size:small; color:#1a1aff; font-family:"><font color="#1a1aff" size="3"><font color="#1a1aff" size="3"></p> <p>this</font></font></p> </span><font color="#1a1aff" size="3"> <p></font></p> </span> <p><span class="" style="font-size:small; color:#ae0000; font-family:"><span class="" style="font-size:small; color:#ae0000; font-family:">.</span></span><span class="" style="font-size:small; font-family:">grvManualSchedule</span><span class="" style="font-size:small; color:#ae0000; font-family:"><span class="" style="font-size:small; color:#ae0000; font-family:">.</span></span><span class="" style="font-size:small; font-family:">DataKeys[index][</span><span class="" style="font-size:small; color:#800080; font-family:"><span class="" style="font-size:small; color:#800080; font-family:">&quot;DMPS_FD_Code&quot;</span></span><span class="" style="font-size:small; font-family:">]</span><span class="" style="font-size:small; color:#ae0000; font-family:"><span class="" style="font-size:small; color:#ae0000; font-family:">.</span></span><span class="" style="font-size:small; font-family:">ToString(); </span></p> <p><span class="" style="font-size:small; font-family:">and also check the&nbsp; highlighted index</span></p> <p><span class="" style="font-size:small; font-family:">gv1.DataKeys[r].Values[<span style="background-color:#888888">1</span>].ToString()</span></p> <p></p> 2009-10-12T04:26:45-04:003452852http://forums.asp.net/p/1480289/3452852.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Sorry for that I've posted something wrongly.</p> <p>Here is the event<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void gv_ins(object sender, GridViewRowEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int tot = gv1.Rows.Count;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int r = 0; r &lt; tot; r&#43;&#43;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> <strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (gv1.DataKeys[r].Values[0].ToString() == &quot;&quot;)<br> </strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gv1.EditIndex = r;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>but I'm with this error for line in bold above<br> <em>Argumentoutofrangeexception was unhandled by user code</em></p> 2009-10-12T04:55:52-04:003453070http://forums.asp.net/p/1480289/3453070.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Any advice?&nbsp;</p> 2009-10-12T07:01:14-04:003453127http://forums.asp.net/p/1480289/3453127.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>Do you set&nbsp;your GridView's DataKeyNames property?</p> 2009-10-12T07:31:31-04:003453137http://forums.asp.net/p/1480289/3453137.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Thanks. How to set that?&nbsp;</p> 2009-10-12T07:36:09-04:003453166http://forums.asp.net/p/1480289/3453166.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>Sample code:</p> <p><font color="#0000ff" size="2"><font color="#0000ff" size="2"></p> <p>&lt;</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">asp</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">:</font></font><font color="#a31515" size="2"><font color="#a31515" size="2">GridView</font></font><font size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">ID</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=&quot;GridView1&quot;</font></font><font size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">runat</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=&quot;server&quot;</font></font><font size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">AutoGenerateColumns</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=&quot;False&quot;</font></font><font size="2"> </font><strong><em><font color="#ff0000" size="2"><font color="#ff0000" size="2">DataKeyNames</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=&quot;au_id&quot;</font></font><font size="2"> </font></em></strong><font color="#ff0000" size="2"><font color="#ff0000" size="2">DataSourceID</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=&quot;SqlDataSource1&quot;&gt;</font></font></p> <p></p> <p>If you have composite key columns, you can set which comma, like <strong><em><font color="#ff0000" size="2"><font color="#ff0000" size="2">DataKeyNames</font></font><font color="#0000ff" size="2"><font color="#0000ff" size="2">=&quot;au_id, book_id&quot;</font></font><font size="2"> </font></em></strong></p> <p><font size="2">Hope it help</font></p> 2009-10-12T07:48:15-04:003453236http://forums.asp.net/p/1480289/3453236.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Many thanks. I've put this shown below and the key name is based on the item also shown&nbsp;below but I've got the following error (in italic)<br> &nbsp;&nbsp;&nbsp; DataKeyNames=&quot;itm_start_dt&quot; </p> <p>&nbsp;&nbsp;&nbsp; &lt;asp:TemplateField HeaderText=&quot;Start Date&quot; HeaderStyle-HorizontalAlign=&quot;Left&quot; &gt;<br> &nbsp;&nbsp;&nbsp; &lt;ItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp; &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;itm_start_dt&quot; HtmlEncode=&quot;False&quot; ForeColor=&quot;DarkBlue&quot; BackColor=&quot;AntiqueWhite&quot; BorderStyle=&quot;None&quot;<br> &nbsp;&nbsp;&nbsp; ReadOnly=&quot;true&quot; Width=&quot;65px&quot; Text='&lt;%# Eval(&quot;start_dt&quot;) %&gt;'/&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/ItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp; &lt;EditItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp; &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;edi_start_dt&quot; HtmlEncode=&quot;False&quot; <br> &nbsp;&nbsp;&nbsp; Enabled=&quot;true&quot; Width=&quot;65px&quot; Text='&lt;%# Eval(&quot;start_dt&quot;) %&gt;' /&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/EditItemTemplate&gt;<br> &nbsp;&nbsp;&nbsp; &lt;/asp:TemplateField&gt;</p> <p><em>Server Error in '/' Application.<br> --------------------------------------------------------------------------------</em></p> <p><em>DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'chk_start_dt2'. </em></p> 2009-10-12T08:19:15-04:003453277http://forums.asp.net/p/1480289/3453277.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>From the error message,&nbsp;looks like you are trying to bind the&nbsp;datasource&nbsp;of&nbsp;column named <em>chk_start_dt2 </em>which does not exists.</p> <p>Please check your SQL and also control that bind to <em>chk_start_dt2.</em></p> 2009-10-12T08:39:26-04:003453381http://forums.asp.net/p/1480289/3453381.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Many thanks. I used one existing column name from that Selectcommand and now there're no problem. But the issue is nothing will happen when I click the 'New' button which is with this event<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void gv_ins(object sender, GridViewRowEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int tot = gv1.Rows.Count;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int r = 0; r &lt; tot; r&#43;&#43;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (gv1.DataKeys[r].Values[0].ToString() == &quot;&quot;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gv1.EditIndex = r;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br> &nbsp;</p> 2009-10-12T09:18:39-04:003454804http://forums.asp.net/p/1480289/3454804.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>When you debug, will it step into <em>gv.EditIndex = r; </em>?</p> <p>So far I know, GridView has no Insert command built on it.</p> <p>I think you should use&nbsp;<font size="2">RowCommand event handler to handle your custom Insert command.</font></p> <p><font size="2">Not sure link below is what you look for<br> <a href="http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx">http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx</a></font></p> <p><font size="2">Hope it help</p> </font> <p>&nbsp;</p> 2009-10-13T01:20:58-04:003454820http://forums.asp.net/p/1480289/3454820.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Many thanks. I did click the 'New' button of the gridview to insert one new record. How to use rowcommand event handler to handle the custom insert command?&nbsp;</p> 2009-10-13T01:45:07-04:003454858http://forums.asp.net/p/1480289/3454858.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>I cut&nbsp;some code from the link I posted previously</p> <pre class="prettyprint">protected void Customers_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == &quot;InsertNew&quot;) { TextBox FirstName = Customers.FooterRow.FindControl(&quot;InsertFirstName&quot;) as TextBox; TextBox LastName = Customers.FooterRow.FindControl(&quot;InsertLastName&quot;) as TextBox; SqlParameter fName = new SqlParameter(&quot;@FirstName&quot;, SqlDbType.VarChar, 30); fName.Direction = ParameterDirection.Input; fName.Value = FirstName.Text; insertParameters.Add(fName); SqlParameter lName = new SqlParameter(&quot;@LastName&quot;, SqlDbType.VarChar, 30); lName.Direction = ParameterDirection.Input; lName.Value = LastName.Text; insertParameters.Add(lName); CustomersSqlDataSource.Insert(); } }</pre> <p>Take note on&nbsp;<em>e.CommandName == &quot;<strong>InsertNew</strong>&quot;. </em>The CommandName is what you set for the button.<br> E.g.<br> &lt;asp:Button Id=&quot;btnNew&quot; runat=&quot;server&quot; CommandName=&quot;<strong>InsertNew</strong>&quot; /&gt;</p> <p>Hope it help.</p> 2009-10-13T02:25:24-04:003454975http://forums.asp.net/p/1480289/3454975.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Many thanks Kpyap. I have changed the event to be<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected void gv_ins(object sender, GridViewCommandEventArgs e)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.CommandName == &quot;InsertNew&quot;)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ds_rec_det.Insert();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p>where ds_rec_det is the data source name of the gridview</p> <p>I've also removed this in the Markup<br> &nbsp;&nbsp;&nbsp; Onrowcreated=&quot;gv_ins&quot;</p> <p>but nothing will happen when I've clicked the 'New' button of the gridview</p> 2009-10-13T04:24:40-04:003455044http://forums.asp.net/p/1480289/3455044.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Hi,</p> <p>When debug, is the event handler been executed?</p> <p>Has you bind the event? <br> E.g.<br> &lt;asp:gridview id=<span style="color:maroon">&quot;gv&quot;</span>&nbsp;datasourceid=<span style="color:maroon">&quot;ds_rec&quot;&nbsp;</span>onrowcommand=<span style="color:maroon">&quot;gv_ins&quot;&nbsp;</span>runat=<span style="color:maroon">&quot;server&quot;</span>&gt;</p> <p>Hope it help.<br> &nbsp;</p> 2009-10-13T05:19:55-04:003455141http://forums.asp.net/p/1480289/3455141.aspx/1?Re+OnrowcreatedRe: Onrowcreated <p>Many many thanks. Yes, I've put this</p> <p>&nbsp;&nbsp;&nbsp; OnRowCommand=&quot;gv_ins&quot;</p> <p>The event was fired but the condition can't be satisfied<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (e.CommandName == &quot;InsertNew&quot;)</p> 2009-10-13T06:24:49-04:00