Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have a stored procedure like
SQL
create procedure sp_sample
@Id int,
@join_date date,
@class_time time
As
Begin
 Insert into stud(SID,SJoinDate,SClassTime) values(@Id,@join_date,@class_time)
End


Now I have an ASP.NET web form to submit the details using the above stored procedure.
In the asp.net I am defining the variables as

C#
string Id = txtID.Text;
string join_date =txtDate.Text;
string class_time = txtTime.Text;

I want to insert particular date and time in the textbox.
Now if I insert the values in the web form using the stored procedure,I am getting the error like

"Error while converting datatype nvarchar to time".

Can anyone help help me about this conversion error.

Thank you,
Posted
Comments
DamithSL 25-Jun-12 10:53am    
Can you update the question with your insert code sample?
palcordis 25-Jun-12 11:12am    
Here is my Insert menthod,

public int InsertStud(string ID,string Join_Date, string class_Time)
{
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand cmd = new SqlCommand("usp_sample", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("ID",@ID);
cmd.Parameters.AddWithValue("Join_Date", @Join_Date);
cmd.Parameters.AddWithValue("class_Time", @class_Time);
return cmd.ExecuteNonQuery();
con.Close();

}

Join Date is in string format, but in Stored procedure you have declared as DateTime, so use

Convert.ToDateTime() to convert it into DateTime
 
Share this answer
 
"Error while converting datatype nvarchar to time".
Based on your error, it looks like error lies here:
SQL
@class_time time


Whatever you are passing class_time in
C#
string class_time = txtTime.Text;
is not of correct time format that is expected in stored procedure.

Make sure that the data passed for class_time is in correct time format expected.
Refer: MSDN: DataType time (Transact-SQL)[^]
 
Share this answer
 

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