Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I am stumped but it seems so easy.

I have a Gridview of 11 columns. Column 8 is the date of when the record was added. This date is stored as a DateTime in an SQL Server table.

On the screen there is an edit button that takes the user to an update screen using a RowEditing event in the C# code behind.

What I want to do is compare this Add date to 08/01/2012. Any record added before this date brings up one update screen and every record added after this date brings up a different update screen.

The RowEditing event uses UrlParameterPasser to pass the record ID to the update program.

One thing I do not know how to do is to show the data in the cell (column 8) selected. I get an error that the string is not recognized as a valid DateTime and am not sure how to display what the program sees.

I am also not sure if the condition would work if I could get around the error.

Any help will be much appreciated.

Here is some of my code:

ASPX

ASP.NET
<asp:BoundField DataField="WelfareNumber" HeaderText="Welfare#" SortExpression="WelfareNumber" ItemStyle-Wrap="false" />
<asp:BoundField DataField="CPLastName" HeaderText="CP Last Name" SortExpression="CPLastName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="CPFirstName" HeaderText="First Name" SortExpression="CPFirstName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="CPParticipantID" HeaderText="CP Par ID" SortExpression="CPParticipantID" ItemStyle-Wrap="false" />
<asp:BoundField DataField="NPLastName" HeaderText="NP Last Name" SortExpression="NPLastName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="NPFirstName" HeaderText="First Name" SortExpression="NPFirstName" ItemStyle-Wrap="false" />
<asp:BoundField DataField="NPParticipantID" HeaderText="NP Par ID" SortExpression="NPParticipantID" ItemStyle-Wrap="false" />
<asp:BoundField DataField="Added" HeaderText="Added" SortExpression="Added" ItemStyle-Wrap="false" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" ItemStyle-ForeColor="LightSlateGray"/>
<asp:BoundField DataField="AddedBy" HeaderText="Added By" SortExpression="AddedBy" ItemStyle-Wrap="false" ItemStyle-ForeColor="LightSlateGray"/>
<asp:BoundField DataField="Updated" HeaderText="Updated" SortExpression="Updated" ItemStyle-Wrap="false" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False" ItemStyle-ForeColor="LightSlateGray"/>
<asp:BoundField DataField="UpdatedBy" HeaderText="Updated By" SortExpression="UpdatedBy" ItemStyle-Wrap="false" ItemStyle-ForeColor="LightSlateGray" />


ASPX.CS

C#
protected void gvCAD_RowEditing(object sender, GridViewEditEventArgs e)
   {
       // Pass textbox values to ReceiveQueryString.aspx
       string strWhereToGo;
       string strAdded = gvCADs.Rows[e.NewEditIndex].Cells[8].Text;
       DateTime strAdded1;
       strAdded1 = DateTime.Parse(strAdded);
       strAdded = strAdded1.ToString("MM/dd/yyyy");
       if (Convert.ToDateTime(strAdded) > Convert.ToDateTime("07/30/2012"))
       {
           strWhereToGo = "~/CallCenter/UpdateCAD_2012.aspx";
           //UrlParameterPasser urlWrapper = new UrlParameterPasser("~/CallCenter/UpdateCAD_2012.aspx");
       }
       else
       {
           strWhereToGo = "~/CallCenter/UpdateCAD.aspx";
           //UrlParameterPasser urlWrapper = new UrlParameterPasser("~/CallCenter/UpdateCAD.aspx");
       }
       //UrlParameterPasser urlWrapper = new UrlParameterPasser("~/CallCenter/UpdateCAD.aspx");
       UrlParameterPasser urlWrapper = new UrlParameterPasser(strWhereToGo);
       // Add some values
       string CAD = ((Label)gvCADs.Rows[e.NewEditIndex].FindControl("itCPCADID")).Text;
       urlWrapper["CPCADID"] = CAD;
       //urlWrapper["age"] = age.Text;

       // Redirect to the page, passing the parameters in the querystring
       urlWrapper.PassParameters();
   }
Posted
Comments
[no name] 23-Aug-12 15:30pm    
What is the value of gvCADs.Rows[e.NewEditIndex].Cells[8].Text at the time you get this error?
JTRizos 23-Aug-12 18:15pm    
I wish I could tell. For the record I selected, it should be 08/22/2012. If this were true, I should not get the error message and my logic should diret the user to the new update screen "~/CallCenter/UpdateCAD_2012.aspx".

Thanx for your question.
[no name] 23-Aug-12 18:43pm    
What do you mean "I wish I could tell"? You did not run your project under the debugger?
JTRizos 23-Aug-12 18:48pm    
I am using Visual Studio 2005 and I run the code using Debug but it kicks out. The application keeps running but the debugger quits. I have read there was a problem with the debugger doing this.

I am curious ... am I on the right track and should this work as I have it coded? Thanx again!!
Christian Graus 23-Aug-12 18:50pm    
Why not use Console.WriteLine to emit your value to the output window, if the debugger is broken. And no, this is not normal.

I feel rowediting event is the problem, because when you click Edit button RowEdit event occurs but this occurs before Gridview control get into edit mode.

Normally we use this event as a precaution/prevention. So that you can cancel editing.

The very usual code that we write in RowEdit is to set EditIndex:
gvCAD.EditIndex = e.NewEditIndex;

And EditCacel event as:
C#
protected void gvCAD_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    //Reset the edit index.
    gvCAD.EditIndex = -1;

}



I propose you go with gvCAD_RowUpdating event and try.

Thanks,

Kuthuparakkal
 
Share this answer
 
Comments
JTRizos 24-Aug-12 17:03pm    
Thanx Kuthuparakkal, I did get around the problem I had with the rowediting event. It now goes to the right update program if the added date is before 08/01/2012 but goes to the login screen if the date is after 08/01/2012. A step closer but I still need to determine why it does what it does. Maybe a typo somewhere.
Thanx again for your response, much appreciated.
I used VS2008 instead of VS2005 which is what I normally use due to the Report Services issue. The VS2008 debugger works well and I was able to see what data was causing my problem. In the code I provided above the Cells[8] in "gvCADs.Rows[e.NewEditIndex].Cells[8].Text;" just needed to be changed to Cells[9] which contained the Added date.

As for being kicked back to the login screen once the code above worked, I just had to include the new "~/CallCenter/UpdateCAD_2012.aspx" program in the web.config file.

I hope this helps others. And thanx to all the responders. Sometimes it just takes some simple advise to kick the thought process into action.

I have yet to be disappointed with this site when it comes to getting advise and help. Great site!!
 
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