Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Failed to convert parameter value from a String to

Author  Topic 

sajitha
Starting Member

10 Posts

Posted - 2012-03-28 : 00:40:40
Hi
I am getting error while inserting details ie,
"Failed to convert parameter value from a String to DateTime". I am facing the same problem, wherever I use ajax Calender Control.


This is my stored Procedure

USE [PMS]
GO
/****** Object: StoredProcedure [dbo].[WorkInsertion] Script Date: 03/28/2012 15:38:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[WorkInsertion]
(@WorkDone nvarchar,
@StartDate datetime,
@StartTime time(7),
@StopDate datetime,
@StopTime time(7),
@Username nvarchar)

AS
INSERT INTO Work(WorkDone, StartDate, StartTime, StopDate, StopTime, Username) VALUES(@WorkDone, @StartDate, @StartTime, @StopDate, @StopTime, @Username);

This is my .CS file

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["PMSConnectionstrings"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("WorkInsertion", conn);
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.Add("@WorkDone", System.Data.SqlDbType.NVarChar, 500);
comm.Parameters["@WorkDone"].Value = workTextBox.Text;
comm.Parameters.Add("@StartDate", System.Data.SqlDbType.DateTime);
comm.Parameters["@StartDate"].Value = startdateTextBox.Text;
comm.Parameters.Add("@StartTime", System.Data.SqlDbType.Time);
comm.Parameters["@StartTime"].Value = starttimeTextBox.Text;
comm.Parameters.Add("@StopDate", System.Data.SqlDbType.DateTime);
comm.Parameters["@StopDate"].Value = stopdateTextBox.Text;
comm.Parameters.Add("@StopTime", System.Data.SqlDbType.Time);
comm.Parameters["@StopTime"].Value = stoptimeTextBox.Text;
comm.Parameters.Add("@Username", System.Data.SqlDbType.NVarChar);
comm.Parameters["@Username"].Value = "Administrator";

try
{
conn.Open();
comm.ExecuteNonQuery();
errorLabel.Text = "Inserted Work details Successfully.......";


}

catch
{
errorLabel.Text = "Error inserting work details! Please " +
"try again later, and/or change the entered data!";


}

finally
{
conn.Close();
}

}

Any help appreciated

dinakar
Master Smack Fu Yak Hacker

2507 Posts

Posted - 2012-03-28 : 00:51:40
I am not a .NET developer but I think you need to convert the date type values to Date format explicitly before you pass them to comm.Parameters collection.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

rajarajan
Starting Member

48 Posts

Posted - 2012-03-28 : 01:06:26
u can use Convert.datetime(String Variable) in C# before sending to the Stored Procedure
i can see u have used time ,try with datetime
Go to Top of Page

charlesbecon
Starting Member

3 Posts

Posted - 2016-12-14 : 01:26:44
You can use TryParse

public static bool TryParse(
string s,
out DateTime result
)

This method is similar to the DateTime.Parse(String) method, except that the TryParse(String, DateTime) method does not throw an exception if the conversion fails. More...string to date

http://net-informations.com/q/faq/stringdate.html

Charles


Go to Top of Page
   

- Advertisement -