You should check your data type, the 'hrs' in Database is 'Datetime' or others?
Golia
Connection.Connection con = new Connection.Connection(); ds = con.mydata();
what is the mydata() in your action, the problem may lies in the procedure you stored the data, Can you share this method?
or you can use my demo to fill the data to DataSet from Database:
public ActionResult Recovery()
{
SqlConnection conn = new SqlConnection("Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=Golia1111;Integrated Security=SSPI;");
conn.Open();
SqlCommand cmd = new SqlCommand("Select sID,t_hrs FROM PersonModels", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
List<PersonModel> lmd = new List<PersonModel>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
lmd.Add(new PersonModel
{
t_hrs = dr["t_hrs"] == DBNull.Value ? (DateTime?)null : (DateTime?)dr["t_hrs"],
sID = Convert.ToInt32(dr["sID"]),
});
}
return View(lmd);
}
Result:
Best Regards,
Jerry Cai
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
58 Points
120 Posts
Setting and storing time in MVC 5 time input field
Jan 10, 2021 06:26 PM|Golia|LINK
Hi all,
I have an MVC app.
I make a call to a database, get my data in a dataset and store it in my class.
Heres how im storing my data from the dataset
The field "hrs" that comes back from mysql is null and thats fine.
When i try to store 11:11 hour:minute value in my "hrs" field the value is store correctly on the database.
But when I return on webpage i get the following error:
on controller this line
The time value memorized in database is
11:11:00.00000
And type of field is
I have tried, without success edit
to
How do fix this error?
Thanks
All-Star
57904 Points
15521 Posts
Re: Setting and storing time in MVC 5 time input field
Jan 10, 2021 07:47 PM|bruce (sqlwork.com)|LINK
If you use the debugger you can see what data type dr[“hrs”] is and why the cast fails.
Member
58 Points
120 Posts
Re: Setting and storing time in MVC 5 time input field
Jan 10, 2021 08:39 PM|Golia|LINK
I have posted the debug error
Member
190 Points
176 Posts
Re: Setting and storing time in MVC 5 time input field
Jan 10, 2021 10:32 PM|SurferOnWww|LINK
What is Type of column "hrs" in the DataTable of the DataSet in question?
I guess that the error occurs because the Type is neither "DataTime?" nor "DateTime".
Participant
790 Points
272 Posts
Re: Setting and storing time in MVC 5 time input field
Jan 11, 2021 03:15 AM|Jerry Cai|LINK
Hi,Golia
You should check your data type, the 'hrs' in Database is 'Datetime' or others?
what is the mydata() in your action, the problem may lies in the procedure you stored the data, Can you share this method?
or you can use my demo to fill the data to DataSet from Database:
Result:
Best Regards,
Jerry Cai