How can I Parse Time from a DateTime and enter into database

Hi. I am using .Net framework 3.5 with C#, and SQL 2008 for my web app.

I have a field on a form that is Time(7) in my SQL table.  When data is entered into a subform it shows as hh:mm but when I add it to my child gridview it is displaying date/time.  I am trying to parse the time from it so I can add my child record to my database.

This is what I am using:
 
string l_format = "", string l_type = "";

ctrl = Survey_sub.FindControl(dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim());  // this returns the time format to use

if (l_type == "Time")
                        {
                            l_format = CheckFormatType(dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim());
                            
                            DateTime dt = new DateTime();
                             
                            if (l_format == "HH:mm" || l_format == "hh:mm")
                            {
                                
                                dt = Convert.ToDateTime(txt.Text.Trim());
                                String strformatted = String.Format("{0:HH:mm}", dt);

                                txt.Text = strformatted;
                               
                                dr[dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim()] = txt.Text.Trim();
                            }
                            else if (l_format == "HH:mm:ss" || l_format == "hh:mm:ss")
                            {
                                dt = Convert.ToDateTime(txt.Text.Trim());
                                String strformatted = String.Format("{0:HH:mm}", dt);

                                txt.Text = strformatted;
                                dr[dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim()] = txt.Text.Trim();
                            }
                        }

Open in new window


   ------- This is still displaying DateTime instead of time.
ptslvAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AshokSr. Software EngineerCommented:
String strformatted = dt.ToString("HH:mm");
0
ptslvAuthor Commented:
ashok111,

I changed my code to what you suggested.  I am still getting "10/3/2013 2:56:00 PM" in my gridview control.  And when I hit submit I get:

Input string was not in a correct format.Couldn't store <10/3/2013 2:56:00 PM> in Start_Time Column. Expected type is TimeSpan.

I don't use a timeSpan data type.  Why is it looking for a timespan?
0
AshokSr. Software EngineerCommented:
It might not be your C# code.

The problem might be in your aspx code.

Post the code from apsx file.
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

AshokSr. Software EngineerCommented:
dsGrid is probably configured to expect timeSpan data type in that column.
0
AshokSr. Software EngineerCommented:
If you were not getting this error before, then we are half way there (I mean almost).
0
ptslvAuthor Commented:
My gridview columns are generated through the code.  The datatypes are set in the code.  The datatype for this Start_Time field is "Time".  I am using this line of code to go to a function to check the datatype of the field:

l_type = CheckDataType(dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim());

Open in new window


Then the code that I posted previously above is run.

Here is my gridview on the aspx page.  

<asp:GridView ID="grdvwDetails" runat="server" 
        BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" 
        CellPadding="4" ForeColor="Black" GridLines="Vertical" 
        AutoGenerateEditButton="True" OnRowDataBound="grdvwDetails_RowDataBound" 
        style="text-align: center" OnSelectedIndexChanged="grdvwDetails_SelectedIndexChanged">
                                <RowStyle BackColor="#F7F7DE" />
                                <FooterStyle BackColor="#CCCC99" />
                                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                                <AlternatingRowStyle BackColor="White" />
    </asp:GridView>

Open in new window

0
dejaanbuCommented:
try to handle it in Rowdatabound event:

protected void theGridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
	if (e.Row.RowType == DataControlRowType.DataRow) {
		DateTime ds = DateTime.Parse(e.Row.Cells[6].Text); //specifiy the correct column index here
		e.Row.Cells[6].Text = ds.ToString("hh:mm");
	}

}

Open in new window

0
ptslvAuthor Commented:
I added the code to the Rowdatabound event but get this same error message:

Input string was not in a correct format.Couldn't store <10/3/2013 2:56:00 PM> in Start_Time Column. Expected type is TimeSpan.
0
dejaanbuCommented:
are you sure, the error points to the RowDatabound event? did you check with  a breakpoint?
0
ptslvAuthor Commented:
Correct.  This is inherited code, so I am maintaining it and making certain requested changes.  I fixed the problem.  I removed the code from the RowDatabound, and added some code in addition to your parse code (modified) to the function that adds the detail record to the detail grid:

if (l_type == "Time")
                        {
                            l_format = CheckFormatType(dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim());
                            
                            DateTime dt = new DateTime();
                             
                            if (l_format == "HH:mm" || l_format == "hh:mm")
                            {
                                if (txt.Text.Trim() == "")
                                {
                                    dr[dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim()] = DBNull.Value;
                                }
                                else
                                {
                                    dt = Convert.ToDateTime(txt.Text.Trim());
                                    DateTime ds = DateTime.Parse(dt.ToString("HH:mm"));
                                    String strformatted = dt.ToString("HH:mm");
                                    txt.Text = strformatted;
                                    dr[dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim()] = txt.Text.Trim();
                                }
                            }
                            else if (l_format == "HH:mm:ss" || l_format == "hh:mm:ss")
                            {
                                if (txt.Text.Trim() == "")
                                {
                                    dr[dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim()] = DBNull.Value;
                                }
                                else
                                {
                                    dt = Convert.ToDateTime(txt.Text.Trim());
                                    DateTime ds = DateTime.Parse(dt.ToString("HH:mm:ss"));
                                    String strformatted = dt.ToString("HH:mm:ss");
                                    txt.Text = strformatted;
                                    dr[dsGrid.Tables[lblTableName.Text.Trim()].Columns[i].ColumnName.Trim()] = txt.Text.Trim();
                                }
                            }

Open in new window


There was an area in the code that was setting the field to a DateTime, and I had to change it to a string datatype to get it to drop the date.  The only thing I am not able to do is drop the seconds from displaying when the format is hh:mm.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ptslvAuthor Commented:
There are several areas in the code that are looking for DateTime data type.  I had to find the area where the dataset for the datagrid was being set, and change the datatype to a string:

dt.Columns.Add(ds.Tables[0].Rows[i].ItemArray.GetValue(5).ToString().Trim(), typeof(string));
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.