1

ASP.NET with gridview resets all date fields to null when editing (using SQL Server 2012, c#, VS 2017)

I have looked at the one post regarding this, but the answer does not resolve the problem.

Code Snippet below:

aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    DropDownList DropDownList6 = (DropDownList)GridView1.Rows[GridView1.EditIndex].Cells[2].FindControl("DropDownList6");
    DropDownList DropDownList5 = (DropDownList)GridView1.Rows[GridView1.EditIndex].Cells[3].FindControl("DropDownList5");
    DropDownList DropDownList3 = (DropDownList)GridView1.Rows[GridView1.EditIndex].Cells[4].FindControl("DropDownList3");
    TextBox TextBox4 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[5].FindControl("TextBox4");
    TextBox TextBox7 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[6].FindControl("TextBox7");
    TextBox TextBox8 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[7].FindControl("TextBox8");
    TextBox TextBox9 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[8].FindControl("TextBox9");
    TextBox TextBox10 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[9].FindControl("TextBox10");
    TextBox TextBox2 = (TextBox)GridView1.Rows[GridView1.EditIndex].Cells[10].FindControl("TextBox2");
    SqlDataMaintenanceTasks.UpdateParameters["Location"].DefaultValue = DropDownList6.SelectedValue;
    SqlDataMaintenanceTasks.UpdateParameters["Phase"].DefaultValue = DropDownList5.SelectedValue;
    SqlDataMaintenanceTasks.UpdateParameters["Periodicity"].DefaultValue = DropDownList3.SelectedValue;
    SqlDataMaintenanceTasks.UpdateParameters["Task"].DefaultValue = TextBox4.Text;
    SqlDataMaintenanceTasks.UpdateParameters["Team"].DefaultValue = txtTeam.SelectedValue;
    SqlDataMaintenanceTasks.UpdateParameters["TimeEstimation"].DefaultValue = TextBox7.Text;
    SqlDataMaintenanceTasks.UpdateParameters["ScheduledDate"].DefaultValue = TextBox8.Text;
    SqlDataMaintenanceTasks.UpdateParameters["DueDate"].DefaultValue = TextBox9.Text;
    SqlDataMaintenanceTasks.UpdateParameters["CompletionDate"].DefaultValue = TextBox10.Text;
    SqlDataMaintenanceTasks.UpdateParameters["Comments"].DefaultValue = TextBox2.Text;
    SqlDataMaintenanceTasks.Update();
}

.aspx code:

                <asp:TemplateField HeaderText="ScheduledDate" SortExpression="ScheduledDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("ScheduledDate") %>' TextMode="Date"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtSchedDate" runat="server" TextMode="Date"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label9" runat="server" Text='<%# Bind("ScheduledDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="DueDate" SortExpression="DueDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("DueDate") %>' TextMode="Date"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtDueDate" runat="server" TextMode="Date"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label10" runat="server" Text='<%# Bind("DueDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="CompletionDate" SortExpression="CompletionDate">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox10" runat="server" Text='<%# Bind("CompletionDate") %>' TextMode="Date"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtCompDate" runat="server" TextMode="Date"></asp:TextBox>
                    </FooterTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label11" runat="server" Text='<%# Bind("CompletionDate") %>' TextMode="Date"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
  • What does 'resets all date fields to null ` mean? The fields are null in the database? Might that have to do something with trying to insert strings into DateTime fields? – oerkelens Nov 26 '17 at 18:32
  • you need to setup breakpoints on your `asp.net Page_Load` method also understand what `PostBacks` are.. read up on the `IsPostBack` method all button clicks in a asp.net web page trigger postbacks.. you should also read up about Session variables.. you are losing the data due to incorrect handling / of PostBack is my initial / best guess – MethodMan Nov 26 '17 at 18:34
  • Null means what was 25/11/2017 00:00:00 is changed to blank. Please remember I am a novice at asp.net and c#. – Jim Farnworth Nov 26 '17 at 19:16
  • This part of code is not help at all. Debug your code step by step to find where the fields are reset. – Aristos Nov 26 '17 at 20:14
  • changed to blank _where_? (as a previous commenter already asked!). In the database? On the screen? Both? There's not enough info here to understand the flow of the program or where the issue is. If need be, step through your code in the debugger to find where it starts being "blank". – ADyson Nov 26 '17 at 20:50
  • Changed to blank on the screen and database as soon as you press update even if no changes made. I did step through the code but all that showed was that it went blank when "SqlDataMaintenanceTasks.Update();" was triggered. – Jim Farnworth Nov 26 '17 at 21:40
  • This is the .aspx code: Sorry can't put this in comments! – Jim Farnworth Nov 27 '17 at 07:50
  • No you can't, but you _can_ edit the question. Look for the little "edit" button just underneath the "c#, asp.net" etc. tags – ADyson Nov 27 '17 at 07:58
  • 1
    .aspx code added. Thanks ADyson. Shows how green I am! – Jim Farnworth Nov 27 '17 at 08:27
  • if it goes blank when you do the database update, then really it's probably the database code which we need to look at. – ADyson Nov 27 '17 at 14:39
  • Dear stackoverflow as a newbee I am not qualified to automatically move this discussion to chat. – Jim Farnworth Nov 27 '17 at 16:42
  • Just another bit of information, if you change one date and say Comments, that date updates but the other dates will clear. Change any other field in the gridview all date fields are lost??? – Jim Farnworth Nov 27 '17 at 18:47
  • SOLVED: ASP.NET with gridview resets all date fields to null when editing (using SQL Server 2012, c#, VS 2017). Simply had to change SQL Server date fields to Text. This does mean fiddling with char to date conversion for reports etc. but it is manageable. – Jim Farnworth Dec 05 '17 at 06:02
  • Why do some people comment in order for them look good when they do not have any idea of the answer? Thanks to ADyson for being helpful and polite. PS: I am not trying to be obscure I want to help with my experience (how BAD that is) in order to help, because BELIEVE or NOT I have done some cool stuff with ASP.NET! – Jim Farnworth Dec 17 '17 at 20:16

1 Answers1

0

SOLVED: ASP.NET with gridview resets all date fields to null when editing (using SQL Server 2012, c#, VS 2017). Simply had to change SQL Server date fields to Text. This does mean fiddling with char to date conversion for reports etc. but it is manageable.