Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a database to save details of online-exam generation.
Here i want to save duration of exam time and i am using datetime datatype for this, but it takes current date also which is not needed.
...below is my table structure...
create table Exams
(
slNo int primary key identity(1,1),
examID varchar(10) unique  not null,
title varchar(500) not null,
examHours datetime not null,
totalQn integer not null,
totalMarks integer not null
);


The user inputs the time in three textboxes in format txtHH , txtmm , and txtSS respectively.
here is codebehind page...
SqlConnection con = GlobalConnect.FnConnection();
            con.Open();
            string exmTime = string.Format(txtHH.Text.ToString() + ":" + txtMM.Text.ToString() + ":" + txtSS.Text.ToString());
            SqlCommand cmd = new SqlCommand("Insert into dbo.Exams(examID,title,examHours,totalQn,totalMarks) values(@examID,@title,@examHours,@totalQn,@totalMarks)");            
            cmd.Parameters.AddWithValue("@examID", lblExmId.Text);
            cmd.Parameters.AddWithValue("@title", txtTitle.Text.Trim());
            cmd.Parameters.AddWithValue("@examHours",Convert.ToDateTime( exmTime));
            cmd.Parameters.AddWithValue("@totalQn", TxtNoOfQn.Text.Trim());
            cmd.Parameters.AddWithValue("@totalMarks", TxtTotMarks.Text.Trim());          

            int effect = GlobalConnect.ExecuteNonQuery(cmd);


Please suggest me how to do this.
Posted

Use int to store the duration in minutes. This you can convert to hours & min as per your need.
 
Share this answer
 
Comments
[no name] 21-Nov-13 3:44am    
Thanks for nice suggestions. I have got the idea.
Use an integer, and store the cumulative number of seconds instead: you can convert that to a number of hours and minutes pretty easily if you need to, and the conversion to set teh DB value is trivial code.

However, you need to patch up that code: if the user enters a wrong character in a text box, or specifies 77 minutes, your website will crash. Always check your inputs!
 
Share this answer
 
Comments
[no name] 21-Nov-13 3:45am    
Thanks for nice suggestions. I have got the idea.

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