locked
TextMode Overwriting Values RRS feed

  • Question

  • User-713783997 posted

    hello,

    Hopefully, this is enough info. I have a form which, on the top, has three basic fields, lets say Salesman, SalesDate, and ItemsSold.  Underneath, is a grid view from the same data source.  The gridview has a linkButton, which, when clicked, brings up the record in the form above, which allows that record to be updated.  In two of the fields (Salesman and ItemsSold, there are no issues.  The record is updated perfectly.  The SalesDate also works fine, however, I added a TextMode = "Date" to the Textbox, to make it more intuitive for the user.  For new entries, this is great, but for updates, when the record is brought up, it completely wipes out the existing date and replaces it with the mm/dd/yyy and in the underlying data, it records 1/1/1900.

    must be something I'm overlooking.

    thanks as always

    Friday, June 7, 2019 4:59 PM

All replies

  • User475983607 posted

    A date type input expects a yyyy-mm-dd date format.  

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

    Friday, June 7, 2019 5:24 PM
  • User-713783997 posted

     ... really struggling with this one.  Regardless of whether or not I update the [SalesDate] field, the textbox is populated mm/dd/yyyy.  In other words, lets say I have an existing record in the Gridview:

    [Salesman]: Moe Howard

    [SalesDate]: 6/1/2019

    [TotalSales]: 30 

    Now, when I click on this record in the gridview (for updating) the textboxes abvove will populate thus:

    [Salesman]: Moe Howard

    [SalesDate]: mm/dd/yyyy

    [TotalSales]: 30

    in other words, the SalesDate was completely wiped out, and when I go to the underlying data in the table it'll be: 1/1/1900

    I was thinking the only thing I did was to include TextMode = "Date".  As I said, a normal textbox for the date work fine, I just like the way the dates go in with the TextMode = "Date"

    ….really appreciate the help!!!

    Friday, June 7, 2019 5:54 PM
  • User475983607 posted

    The date input must be in the format yyyy-mm-dd.  If you are using standard GridView binding then apply a text format to the binding expression.  This can be done in the GridView UI wizard or you can easily update the markup yourself.

    Text='<%# Eval("SalesDate", "{0:yyyy-MM-dd}") %>' />

    Or if you are using a bound field

    DataFormatString = "{0:yyyy-MM-dd}"

    You did not post any code so it's difficult to give an accurate solution.
    .

    Friday, June 7, 2019 6:34 PM
  • User-713783997 posted

    thanks again,

    I think I misunderstood the general concept of the TextMode.  Seems as though its mainly intended for Data Entry as opposed to bringing up existing records.  I did use the DataFormatString and it did give me the yyyy-mm-dd format in the Gridview….. this helps

    Friday, June 7, 2019 7:10 PM
  • User475983607 posted

    I think I misunderstood the general concept of the TextMode.  Seems as though its mainly intended for Data Entry as opposed to bringing up existing records.  I did use the DataFormatString and it did give me the yyyy-mm-dd format in the Gridview….. this helps

    It is not clear how you are populating the form.  I assumed the form is a bound control but without the code we can only guess what you are doing.

    The solution is very easy.  You must format the SalesDate text as yyyy-MM-dd.  Maybe you are populating the textbox in the code behind?

    txtSalesDate.Text = MyDate.ToString("yyyy-MM-dd");

    Friday, June 7, 2019 7:44 PM
  • User-713783997 posted

    thanks mgebhard,

    good catch, yes, when I click the button (to pull up the record) that fires a SELECT Stored Procedure (based off the ID field) and then populates the textboxes.  So if I pull up an existing Salesman with an existing date, and an existing TotalSold, the Salesman is there, and the TotalSold is there, but the date is replaced with mm/dd/yyy and the underlying field will be 1/1/1900.  Maybe something I need to do to the stored proc?

    I appreciate your diligence in trying to help me.  I'm new to this, but slowly figuring it out

    Friday, June 7, 2019 7:59 PM
  • User475983607 posted

    good catch, yes, when I click the button (to pull up the record) that fires a SELECT Stored Procedure (based off the ID field) and then populates the textboxes.  So if I pull up an existing Salesman with an existing date, and an existing TotalSold, the Salesman is there, and the TotalSold is there, but the date is replaced with mm/dd/yyy and the underlying field will be 1/1/1900.  Maybe something I need to do to the stored proc?

    I appreciate your diligence in trying to help me.  I'm new to this, but slowly figuring it out

    Share your code.

    Friday, June 7, 2019 8:01 PM
  • User-713783997 posted

    don't know what you may need or not, but hopefully this will shed some light...

    default.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:HiddenField ID="hfSalesID" runat="server" />
                <table class="salesTable">
                    <tr>
                        <td colspan="2">
                            <asp:Label ID="Label1" runat="server" Text="Salesman Name"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtSalesmanName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
    
                    <tr>
                        <td colspan="2">
                            <asp:Label ID="Label2" runat="server" Text="Sales Date"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtSalesDate" runat="server" TextMode="Date"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <asp:Label ID="Label3" runat="server" Text="Number of Sales"></asp:Label>
                        </td>
                        <td>
                            <asp:TextBox ID="txtNumberOfSales" runat="server"></asp:TextBox>
                        </td>
                    </tr>
    
                    <tr>
                        <td>
                            <asp:TextBox ID="txtEmpty" runat="server" BorderStyle="none"></asp:TextBox></td>
                    </tr>
                        <tr>
    
                            <td colspan="2">
                                <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
                                <asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
                                <asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
                            </td>
                        </tr>
                    <tr>
                        <td></td>
                        <td colspan="2">
                            <asp:Label ID="lblSucessMessage" runat="server" Text="" ForeColor="Green"></asp:Label>
                        </td>
                    </tr>
    
                    <tr>
                        <td></td>
                        <td>
                            <asp:Label ID="lblErrorMessage" runat="server" Text="" ForeColor="Red"></asp:Label>
                        </td>
                    </tr>
                </table>
                <br />
                <asp:GridView ID="gvSales" runat="server" AutoGenerateColumns="False" Class="salesGrid">
                    <Columns>
                        <asp:BoundField DataField="salesmanName" HeaderText="Salesman Name" />
                        <asp:BoundField DataField="salesDate" HeaderText="Sales Date"/>
                        <asp:BoundField DataField="numberOfSales" HeaderText="Number of Sales" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkView" runat="server" CommandArgument='<%# Eval("salesID") %>' OnClick="lnk_OnClick">View</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
    
            </div>
        </form>
    </body>
    </html>
    

    ….and the Default.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection SqlCon = new SqlConnection(@"Data Source=… ;Initial Catalog=test;Integrated Security=True;");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnDelete.Enabled = false;
                FillGridView();
            }
        }
    
        protected void btnClear_Click(object sender, EventArgs e)
        {
            Clear();
        }
    
        public void Clear()
        {
            hfSalesID.Value = "";
            txtNumberOfSales.Text = "";
            txtSalesDate.Text = "";
            txtSalesmanName.Text = "";
            btnDelete.Enabled = false;
            lblErrorMessage.Text = lblSucessMessage.Text = "";
            btnSave.Text = "Save";
        }
    
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (SqlCon.State == ConnectionState.Closed)
                SqlCon.Open();
            SqlCommand cmd = new SqlCommand("CreateOrUpdateSales", SqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@SalesID", (hfSalesID.Value == "" ? 0 : Convert.ToInt32(hfSalesID.Value)));
            cmd.Parameters.AddWithValue("@SalesmanName", txtSalesmanName.Text.Trim());
            cmd.Parameters.AddWithValue("@SalesDate", txtSalesDate.Text.Trim());
            cmd.Parameters.AddWithValue("@NumberOfSales", txtNumberOfSales.Text.Trim());
            cmd.ExecuteNonQuery();
            SqlCon.Close();
            string SalesID = hfSalesID.Value;
            Clear();
    
            if (SalesID == "")
                lblSucessMessage.Text = "Saved Sucessfully";
            else
                lblSucessMessage.Text = "Updated Sucessfully";
            Response.AddHeader("REFRESH", "1.5;URL=Default.aspx");
        }
    
        void FillGridView()
        {
            if (SqlCon.State == ConnectionState.Closed)
                SqlCon.Open();
            SqlDataAdapter SqlDa = new SqlDataAdapter("SalesViewAll", SqlCon);
            SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dtbl = new DataTable();
            SqlDa.Fill(dtbl);
            SqlCon.Close();
            gvSales.DataSource = dtbl;
            gvSales.DataBind();
        }
    
        protected void lnk_OnClick(object sender, EventArgs e)
        {
            int SalesId = Convert.ToInt32((sender as LinkButton).CommandArgument);
            if (SqlCon.State == ConnectionState.Closed)
                SqlCon.Open();
            SqlDataAdapter SqlDa = new SqlDataAdapter("SalesViewByID", SqlCon);
            SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
            SqlDa.SelectCommand.Parameters.AddWithValue("@SalesID", SalesId);
            DataTable dtbl = new DataTable();
            SqlDa.Fill(dtbl);
            SqlCon.Close();
            hfSalesID.Value = SalesId.ToString();
            txtSalesmanName.Text = dtbl.Rows[0]["salesmanName"].ToString();
            txtSalesDate.Text = dtbl.Rows[0]["salesDate"].ToString();
            txtNumberOfSales.Text = dtbl.Rows[0]["numberOfSales"].ToString();
            btnSave.Text = "Update";
            btnDelete.Enabled = true;
        }
    
    
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (SqlCon.State == ConnectionState.Closed)
                SqlCon.Open();
            SqlCommand cmd = new SqlCommand("SalesDeleteByID", SqlCon);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@SalesID", Convert.ToInt32(hfSalesID.Value));
            cmd.ExecuteNonQuery();
            SqlCon.Close();
            Clear();
            FillGridView();
            lblSucessMessage.Text = "Sucessfully Deleted";
        }
    }
    
    

    ...not sure if this will be much help without the actual database and such, but again, I appreciate this

    Friday, June 7, 2019 8:34 PM
  • User-713783997 posted

    darn.  I forgot to add.... I was able to replicate this in Chrome, but not Explorer....

    Friday, June 7, 2019 8:39 PM
  • User475983607 posted

    As stated several time now... You must set the date format to yyyy-MM-dd!

    txtSalesDate.Text = ((DateTime)dtbl.Rows[0]["salesDate"]).ToString("yyyy-MM-dd");

    Saturday, June 8, 2019 12:38 PM
  • User-713783997 posted

    works perfectly.  I think I needed it verbatim in front of me (which also tells me I need to hit the books a bit)  Anyway, I appreciate this and I hope others can benefit as well.

    thank you!!!

    Saturday, June 8, 2019 2:59 PM